ASP.NET GridView控件按钮事件 [英] ASP.NET GridView Button Event

查看:153
本文介绍了ASP.NET GridView控件按钮事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图触发在GridView按钮事件。我创建了以下code的GridView:

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>

我绑定数据与数据集,这一切工作正常。
但现在我试图触发Save_Check事件,只是看起来像:

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).

任何想法?我在asp.net没有专家。我通常只code C#或Web服务,有时Silverlight的。但是这一次,我想用asp.net做。

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.

推荐答案

您应该在GridView添加OnRowCommand事件,然后执行一个事件处理程序。在您的按钮,而不是实施的OnClick你应该optionaly刚刚提供的属性的CommandName和CommandArgument即:

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") %>'/>

然后在你的OnRowCommand事件处理程序,您可以添加您的code

Then in your OnRowCommand event handler you can add your code

string test = "test";

按钮总是会触发OnItemCommand事件,即使你不指定的CommandName属性的点击,然而,这可以让你有一个排多个按钮,使每一个将执行不同的functionallity。
该CommandArgument,您可以为您的functionallity一个参数。例如,如果你想通过你所看到的人的ID,您可以通过CommandArgument =&LT;%#的eval(是PersonID)%>

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") %>

这篇关于ASP.NET GridView控件按钮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆