没有控制rowdatabound事件

时间:2014-05-19 03:52:54

标签: c# asp.net .net

我的项目中有一个网格视图。如果项目以管理员身份登录,则可以编辑和查看。但当我以用户身份登录时,我想禁用编辑选项。 我正在尝试禁用编辑选项,这是模板字段中的链接按钮,但我收到错误。

以下是我到目前为止尝试的代码:

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>

2 个答案:

答案 0 :(得分:0)

而不是gvDocuments.FindControl,您需要在单个项目中找到控件。

所以请使用以下内容......

e.Row.FindControl("lbtnEdit");

(这是使用我对<asp:Repeaters>的体验,而不是<asp:GridView>,但我的理解是它是相同的。)

根据MSDN,我的理解是错误的,它不是.Item ...它是.Row

答案 1 :(得分:0)

请根据您的要求修改此样本:

.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档案:

    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;

        }
    }

如果仍然面临问题,请告诉我。