在Gridview_RowEditing()期间要取消编辑吗?

时间:2019-01-31 13:53:00

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

如果当前用户没有必要的权限,我想在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();
}

这将取消 Update ,并且该列将继续显示 Edit 链接。但是,如果我再次单击 Edit ,它将显示此错误:

  

无法加载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;
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-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>*