1

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NoobCoder
  • 127
  • 1
  • 12
  • Add a DataKey to your GridView, so you can easily identify the object for the row. Add a Checkbox to each row, and then put your button outside the GridView...When the button is clicked you loop through each row, find the Checkbox control for that row and find out if it is checked....if it is, grab the DataKey and get the object you need. – JohnPete22 Oct 25 '19 at 12:29
  • That does sound promising, You got a rough example on this? – NoobCoder Oct 25 '19 at 12:50
  • https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.datakeys?view=netframework-4.8 – JohnPete22 Oct 25 '19 at 12:52
  • https://stackoverflow.com/questions/2818203/get-datakey-values-in-gridview-rowcommand – JohnPete22 Oct 25 '19 at 12:53
  • There are quite a few questions in SO on the subject. I would suggest starting there. Can look up....How to Find controls in ASP GridView? How to loop through GridView Rows? How to use DataKeys from GridView? Between all those questions, I think you can start formulating a solution. If you are still having issues, post the updated code and error message and I can help out. – JohnPete22 Oct 25 '19 at 12:55
  • 1
    Thanks for the idea and help, I answered my own question and linked a handy guide on this sort of thing. Thanks for the idea – NoobCoder Oct 25 '19 at 13:14

1 Answers1

0

I found a good solution to my issue using the Checkboxes instead, the link below is what I used.

https://www.c-sharpcorner.com/UploadFile/009464/use-checkbox-inside-gridview-in-Asp-Net/

NoobCoder
  • 127
  • 1
  • 12
  • Glad to hear it! I wasn't trying to be rude when I gave the idea of "do some research yourself" -- but it's the best type of learning. Pointing someone in the right direction is better than just doing it for them. – JohnPete22 Oct 25 '19 at 13:17
  • 1
    Oh no no, you helped and wasn't rude, just a fresh idea was what I needed. Thanks, now I know how to how to use Gridviews just a bit better now :D, Thanks John. – NoobCoder Oct 25 '19 at 13:36