单元格文本始终在Gridview rowdatabound中变为空字符串(“”)

时间:2018-04-29 04:52:43

标签: asp.net

我正在尝试在gridview中绑定的用户列表中禁用admin角色的删除按钮。

所以,我选择rowdatabound选项,我可以根据条件禁用,就像检查角色列的文本一样。

如果角色列文本是“Admin”,那么我必须禁用我自定义的“删除”按钮。

不幸的是,在获取数据时我无法获取单元格的文本。 我使用了如下语句:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Button btn = (e.Row.FindControl("Delbutton") as Button);

        if(e.Row.Cells[7].Text == "ADMIN")
        {
            btn.Enabled = false;
        }
    }
}

我的aspx页面:

<asp:GridView ID="GridView1" runat="server"  
       AutoGenerateColumns="False" 
       OnPageIndexChanging="GridView1_PageIndexChanging" 
       PagerSettings-Mode="Numeric" 
       PagerSettings-PreviousPageText="Previous"
        PagerSettings-PageButtonCount="15" 
       PagerSettings-NextPageText="Next" 
       PagerStyle-ForeColor="WhiteSmoke" 
       OnRowDataBound="GridView1_RowDataBound">
       <Columns>
           <asp:TemplateField>
               <ItemTemplate>
                   <asp:button ID="button1" runat="server" Text="Select" OnClick="button1_Click"  ></asp:button>
               </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="ID">
               <ItemTemplate>
                   <asp:Label ID="id" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
               </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Email">
               <ItemTemplate>
                  <asp:Label ID="idemail" runat="server" Text='<%#Eval("Email") %>'></asp:Label>
               </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Name">
               <ItemTemplate>
                  <asp:Label ID="idName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
               </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Passwor">
               <ItemTemplate>
                  <asp:Label ID="idPassword" runat="server" Text='<%#Eval("Passwor") %>'></asp:Label>
               </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="MobileNumber">
               <ItemTemplate>
                  <asp:Label ID="idMobileNumber" runat="server" Text='<%#Eval("MobileNumber") %>'></asp:Label>
               </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Gender">
               <ItemTemplate>
                  <asp:Label ID="idGender" runat="server" Text='<%#Eval("Gender") %>'></asp:Label>
               </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Qulification">
               <ItemTemplate>
                  <asp:Label ID="idQulification" runat="server" Text='<%#Eval("Qulification") %>'></asp:Label>
               </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Role">
               <ItemTemplate>
                  <asp:Label ID="idRole" runat="server" Text='<%#Eval("Role") %>'></asp:Label>
               </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField>
               <ItemTemplate>
                   <asp:button ID="Delbutton" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure, Do you want to delete.')" OnClick="Delbutton_Click" ></asp:button>
               </ItemTemplate>
           </asp:TemplateField>
       </Columns>
        <PagerSettings NextPageText="Next" PageButtonCount="15" PreviousPageText="Previous"></PagerSettings>
    </asp:GridView>

Please check the image of my code on debugger

This was my data on page after loaded

0 个答案:

没有答案