没有在行绑定事件中获得控制权
本文关键字:控制权 事件 绑定 | 更新日期: 2023-09-27 18:04:02
我有一个网格视图在我的项目。如果项目以管理员身份登录,它具有编辑和查看的功能。但是当我作为用户登录时,我想禁用编辑选项。我试图禁用编辑选项,这是一个链接按钮内模板字段,但我得到错误这样做。
下面是我试过的代码:
protected void gvDocuments_RowDataBound(object sender, GridViewRowEventArgs e)
{
string username = Convert.ToString(Session["Username"]);
LinkButton btn = (LinkButton)gvDocuments.FindControl("lbtnEdit");
if (username == "user")
{
btn.Visible = false;
}
}
谁能告诉我我哪里出错了?编辑:
<asp:GridView ID="gvDocuments" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvDocuments_RowDataBound" OnRowCommand="gvDocuments_RowCommand" PageSize="5" Width="100%" RowStyle-BackColor="#9FCFFF" SortedAscendingHeaderStyle-VerticalAlign="Bottom" SortedAscendingHeaderStyle-HorizontalAlign="Left" BackColor="#9BCDFF" HeaderStyle-BackColor="#51A8FF">
<asp:TemplateField HeaderText="Actions">
<itemtemplate>
<asp:LinkButton ID="lbtnView" runat="server" CommandName="View" Text="View" ForeColor="#0033CC"></asp:LinkButton>
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="DocEdit" Text="Edit" ForeColor="#0033CC"></asp:LinkButton>
<asp:LinkButton ID="lbtnRenew" runat="server" CommandName="Email" Text="Email" ForeColor="#0033CC"></asp:LinkButton>
</itemtemplate>
</asp:TemplateField>
</asp:GridView>
您需要在单个项目中找到控件,而不是gvDocuments.FindControl
。
所以用下面的…
e.Row.FindControl("lbtnEdit");
(这是使用我对<asp:Repeaters>
的经验,而不是<asp:GridView>
,但我的理解是相同的)
我的理解是错误的,根据MSDN,它不是.Item
…它是.Row
请根据您的要求修改此样品:
。aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ddlGridview._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" AutoGenerateColumns ="False" OnRowEditing="GridView1_RowEditing" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText ="ID">
<ItemTemplate>
<asp:Label runat ="server" ID="lblID" Text ='<%#Eval("ID")%>'></asp:Label>
<asp:Label runat ="server" ID="Label1" Text ='<%#Eval("username")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="lk1">
<ItemTemplate>
<asp:LinkButton runat ="server" ID ="lk1" CommandName ="Downl" Text ="lk1" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"ID") %>' ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="lk2">
<ItemTemplate>
<asp:LinkButton runat ="server" ID ="lk2" CommandName ="approve" Text ="lk2" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"ID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="lk3">
<ItemTemplate>
<asp:LinkButton runat ="server" ID ="lk3" CommandName ="rework" Text ="lk3" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"ID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
CS file:
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = CreateDataTable();
GridView1.DataBind();
}
private DataTable CreateDataTable()
{
DataTable myDataTable = new DataTable();
DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "id";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "username";
myDataTable.Columns.Add(myDataColumn);
DataRow row;
row = myDataTable.NewRow();
row["id"] = "1";
row["username"] = "hungn";
myDataTable.Rows.Add(row);
DataRow row1;
row1 = myDataTable.NewRow();
row1["id"] = "2";
row1["username"] = "hoanq";
myDataTable.Rows.Add(row1);
return myDataTable;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Downl")
{
int index = Convert.ToInt32(e.CommandArgument);
// get the GridViewRow where the command is raised
GridViewRow Row = ((GridViewRow)GridView1.Rows[index-1]);
LinkButton approve = Row.FindControl("lk1") as LinkButton;
approve.Enabled = true;
LinkButton rework = Row.FindControl("lk2") as LinkButton;
rework.Enabled = true;
LinkButton comment = Row.FindControl("lk3") as LinkButton;
comment.Enabled = false;
}
}
如果还有问题请告诉我。