Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2006

Tip: Looking for answers? Try searching our database.

Get DataKey on RowDataBound

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MasterChief - 15 Feb 2006 21:27 GMT
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.
tdavisjr - 15 Feb 2006 21:41 GMT
> 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.
[quoted text clipped - 6 lines]
>
> 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.
tdavisjr - 15 Feb 2006 21:49 GMT
Oops.  The index to the DataKeys is probably an integer so please cast
c.CommandArgument appriately.
MasterChief - 15 Feb 2006 23:03 GMT
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.ToString
   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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.