Reputation: 469
In an ASP.NET GridView control
Is it possible to select a row without using the OnSelectedIndexChanged
event?
e.g. suppose I want to have a custom button on the GridView called "ProcessThis" for every row in the grid, and I want an OnClick
method to then access the selected row values.
If that is not possible, is it then possible to alter the text and position of the select button generated by OnSelectedIndexChanged
?
Upvotes: 0
Views: 1828
Reputation: 5636
Yes it is possible to select a row without using the OnSelectedIndexChanged event.
You have to use RowCommand event of GridView
like
C#
protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ProcessThis")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = Gridview1.Rows[index];
// Do what ever you want....
}
}
.aspx
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False"
onrowcommand="Gridview1_RowCommand">
<Columns>
<asp:ButtonField Text="Process This" CommandName="ProcessThis" />
</Columns>
</asp:GridView>
You have to assign CommandName
in the button that you are using inside your GridView
.
Hope you understand and works for you.
Upvotes: 1
Reputation: 124746
You could simply add a custom button to the GridView
whose CommandName
property is Select
. Then use the SelectedIndexChanged
event as usual.
Why do you say you don't want the SelectedIndexChanged
event to fire? Note that this event will fire even if you change the selected index in code by calling the GridView.SelectRow
method.
Upvotes: 1
Reputation: 17973
You can use the RowCommand
event on the gridview for that purpose. It is triggered every time a button is clicked in the gridview.
If you use the DataKeys
property in your markup you can fetch a specific ID for instance from the row of the button, in order to process a specific row. Examples are available in the docs.
Upvotes: 0