【问题标题】:Gridview check constraints before editing编辑前 Gridview 检查约束
【发布时间】:2015-01-03 12:18:12
【问题描述】:

我有一个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,但我看不到如何在我的验证函数中启动编辑。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    你的想法是对的。添加 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;
              }
          }
    }
    

    【讨论】:

    • 这是解决我问题的好方法。考虑到我在同一页面上编辑我的答案,重定向应该包含在我的案例中的 if 语句中。
    【解决方案2】:

    其他解决方案是将启用属性设置为Edit Button。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2014-03-20
      • 2011-01-28
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多