【问题标题】:Stop GridView_RowUpdating firing from the GridView_RowCommand in ASP.Net停止从 ASP.Net 中的 GridView_RowCommand 触发 GridView_RowUpdating
【发布时间】:2012-09-04 10:19:44
【问题描述】:

我有一个 GridView 控件并启用了编辑功能。

我的编辑和更新按钮都是链接按钮,如下:

<asp:LinkButton ID="buttonEdit" runat="server" Text="Edit" CausesValidation="false"
                            CommandName="Edit" />

<asp:LinkButton ID="buttonUpdate" runat="server" CausesValidation="True"
                            CommandName="Update" Text="Update" ValidationGroup="Edit" />

当用户单击编辑按钮时,其中一列有一个允许编辑记录的文本框:

<EditItemTemplate>
    <asp:TextBox ID="textBoxEdit" runat="server" Text='<%#Eval("Name") %>' />
    <asp:Label ID="labelEditWarning" CssClass="error" runat="server" Text="Name already exists" Visible="false" />
</EditItemTemplate>

当用户单击更新链接按钮时,网格的 RowCommand 事件会触发。在这里,我想对数据库中的现有记录进行验证。如果验证失败,那么如何阻止网格的 RowUpdating 事件触发但似乎没有办法做到这一点!?

protected void gridName_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Edit"))
    {
        //Perform validation & cancel update if the validation fails.
    }
}

protected void gridName_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
     //Update my record. But I don't want this to fire if my validation fails in 
     //the row command event.
}

谁能帮忙?

我正在使用 ASP.Net 4.0

提前致谢。

【问题讨论】:

    标签: asp.net gridview rowcommand


    【解决方案1】:

    将“buttonUpdate”上的 CommandName 从“Update”更改为“rename”。这将停止触发 RowUpdating 事件。然后,您可以在 RowCommand 事件中添加一些代码来处理记录的验证和更新,例如

    protected void gridName_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("rename"))
        {
            if(validation == true)
            {
              DatabaseDataContext data = new DatabaseDataContext();
    
              string rowID = e.CommandArguement.ToString();
              var rowToUpdate = data.TableOne.Where(d => d.ID.ToString() == rowID);
              rowToUpdate.Name = newName;
    
              data.SubmitChanges();
            }
            else
            {
              //Set error label
            }
        }
    }
    

    您还需要将 Button 的 CommandArguement 更改为:

    <asp:LinkButton ID="buttonUpdate" runat="server" CausesValidation="True"
                            CommandName="Update" CommandArguement='<%# Eval("ID") %>' Text="Update" ValidationGroup="Edit" />
    

    【讨论】:

    • 感谢您的回复,但如果我这样做,那么我无法知道哪一行正在被更改。我目前在 gridName_RowUpdating 事件中使用 GridViewUpdateEventArgs 来执行此操作
    • 你可以这样做:DataBinder.Eval(e.Row.DataItem,"ID");然后用它来查找数据库中的记录。
    • 道歉我认为这可能是错误的,因为我刚刚测试过它,它似乎并不喜欢它。另一种方法是在按钮的 CommandArguement 中使用行的 ID,例如 。然后使用 this 的值来查找数据库中的记录。
    【解决方案2】:

    你可以在你的 gv_RowCommand 事件中更新

    示例代码:

    protected void gridName_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName=="Edit")
        {
            //Perform validation & cancel update if the validation fails.
        }
    
       if(your validtaion successconditon flag set true)
        {    
           if (e.CommandName == "Update")
           {
           }
        }
    }
    

    【讨论】:

    • 感谢您的回复。这将如何停止 RowUpdating 事件?它仍然会触发,并且会更新记录。
    【解决方案3】:

    您还可以使用 GridViewUpdateEventArg 的 Cancel 属性取消更新。

    例如

       protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
          // Your validation logic goes here...
    
         // If validation logic fails...
         e.Cancel = true;
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 2019-11-23
      相关资源
      最近更新 更多