ASP.NET GridView Button Event

I’m trying to trigger a button event in a 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 the data with a dataset, this all works fine.
But now I’m trying to trigger the Save_Check event which simply looks like:

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

However I always get an error: “Server error in application, wrong argument on repost”. (It’s in dutch so I tried to translate it as clearly as possible).

Any ideas? I’m no expert in asp.net. I normally only code in c# or webservices, and sometimes silverlight. But this time I wanted to do it with asp.net.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You should add an OnRowCommand event in the GridView and then implement an event handler. On your Button instead of implementing OnClick you should optionaly just provide the attributes CommandName and CommandArgument i.e:

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

Then in your OnRowCommand event handler you can add your code

string test = "test";

The click of the Button will always trigger the OnItemCommand event, even if you do not specify the CommandName attribute, however this allows you to have multiple buttons on a row, so that each one will perform a different functionallity.
The CommandArgument allows you to provide an argument for your functionallity. If for example you wanted to pass the ID of the Person you are seeing, you could pass CommandArgument=”<%# Eval(“PersonID”) %>

Method 2

I tried what u said. In a very simple way. I noticed that gridview didn’t have the onItemCommand eventtrigger, but i just 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>

Like this it works. However if I create a templatecolumn with a button inside it, it gives the same error. Also.. If i change the buttontype of the buttoncolumn to “pushbutton” it again gives the same error. It works with a linkbutton… What’s the difference? I really would like to have a button cuz a link just looks ugly 😉

Cheers


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x