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

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

问题描述

我正在尝试在 gridview 中触发按钮事件.我使用以下代码创建了一个 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 的专家.我通常只使用 c# 或 webservices 编写代码,有时也使用 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 事件,然后实现一个事件处理程序.在你的 Button 而不是实现 OnClick 你应该可选地只提供属性 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 事件处理程序中您可以添加您的代码

Then in your OnRowCommand event handler you can add your code

string test = "test";

单击 Button 将始终触发 OnItemCommand 事件,即使您没有指定 CommandName 属性,但是这允许您在一行中有多个按钮,以便每个按钮执行不同的功能.CommandArgument 允许您为您的功能提供参数.例如,如果你想传递你所看到的人的 ID,你可以传递 CommandArgument="<%# 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天全站免登陆