473,324 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Get DataKey on RowDataBound

I am trying to add an attribute to a gridview row so when you click on
it, it will pop up an alert saying the current datakey for that row.
How is it possible to get a datakey when using the rowdatabound I want
to do something like

If (e.Row.RowType = DataControlRowType.DataRow) Then
e.row.attribute.add("onClick", "alert('" + KeyID + "')'")
End If

but I don't know how to get the KeyID? Any help would be appreciated.

Feb 15 '06 #1
3 17288

MasterChief wrote:
I am trying to add an attribute to a gridview row so when you click on
it, it will pop up an alert saying the current datakey for that row.
How is it possible to get a datakey when using the rowdatabound I want
to do something like

If (e.Row.RowType = DataControlRowType.DataRow) Then
e.row.attribute.add("onClick", "alert('" + KeyID + "')'")
End If

but I don't know how to get the KeyID? Any help would be appreciated.


You need to hande the RowCommand Event.

Then retreive the datakey

DataKey key = GridView1.DataKeys[e.CommandArgument]

Then the the key should have a Value property which is the datakey.
Now, I'm doing this totally from memory; but this should point you in
the right diretion.

Feb 15 '06 #2
Oops. The index to the DataKeys is probably an integer so please cast
c.CommandArgument appriately.

Feb 15 '06 #3
I can get the DataKey using the RowCommand Event but I want to be able
to put the OnClick attribute into the Row and have it alert the KeyID.
When I try to get the value of KeyID that I got in RowCommand it comes
back with a null value. The only way I can find to add an attribute to
a row is during the RowDataBound Event.

Public Class _MasterTestBed
Inherits System.Web.UI.Page
Dim KeyID As String
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.GridViewCommandEventArgs ) Handles
GridView1.RowCommand
KeyID = GridView1.DataKeys(e.CommandArgument).Value.ToStri ng
End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
e.Row.Attributes.Add("onMouseOver",
"this.style.backgroundColor='lightgrey'")
e.Row.Attributes.Add("onMouseOut",
"this.style.backgroundColor='Transparent'")
e.Row.Attributes.Add("onClick", "alert('" + KeyID + "')'")
End If
End Sub
End Class

Feb 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Andy Sutorius | last post by:
Hi, I have a datalist with a asp:button. I have onitemcommand firing when the button is clicked. I am attempting to get the datakey of the button that was clicked. I can retrieve a specific...
3
by: Eustice Scrubb | last post by:
When deleting in a Datagrid, I'm using a Datakey to reference the deletion since the specific table I'm deleting from doesn't have a index or ID for each entry. Sub myDataGrid_Delete(ByVal...
7
by: Joel Reinford | last post by:
I am looking for a way to set the selectedindex of a datagrid based on the datakey value. For example given this grid with a datakey of OrderID: OrderID Item 54 A 98 ...
1
by: mike | last post by:
i'd like to get datakey of GridView So i write code like below but it wasn't work well. how should i do? protected void BookGridView_RowDataBound(object sender, GridViewRowEventArgs e) { int...
1
by: Mark Richards | last post by:
I posted this earlier, and got no response. Basically I need to be able to grab a value from one of the fields in a gridView to populate a dropdown in another column of the gridview: I have a...
3
by: ryan.mclean | last post by:
Hello everyone, I'm in a bind, and I hope somebody can point me in the right direction. I have a gridview that is bound when it is not a postback. When the grid is bound, I remove the contents...
1
by: Jim in Arizona | last post by:
I'm don't know how I would get around this. When working with a datalist control, there a specific built in commands (OnEditCommand, OnCancelCommand, etc). These commands, when the datalist...
2
by: Rod | last post by:
While working with a datagrid I have a value for the datakey, from this I want to find the ItemIndex. How please? many thanks in advance.
5
by: jrobbins | last post by:
public IEnumerable<AuthorizationObjectClass> GetEmployeeNoAuthorizations(SqlInt32 PersonnelNumber) { DataSet ds; AuthorizationObjectClass auth = new...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.