C# 在Gridview_RowEditing()期间取消编辑?

C# 在Gridview_RowEditing()期间取消编辑?,c#,asp.net,gridview,visual-studio-2013,postback,C#,Asp.net,Gridview,Visual Studio 2013,Postback,如果当前用户没有必要的权限,我想在行编辑期间取消行编辑 这就是行编辑的样子: protected void GridView_RowEditing(object sender, GridViewEditEventArgs e) { string user = GetCurrentUser(); if (user == string.Empty) { /* Show message alert */ return; } Gri

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

这就是
行编辑的样子:

protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
    string user = GetCurrentUser();
    if (user == string.Empty)
    {
        /* Show message alert */
        return;
    }
    GridView.EditIndex = e.NewEditIndex;
    BindData();
}
这将取消更新,列将继续编辑链接。但如果我再次单击“编辑”,它将显示此错误:

未能加载viewstate。正在加载viewstate的控件树必须与上次请求期间用于保存viewstate的控件树匹配。例如,动态添加控件时,回发期间添加的控件必须与初始请求期间添加的控件的类型和位置匹配

我想在进行回发之前,必须先隐藏控件

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

我还尝试设置
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;
}
受保护的无效GridView1\u行编辑(对象发送者,System.Web.UI.WebControls.GridViewEditEventArgs e)
{
//NewEditIndex属性,用于确定正在编辑的行的索引。
gvbookstorecords.EditIndex=e.NewEditIndex;
gridDataBind();
}
受保护的无效GridView1_行更新(对象发送方,System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
//从Gridview中查找要更新的行的控件
TextBox Name=gvbookstorecords.Rows[e.RowIndex].FindControl(“Name”)作为TextBox;
TextBox Birthdate=gvbookstorecords.Rows[e.RowIndex].FindControl(“Birthdate”)作为TextBox;
TextBox Gender=gvbookstorecords.Rows[e.RowIndex].FindControl(“Gender”)作为TextBox;
//将EditIndex属性设置为-1可取消Gridview中的编辑模式
gvbookstorecords.EditIndex=-1;
//调用ShowData方法以显示更新的数据
gridDataBind();
}
受保护的无效GridView1\u行取消编辑(对象发送者,System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
{
//将EditIndex属性设置为-1可取消Gridview中的编辑模式
gvbookstorecords.EditIndex=-1;
gridDataBind();
}    
*--------------------------------------------------------------------------------*
*

我刚才也有同样的问题。然后我才发现这很容易修复。 调用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();
    }
}

这是什么?你能解释一下你的答案吗?我用上面的C代码进行编辑。在“编辑”中,用户可以更新或取消,并且可以使用ASPX代码进行UI网格设计。如果它不起作用,那么为什么要将其作为答案发布?
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();
    }
}