Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Saturday, November 17, 2007

Identify Button Clicked in GridView

I had a GridView with a ButtonField used to Select a record to display a User's details in a DetailsView. I wanted a second button to display different form for changing a Users's password, since I wanted that to be a separate option.

GridView (usersGrid):

<asp:ButtonField CommandName="Select" Text="Select" />
<asp:LinkButton runat="server" ID="changePasswordButton" Text="Change Password" OnClick="usersGrid_SelectRow" />

Code Behind:

protected void usersGrid_SelectRow(object sender, EventArgs e)
GridViewRow row = (GridViewRow)((LinkButton)sender).Parent.Parent;
usersGrid.SelectedIndex = row.RowIndex;

MyDatabase.DBUtil = new MyDatabase.DBUtil();
changePasswordDetails.DataSource = DB.GetUser(Int16.Parse(usersGrid.SelectedValue.ToString());
userDeatils.Visible = false;
changePasswordDetails.Visible = true;
changePasswordDetails.DataKeyNames = new string[] { "UserID" };

No comments:

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.