编辑前的Gridview检查约束

时间:2015-01-03 12:18:13

标签: c# asp.net gridview

我有一个gridview,我想在编辑一行之前检查一些约束。更具体地说,如果用户是该帖子的作者,则他能够编辑该行。

<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>
                        &nbsp;<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>
                        &nbsp;<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,但我不知道如何在我的验证函数中启动Edit。

2 个答案:

答案 0 :(得分:0)

你正在思考。添加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事件查找编辑按钮,并在权限检查后启用/禁用它。

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;
          }
      }
}

答案 1 :(得分:0)

其他解决方案是将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
}