erockman9
asked on
click anywhere in gridview asp.net
i'd like to be able to click a row in gridview and send the data from one column in a database to another page. I'm using aspx and i've tried a bunch of solutions but i can't get the page to response.redirect. with the data from the table.
ASKER
Visual Studio 2008.
using master pages and web content pages. it seems like it should be so much easier than it is. i must be missing something obvious.
i can do it easily with a hyperlink but it needs to be able to work with a click anywhere on the row.
i've attached what i have so far...i was hoping i could just add one more line with a onclick command.
using master pages and web content pages. it seems like it should be so much easier than it is. i must be missing something obvious.
i can do it easily with a hyperlink but it needs to be able to work with a click anywhere on the row.
i've attached what i have so far...i was hoping i could just add one more line with a onclick command.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='FFFF99'")
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='FFFFFF'")
Ok I think you need to override the Render method like this. Apologies but this is in C#
protected override void Render(HtmlTextWriter writer)
{
AddOnClickAttribute();
base.Render(writer);
}
And add the following method:
private void AddOnClickAttribute()
{
if (gridRS.Rows.Count > 0)
{
foreach (GridViewRow row in gridRS.Rows)
{
if (row.RowType == DataControlRowType.DataRow )
row.Attributes.Add("onclic k", Page.ClientScript.GetPostB ackEventRe ference(gr idRS, "Select$" + row.RowIndex.ToString() + "," + row.Cells[1].Text, true));
}
}
}
row.Cells[1] can be changed to use whatever column you need to use to identify the row.
You can then add this method:
protected void gridRS_RowCommand(object sender, GridViewCommandEventArgs e)
{
int RowId;
RowId = System.Convert.ToInt32((st ring)e.Com mandArgume nt);
}
And this gives you the value that was in the Cells[1] and you can do whatever you like with it.
Make sense?
protected override void Render(HtmlTextWriter writer)
{
AddOnClickAttribute();
base.Render(writer);
}
And add the following method:
private void AddOnClickAttribute()
{
if (gridRS.Rows.Count > 0)
{
foreach (GridViewRow row in gridRS.Rows)
{
if (row.RowType == DataControlRowType.DataRow
row.Attributes.Add("onclic
}
}
}
row.Cells[1] can be changed to use whatever column you need to use to identify the row.
You can then add this method:
protected void gridRS_RowCommand(object sender, GridViewCommandEventArgs e)
{
int RowId;
RowId = System.Convert.ToInt32((st
}
And this gives you the value that was in the Cells[1] and you can do whatever you like with it.
Make sense?
ASKER
i think i'm close now...
i'm a little unclear on the first part
protected override void Render(HtmlTextWriter writer)
{
AddOnClickAttribute();
base.Render(writer);
do i load that in pageload sub in the code behind?
i'm a little unclear on the first part
protected override void Render(HtmlTextWriter writer)
{
AddOnClickAttribute();
base.Render(writer);
do i load that in pageload sub in the code behind?
ASKER
maybe i'm not that close...i must be having a mental block i thought i'd done this before. i'm losing my mind!!!!
i can play with the syntax but do you think my logic is even right?
i can play with the syntax but do you think my logic is even right?
Partial Public Class Eligibility_CarrierPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AddOnClickAttribute()
End Sub
Protected Sub AddOnClickAttribute()
If (GridView1.Rows.Count > 0) Then
For Each (GridViewRow row in GridView1.Rows)
Next
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Session("TextboxValue") = TextBox1.Text
Response.Redirect("Eligibility_CarrierSearch.aspx")
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='FFFF99'")
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='FFFFFF'")
e.Row.Attributes.Add("onclick",Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + Row.RowIndex.ToString() + "," + row.cells[1].text,true))
End Sub
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim RowId
RowId = System.Convert.ToInt32((String)e.CommandArgument)
End Sub
End Class
No, you need to override the Render method and that's where you put AddOnClickAttribute()
Not sure how you override in VB.net?
Not sure how you override in VB.net?
ASKER
no..i got that
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWrit er)
MyBase.Render(writer)
AddOnClickAttribute()
End Sub
now i've got to figure out the rest....
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWrit
MyBase.Render(writer)
AddOnClickAttribute()
End Sub
now i've got to figure out the rest....
Ok do the AddOnClickAttribute before the MyBase.Render though
ASKER
okay...got that!
on the next section i'm not able to add anything beyond this because it doesn't like the word ROW going in there...any ideas
Protected Sub AddOnClickAttribute(ByVal sender As Object, ByVal e As System.EventArgs)
If (GridView1.Rows.Count > 0) Then
End If
For Each (GridViewRow row in gridView1.Rows)
Next
If (Row
End Sub
on the next section i'm not able to add anything beyond this because it doesn't like the word ROW going in there...any ideas
Protected Sub AddOnClickAttribute(ByVal sender As Object, ByVal e As System.EventArgs)
If (GridView1.Rows.Count > 0) Then
End If
For Each (GridViewRow row in gridView1.Rows)
Next
If (Row
End Sub
AddOnClickAttribute is a method writen by me.
It has no parameters and is not an event handler.
It should juts look like this (In C#):
private void AddOnClickAttribute()
{
if (gridRS.Rows.Count > 0)
{
foreach (GridViewRow row in gridRS.Rows)
{
if (row.RowType == DataControlRowType.DataRow )
row.Attributes.Add("onclic k", Page.ClientScript.GetPostB ackEventRe ference(gr idRS, "Select$" + row.RowIndex.ToString() + "," + row.Cells[1].Text, true));
}
}
}
It has no parameters and is not an event handler.
It should juts look like this (In C#):
private void AddOnClickAttribute()
{
if (gridRS.Rows.Count > 0)
{
foreach (GridViewRow row in gridRS.Rows)
{
if (row.RowType == DataControlRowType.DataRow
row.Attributes.Add("onclic
}
}
}
ASKER
i think thats my problem...i should use C## instead of VB...
i made the changes to VB and it looks like this
Protected Sub AddOnClickAttribute()
If (GridView1.Rows.Count > 0) Then
End If
For Each Row In GridView1.Rows
Next
BUT IT FALLS APART AFTER I PUT IN THE NEXT PART
If (e.row.RowType = DataControlRowType.DataRow ) Then
e.row.Attributes.Add("oncl ick", Page.ClientScript.GetPostB ackEventRe ference(Gr idView1, "Select$" + row.RowIndex.ToString() + "," + row.Cells[1].Text, true)
End If
End Sub
SEE ANYTHING OBVIOUS I'M DOING WRONG????
i made the changes to VB and it looks like this
Protected Sub AddOnClickAttribute()
If (GridView1.Rows.Count > 0) Then
End If
For Each Row In GridView1.Rows
Next
BUT IT FALLS APART AFTER I PUT IN THE NEXT PART
If (e.row.RowType = DataControlRowType.DataRow
e.row.Attributes.Add("oncl
End If
End Sub
SEE ANYTHING OBVIOUS I'M DOING WRONG????
Can't see anything obvious but I don't do VB.net.
Is it not compiling? Any specific error messages?
Is it not compiling? Any specific error messages?
ASKER
using
Protected Sub AddOnClickAttribute() i get the error that e is not declared
so then i add the stuff after the () so it looks like this:
(Protected Sub AddOnClickAttribute(ByVal sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs )
and then my
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWrit er)
AddOnClickAttribute()
MyBase.Render(writer)
GIVE ME THIS ERROR
Error 1 Argument not specified for parameter 'e' of 'Protected Sub AddOnClickAttribute(sender As Object, e As System.Web.UI.WebControls. GridViewRo wEventArgs )'. C:\Users\Eric Hart\Documents\Visual Studio 2008\Projects\WebApplicati on16\WebAp plication1 6\Eligibil ity_Carrie rSearch.as px.vb 6 9 WebApplication16
Protected Sub AddOnClickAttribute() i get the error that e is not declared
so then i add the stuff after the () so it looks like this:
(Protected Sub AddOnClickAttribute(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.
and then my
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWrit
AddOnClickAttribute()
MyBase.Render(writer)
GIVE ME THIS ERROR
Error 1 Argument not specified for parameter 'e' of 'Protected Sub AddOnClickAttribute(sender
No you don't need any parameters!
Here is my method:
private void AddOnClickAttribute()
{
if (gridRS.Rows.Count > 0)
{
foreach (GridViewRow row in gridRS.Rows)
{
if (row.RowType == DataControlRowType.DataRow )
row.Attributes.Add("onclic k", Page.ClientScript.GetPostB ackEventRe ference(gr idRS, "Select$" + row.RowIndex.ToString() + "," + row.Cells[1].Text, true));
}
}
}
There is no e in there. You have somehow got an e in your code, that is teh problem.
Here is my method:
private void AddOnClickAttribute()
{
if (gridRS.Rows.Count > 0)
{
foreach (GridViewRow row in gridRS.Rows)
{
if (row.RowType == DataControlRowType.DataRow
row.Attributes.Add("onclic
}
}
}
There is no e in there. You have somehow got an e in your code, that is teh problem.
ASKER
that seems to get me closer...
except i get name 'e' is not declared...
how do i declare it without the paremeters?
Protected Sub AddOnClickAttribute()
If (GridView1.Rows.Count > 0) Then
End If
For Each Row In GridView1.Rows
Next
If (e.Row.RowType = DataControlRowType.DataRow ) Then
e.row.Attributes.Add("oncl ick", Page.ClientScript.GetPostB ackEventRe ference(Gr idView1, "Select$" + row.RowIndex.ToString() + "," + row.Cells[1].Text, true)
End If
End Sub
except i get name 'e' is not declared...
how do i declare it without the paremeters?
Protected Sub AddOnClickAttribute()
If (GridView1.Rows.Count > 0) Then
End If
For Each Row In GridView1.Rows
Next
If (e.Row.RowType = DataControlRowType.DataRow
e.row.Attributes.Add("oncl
End If
End Sub
ASKER
SHOULD I TRY A NEW QUESTION?
i'm not sure i'm making the transition from C## to VB very well.
i'm not sure i'm making the transition from C## to VB very well.
No need for a new question, stick with it!
Your first If Statement just opens and closes
Your Foreach has no code within it
Your code has an e in there which has just appeared out of nowhere!
The code below is more like what it shoudl be - may not be quite right but is along the right lines.
Protected Sub AddOnClickAttribute()
If (GridView1.Rows.Count > 0) Then
For Each row In GridView1.Rows
If (row.RowType = DataControlRowType.DataRow ) Then
row.Attributes.Add("onclic k", Page.ClientScript.GetPostB ackEventRe ference(Gr idView1, "Select$" + row.RowIndex.ToString() + "," + row.Cells[1].Text, true)
End If
Next
End If
End Sub
Your first If Statement just opens and closes
Your Foreach has no code within it
Your code has an e in there which has just appeared out of nowhere!
The code below is more like what it shoudl be - may not be quite right but is along the right lines.
Protected Sub AddOnClickAttribute()
If (GridView1.Rows.Count > 0) Then
For Each row In GridView1.Rows
If (row.RowType = DataControlRowType.DataRow
row.Attributes.Add("onclic
End If
Next
End If
End Sub
ASKER
getting close!!!!!!
im getting these errors
Overload resolution failed because no accessible 'GetPostBackEventReference ' accepts this number of arguments.
plus
identifier expected here
row.Cells[1]
im getting these errors
Overload resolution failed because no accessible 'GetPostBackEventReference
plus
identifier expected here
row.Cells[1]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Told you we would sort it!
Is this for asp.net? If so which version and which version of Visual Studio?