I want to be able to select one or many rows in my gridview so I am able to store many rows data in an object and then perform whatever I need with the list of objects.
In the ASP code, I have a standard gridview and a TemplateField with an ItemTemplate which I have placed my button. The command name is Switch
(this will be relevant in C#). Below is my templateField code (and I've tried AutoPostBack="False"
which does not work in this case)
<asp:TemplateField>
<ItemTemplate>
<asp:Button CssClass="buttonStuff" Text="Switch" runat="server" CommandName="Switch" CommandArgument="<%# Container.DataItemIndex %>" AutoPostBack="False" />
</ItemTemplate>
</asp:TemplateField>
Now when the Switch
button is clicked on any row, we grab the correct row index and get our values in C#, code below:
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Switch")
{
//Determine the RowIndex of the Row whose Button was clicked.
int rowIndex = Convert.ToInt32(e.CommandArgument);
//Reference the GridView Row.
GridViewRow row = GridView2.Rows[rowIndex];
//Perform what needs to do done
}
}
Now, I've grabbed the correct values and can do what I need to do for that one button click but the issue is that, I cannot just select many rows but just one since the page keeps firing when I click on the button once.
I believe ideally, I would want to disable this auto post back and add it in manually myself in this case.
Any suggestions will be much appreciated, thank you