Click event for individual cells in gridview

I am working with asp.net3.5 C #. I want to make a click event for a cell in a GridView . I actually use a DataTable and assign it a GridView .

I want a click event for a cell. Is it possible? If possible, please help me how to solve this.

+4
source share
2 answers

You will need to register a click event for the cell, which will send it back to the server for your processing. Found This that can help you achieve a cell click.

Here it was done by adding the onclick attribute to the cell in the RowDataBound event, I'm not quite sure that this is what you want, but it can be useful.

+4
source

Hi, you can solve this with this code:

something along these lines

I assumed that you have a button in each row of the grid, and you want to find out in which row you clicked the button, and get the value of another specific cell in the same row that this button is associated with:

  protected void Downloadbtn_Click(object sender, EventArgs e) { Button clickedButton = sender as Button; GridViewRow clickedGridViewRow = (GridViewRow)clickedButton.Parent.Parent; string x = clickedGridViewRow.Cells[AnotherCellNumberInTheSameRowWhoseValueYouWantToGet].Text; } 

And you can link the button in each row of the gridview by specifying this code on the .aspx page where gridview is present.

  <Columns> <asp:TemplateField ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Button ID="Downloadbtn" Text="Download" runat="server" OnClick="Downloadbtn_Click"></asp:Button> </ItemTemplate> </asp:TemplateField> </Columns> 

And you want the AutoGenerateColumns property of the Grid view to be true.

Hope this answers your question.

0
source

Source: https://habr.com/ru/post/1345999/


All Articles