【问题标题】:C# : RowUpdating Method not called when Update is clicked in a gridViewC#:在 gridView 中单击更新时未调用 RowUpdating 方法
【发布时间】:2011-08-19 02:23:30
【问题描述】:

我有一个 gridView,它有一个编辑、删除、更新和取消链接按钮来执行相关的功能。

编辑、删除和取消命令工作正常。

但问题是,当我单击 gridView 上的更新链接按钮时,RowUpdating 事件没有被触发。

可能出了什么问题?

这是我的代码:

<asp:GridView ID="gvCompanies" runat="server" AutoGenerateColumns="False" AllowPaging="True" 
            onpageindexchanging="gvCompanies_PageIndexChanging" 
            onrowediting="gvCompanies_RowEditing" PageSize="20" 
            onrowcancelingedit="gvCompanies_RowCancelingEdit" 
            onrowdeleting="gvCompanies_RowDeleting" 
            onrowupdating="gvCompanies_RowUpdating" >
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID" ReadOnly="True" />
                <asp:BoundField DataField="name" ItemStyle-Width="200px" 
                    ItemStyle-HorizontalAlign="Left" HeaderText="Name" >
               <ItemStyle HorizontalAlign="Left" Width="200px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="address" HeaderText="Address">
                    <ItemStyle HorizontalAlign="Left" Width="200px" />
                </asp:BoundField>
                <asp:BoundField DataField="city" ItemStyle-Width="200px" 
                    ItemStyle-HorizontalAlign="Left" HeaderText="City" >
                 <ItemStyle HorizontalAlign="Left" Width="200px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="state" ItemStyle-Width="200px" 
                    ItemStyle-HorizontalAlign="Left" HeaderText="State" >
                    <ItemStyle HorizontalAlign="Left" Width="200px" />
                </asp:BoundField>
                <asp:BoundField DataField="zipcode" HeaderText="Zip Code" />
                <asp:BoundField DataField="telephone" HeaderText="Telephone" />
                <asp:BoundField DataField="fax" HeaderText="Fax" />
                <asp:BoundField DataField="mobile" HeaderText="Mobile" />
                <asp:BoundField DataField="website" HeaderText="Website" />
                <asp:BoundField DataField="email" HeaderText="Email" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            </Columns>
        </asp:GridView>

在 .cs 文件中我的 RowUpdating 事件如下

    protected void gvCompanies_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //My Code for Update
    }

注意:我在 Stackoverflow 上查看了所有类似的问题,但似乎没有一个对我有用。所以我发布了这个问题

【问题讨论】:

  • 当我实现你的代码时它工作正常。你申请 gvCompanies.EditIndex = e.NewEditIndex;在行编辑中
  • @John K:我在 GridView 中没有找到像“ShowUpdateButton”这样的选项。
  • @Fraz Sundal:是的,我已经实现了,我的编辑、删除和取消按钮会响应那里的事件,但只有更新不起作用
  • 抱歉,我错了,我在想一些不同的东西 - ShowUpdateButton 不在 GridView 控件中,因为它的行为(连同取消)是基于编辑按钮的自动的。请忽略我说的。 (我已经删除了我的评论,因为它是噪音)。
  • @John K:谢谢约翰。没什么大不了。 :)

标签: .net asp.net .net-3.5 gridview


【解决方案1】:

得到了答案。

感谢那些试图帮助我的人。

必须将我的 commandButton 的 CausesValidation 属性更改为 false

希望对大家有所帮助。

【讨论】:

  • 谢谢,这正是我遇到的问题!
【解决方案2】:

编辑

尝试替换:

<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

与:

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton CommandArgument="" CommandName="Edit" runat="server" Text="Edit"/>
    </ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="False" ShowDeleteButton="true" />

将 OnRowCommand="GridView1_RowCommand1" 添加到您的数据网格:

<asp:GridView ID="gvCompanies" runat="server" AutoGenerateColumns="False" AllowPaging="True"
        OnPageIndexChanging="gvCompanies_PageIndexChanging" OnRowEditing="gvCompanies_RowEditing"
        PageSize="20" OnRowCancelingEdit="gvCompanies_RowCancelingEdit" OnRowDeleting="gvCompanies_RowDeleting"
        OnRowUpdating="gvCompanies_RowUpdating"
        OnRowCommand="GridView1_RowCommand1">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="ID" ReadOnly="True" />
            <asp:BoundField DataField="name" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Left"
                HeaderText="Name">
                <ItemStyle HorizontalAlign="Left" Width="200px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="address" HeaderText="Address">
                <ItemStyle HorizontalAlign="Left" Width="200px" />
            </asp:BoundField>
            <asp:BoundField DataField="city" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Left"
                HeaderText="City">
                <ItemStyle HorizontalAlign="Left" Width="200px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="state" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Left"
                HeaderText="State">
                <ItemStyle HorizontalAlign="Left" Width="200px" />
            </asp:BoundField>
            <asp:BoundField DataField="zipcode" HeaderText="Zip Code" />
            <asp:BoundField DataField="telephone" HeaderText="Telephone" />
            <asp:BoundField DataField="fax" HeaderText="Fax" />
            <asp:BoundField DataField="mobile" HeaderText="Mobile" />
            <asp:BoundField DataField="website" HeaderText="Website" />
            <asp:BoundField DataField="email" HeaderText="Email" />
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
        </Columns>
</asp:GridView>

然后在下面添加:

protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
{
        string command = e.CommandName; //use this to get the button that was pressed i.e. edit/delete
}

【讨论】:

  • e.CommandName 没有捕捉到Update 命令。所有其他命令都被 e.CommandName 捕获并且工作正常
  • 这很奇怪,我现在在 myne 上检查了它,它确实用 e.CommandName is Edit 捕获了它
  • 现在看看模板字段是否适用于编辑?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
相关资源
最近更新 更多