【问题标题】:Cancel Editing during Gridview_RowEditing()?在 Gridview RowEditing() 期间取消编辑?
【发布时间】:2019-06-25 00:05:28
【问题描述】:

如果当前用户没有必要的权限,我想在RowEditing 期间取消行编辑

这是RowEditing 的样子:

protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
    string user = GetCurrentUser();
    if (user == string.Empty)
    {
        /* Show message alert */
        return;
    }
    GridView.EditIndex = e.NewEditIndex;
    BindData();
}

这会取消更新,并且该列会继续编辑链接。但是如果我再次点击Edit,它会显示这个错误:

加载视图状态失败。正在加载视图状态的控制树必须与在先前请求期间用于保存视图状态的控制树匹配。例如,动态添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。

我认为它必须在回发之前隐藏控件。

所以我的问题是,我该如何避免这种情况?

我也尝试设置GridView.EditIndex = -1,但得到相同的结果:

protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView.EditIndex = e.NewEditIndex;
    BindData();
    string user = GetCurrentUser();
    if (user == string.Empty)
    {
        /* Show message alert */
        GridView.EditIndex = -1;
        BindData();
        return;
}

【问题讨论】:

    标签: c# asp.net gridview visual-studio-2013 postback


    【解决方案1】:

    我刚才也有同样的问题。然后我才发现这很容易解决。 调用 e.Cancel = true 即可解决问题

    protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string user = GetCurrentUser();
        if (user == string.Empty)
        {
           MsgBox.Prompt(Page, "Not authorize");
           e.Cancel = true;
           BindData();
        }
         else
        {
           GridView.EditIndex = e.NewEditIndex;
           BindData();
        }
    }
    

    【讨论】:

      【解决方案2】:
       protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
              {
                  //NewEditIndex property used to determine the index of the row being edited.  
                  gvBookStoreRecords.EditIndex = e.NewEditIndex;
                  gridDataBind();
              }
              protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
              {
                  //Finding the controls from Gridview for the row which is going to update  
                  TextBox Name = gvBookStoreRecords.Rows[e.RowIndex].FindControl("Name") as TextBox;
                  TextBox Birthdate = gvBookStoreRecords.Rows[e.RowIndex].FindControl("Birthdate") as TextBox;
                  TextBox Gender = gvBookStoreRecords.Rows[e.RowIndex].FindControl("Gender") as TextBox;
                  //Setting the EditIndex property to -1 to cancel the Edit mode in Gridview  
                  gvBookStoreRecords.EditIndex = -1;
                  //Call ShowData method for displaying updated data  
                  gridDataBind();
              }
              protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
              {
                  //Setting the EditIndex property to -1 to cancel the Edit mode in Gridview  
                  gvBookStoreRecords.EditIndex = -1;
                  gridDataBind();
              }    
      *--------------------------------------------------------------------------------*
          <asp:GridView ID="gvBookStoreRecords" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="gvBookStoreRecords_SelectedIndexChanged" OnRowCancelingEdit="GridView1_RowCancelingEdit"   
      
          OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                          <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                          <Columns>  
                          <asp:TemplateField>  
                              <ItemTemplate>  
                                  <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />  
                              </ItemTemplate>  
                              <EditItemTemplate>  
                                  <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>  
                                  <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>  
                              </EditItemTemplate>  
                          </asp:TemplateField>
                      </Columns>  
                          <EditRowStyle BackColor="#999999" />
                          <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                          <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                          <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                          <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                          <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                          <SortedAscendingCellStyle BackColor="#E9E7E2" />
                          <SortedAscendingHeaderStyle BackColor="#506C8C" />
                          <SortedDescendingCellStyle BackColor="#FFFDF8" />
                          <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                      </asp:GridView>*
      

      【讨论】:

      • 这是什么?你能解释一下你的答案吗?
      • 我使用上面的C#代码进行编辑。在编辑中用户可以更新或取消,以及用于 UI 网格设计的 ASPX 代码。
      • 如果它不起作用,那你为什么要发布它作为答案?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多