Gridview 在编辑前检查约束

Gridview check constraints before editing

我有一个网格视图,我想在编辑一行之前检查一些约束。更具体地说,如果用户是 post 的作者,他可以编辑该行。

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
            DataKeyNames="id" DataSourceID="SqlDataSource1" ForeColor="#333333" 
            GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                            CommandName="Update" Text="Update"></asp:LinkButton>
                         <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                            CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                            CommandName="Edit" Text="Edit"></asp:LinkButton>
                         <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                            CommandName="Delete" Text="Delete"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" 
                    SortExpression="id" />
                <asp:BoundField DataField="topicID" HeaderText="topicID" 
                    SortExpression="topicID" />
                <asp:BoundField DataField="author" HeaderText="author" 
                    SortExpression="author" />
                <asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
                <asp:TemplateField HeaderText="content" SortExpression="content">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("content") %>' class="com"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("content") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

我正在考虑添加 onclick() 并从 ItemTemplate 中删除 CommandName,但我不知道如何在我的验证函数中启动编辑。

你的想法是对的。添加 onClick() 并检查

protected void IBNew_Click(object sender, ImageClickEventArgs e)
    {
        if (!_User.HasPermission("IMS", "T00-0001", PermissionTypes.Create))
        {
            js.ShowUPAlert(this, "Permission denied – Please contact your administrator");
        }
        else
        {
            Response.Redirect("EditCategory.aspx");
        }

    }

您可以编写自己的逻辑来检查权限。

您还可以使用 RowDataBound 事件找到编辑按钮,并在权限检查后 enable/disable 它。

protected void gvShow_RowDataBound(object sender, GridViewRowEventArgs e)
{
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
          LinkButton lnkEdit= e.Row.FindControl("lnkEdit") as LinkButton;
          if(//your condition)
          {
             lnkEdit.Enable=true\false;
          }
      }
}

其他解决方案是将 Enabled 属性设置为 Edit 按钮。

<asp:LinkButton ID="LinkButtonIEDEdit" runat="server" CausesValidation="False" 
                                CommandName="Edit" Text="Edit"
                                Enabled='<%# setEdit(Convert.ToString(Eval("author"))) %>'></asp:LinkButton>

后台代码:

protected bool setEdit(string author)
{
    //check cond
}