gpt4 book ai didi

c# - Gridview 在编辑前检查约束

转载 作者:太空宇宙 更新时间:2023-11-03 15:47:00 27 4
gpt4 key购买 nike

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

最佳答案

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

关于c# - Gridview 在编辑前检查约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27754771/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com