【问题标题】:How to differentiate updating or inserting in GridView OnUpdating Event?如何区分在 GridView OnUpdating 事件中更新或插入?
【发布时间】:2013-03-23 07:47:57
【问题描述】:

我正在使用 ASP.NET 开发我的 Web 应用程序,我遇到了需要在 GridView 更新事件上区分 UPDATingINSERTING 的情况。

 protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
    //conditional check
    if(Update Flage){
        //Call Update Function
    }
    else{
        //Call Insert Function
    }
 }

我在 GridView 中有一个 ItemTemplateEditItemTemplate,当单击编辑按钮(在 ItemTemplate 上)时,然后更改为更新按钮(在 EditItemTemplate 上)。

我在 GridView 之外有一个添加按钮,单击后,将新行添加到 GridView 并将按钮文本更改为 ADD,如下代码片段:

ds.Tables[0].Rows.InsertAt(ds.Tables[0].NewRow(), 0);
GridViewID.EditIndex = 0;

LinkButton cmdButton = GridView.Rows[0].FindControl("btnUpdate") as LinkButton;
cmdButton.Text = "Add";

我知道有用于插入行的 InsertItemTemplate,但在我的情况下,我使用 GridView 之外的 Button 来添加新的编辑行。

那么,如何区分 RowUpdating 事件的编辑或插入?有什么推荐的技巧来实现这一目标吗?可能类似于添加 HiddenField 作为标志。

谢谢你。

【问题讨论】:

    标签: c# asp.net gridview edititemtemplate


    【解决方案1】:

    您可以根据该命令名称值识别事件

    例如。

     protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
         {
           string arg = e.CommandName.ToString();
        if(arg="Edit")
         {
         }
        else if(arg=="Update")
          {
          }
    

    标记

    <asp:LinkButton ID="lnkEdit" Text="Customize"  CommandName="edit"  CommandArgument='edit'  runat="server"> 
           </asp:LinkButton>  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 2012-06-16
      • 2022-12-21
      相关资源
      最近更新 更多