Button Event GridView ASP.NET

I am trying to trigger a button event in gridview. I created a gridview with the following code:

<asp:GridView id="ItemsGrid2" BorderColor="black" CellPadding="3" BorderWidth="1" HeaderStyle-BackColor="DarkSlateGray" HeaderStyle-ForeColor="White" AutoGenerateColumns="false" AllowSorting="true" OnSortCommand="Sort_Grid" runat="server" align="center" Font-Name="Verdana" Font-Size="8"> <Columns> <asp:BoundField DataField="Title" HeaderText="Title"/> <asp:BoundField DataField="Year" HeaderText="Year" /> <asp:BoundField DataField="Score" HeaderText="Score" /> <asp:BoundField DataField="Genre" HeaderText="Genre" /> <asp:HyperLinkField HeaderText="Link" DataTextField="Link" DataNavigateUrlFields="Link"/> <asp:TemplateField HeaderText="Seen"> <ItemTemplate> <asp:Button runat="server" Text="Seen" OnClick="Save_Check"/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 

I bind data to a dataset, it all works great. But now I'm trying to fire the Save_Check event, which looks simple:

 public void Save_Check(object sender, EventArgs e) { string test = "test"; } 

However, I always get the error "Server error in application, invalid argument when sending." (He is Dutch, so I tried to translate it as clearly as possible).

Any ideas? I am not an expert at asp.net. I usually only code in C # or webservices, and sometimes silverlight. But this time I wanted to do this using asp.net.

0
source share
2 answers

You must add the OnRowCommand event to the GridView, and then implement the event handler. On your Button, instead of implementing OnClick, you should optionally simply provide the CommandName and CommandArgument ie attributes:

 <asp:Button ID="Button1" runat="server" Text="Seen" CommandName="Seen" CommandArgument='<%#Eval("RecordID") %>'/> 

Then in the OnRowCommand event handler you can add your code

 string test = "test"; 

A button click always triggers the OnItemCommand event, even if you do not specify the CommandName attribute, however, this allows you to have several buttons in a row so that each of them performs a different function. CommandArgument allows you to provide an argument for your function. If, for example, you wanted to pass the identifier of the person you see, you can pass CommandArgument = "<% # Eval (" PersonID ")%>

+2
source

I tried what I said. Very simple. I noticed that gridview does not have an onItemCommand eventtrigger, but instead I used a datagrid instead.

 <asp:DataGrid ID="ItemsGrid" AutoGenerateColumns="false" runat="server" OnItemCommand="Save_Check"> <Columns> <asp:BoundColumn DataField="Title" HeaderText="Title"></asp:BoundColumn> <asp:ButtonColumn DataTextField="Year"></asp:ButtonColumn> </Columns> </asp:DataGrid> 

How it works. However, if I create a template column with a button inside it, it gives the same error. Also .. If I change the buttontype buttoncolumn to "pushbutton", it again gives the same error. It works with linkbutton ... What's the difference? I really would like to have a button because the link just looks ugly;)

Greetings

0
source

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


All Articles