Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 grab row ID from grid view in RowCreated event

Author  Topic 

jhermiz

3564 Posts

Posted - 2008-01-29 : 08:58:17
I want to add a on double click attribute to a row in a grid view.
Once the item is double clicked i want it to go to another page...
I just cant figure out how to get the ID of the row into the URL:


e.Row.Attributes.Add("ondblclick", "javascript:window.location='entry.aspx?ID='" & GrabRowIDHERE & "';return false;")


I can easily grab it in the RowDataBound event but then I cannot create the attribute in this event. I need to add this attribute in the RowCreated event.

Anyone have an idea on how to set up the url correctly I've tried snatching the value using findcontrol but the value isnt there yet. Then I tried just ..window.location='entry.aspx?ID={0}' that dont work either :(.



Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-01-29 : 09:34:24
Not sure why you are having trouble adding it on the RowDataBound event, seems like no problem to me.

Example:

create a page, add a DataView named "dv1" to it. Then add the following code (VB):


Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
Dim s As String() = {"item 1", "item2 ", "item 3"}

gv1.DataSource = s
gv1.DataBind()

End Sub

Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
Handles gv1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick", "alert('This is item: " & e.Row.DataItem.ToString() & "');")
End If
End Sub


Click on each row on the grid; seems to be no problem. Are you sure you are checking the RowType before trying to get the data item? The header and such raise the databinding event, even those they are not bound to anything.



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

jhermiz

3564 Posts

Posted - 2008-01-29 : 09:36:07
quote:
Originally posted by jsmith8858

Not sure why you are having trouble adding it on the RowDataBound event, seems like no problem to me.

Example:

create a page, add a DataView named "dv1" to it. Then add the following code (VB):


Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
Dim s As String() = {"item 1", "item2 ", "item 3"}

gv1.DataSource = s
gv1.DataBind()

End Sub

Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Handles gv1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick", "alert('This is item: " & e.Row.DataItem.ToString() & "');")
End If
End Sub


Click on each row on the grid; seems to be no problem. Are you sure you are checking the RowType before trying to get the data item? The header and such raise the databinding event, even those they are not bound to anything.



- Jeff
http://weblogs.sqlteam.com/JeffS




Ack you beat me to it, looks like I did not format it correctly the first time with my ' ' in the javascript section.

e.Row.Attributes.Add("ondblclick", "javascript:window.location='entry.aspx?ID=" & CType(e.Row.FindControl("lblID"), Label).Text & "';return false;")

This works fine thanks!

Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-01-29 : 09:39:31
Great, I knew it had to be something simple. However, I would suggest not using FindControl() but using the DataItem to get the value if you can, it is more efficient.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -