基于行值禁用网格视图中的链接按钮

时间:2014-06-11 09:31:40

标签: c# asp.net

在下面的代码中,我有一个网格视图,其中包含6列DocumentID,Documentname,View,Edit,ViewButton,EditButton。我的目标是,如果View为false,则应禁用ViewButton,如果Edit为false,则禁用editbutton.But我试过它抛出null引用异常请帮我做这个。

<asp:GridView Width="100%" runat="server" ID="srchgrid" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" ShowFooter="false" 
                  PageSize-Mode="NumericPages" PageSize="10" PagerStyle-Visible="true" OnPageIndexChanging="Search_PageIndex" AllowPaging="true" AllowSorting="true"  
                    CssClass="mGrid"
                    PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"  OnRowCommand="srchgrid_RowCommand" OnRowDataBound="srchgrid_RowDataBound" >
                  <Columns>          

            <asp:TemplateField HeaderText="Document ID" ItemStyle-Width="150px" Visible="false">
                <ItemTemplate>
                    <asp:Label ID="DocumentID"  runat="server"  Text='<%#Eval("DocumentID") %>' > </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>



            <asp:TemplateField HeaderText="Document Name" ItemStyle-Width="200px"  >              
                <ItemTemplate>                                              
                    <asp:Label ID="DocumentName"  runat="server"  Text='<%#Eval("DocumentName") %>'> </asp:Label>
                </ItemTemplate>                
            </asp:TemplateField>

             <asp:TemplateField HeaderText="View" ItemStyle-Width="200px"   >              
                <ItemTemplate>                                              
                    <asp:Label ID="View"  runat="server"  Text='<%#Eval("ViewDoc") %>'> </asp:Label>
                </ItemTemplate>                
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Edit" ItemStyle-Width="200px"   >              
                <ItemTemplate>                                              
                    <asp:Label ID="Edit"  runat="server"    Text='<%#Eval("EditDoc") %>'> </asp:Label>
                </ItemTemplate>                
            </asp:TemplateField>
                  <asp:TemplateField HeaderText="ViewButton" itemstyle-width="150px">
                            <ItemTemplate>
                                <asp:LinkButton ID="btnView" runat="server" CommandName="View" Text="View"  CausesValidation="false"/>
                            </ItemTemplate>    
                        </asp:TemplateField> 

                         <asp:TemplateField HeaderText="EditButton" itemstyle-width="150px">
                            <ItemTemplate>
                                <asp:LinkButton ID="btnEdit" runat="server" CommandName="View1" Text="Edit" CausesValidation="false"/>
                            </ItemTemplate>    
                        </asp:TemplateField>       
                       </Columns>

                     <HeaderStyle Font-Bold="True" ForeColor="White" />
    </asp:GridView> 


    protected void srchgrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       foreach (GridViewRow gr in srchgrid.Rows)
                {

                    Label View = gr.Cells[2].Controls[2] as Label;
                    if (View.Text == "false")
                    {
                        LinkButton btnView = (LinkButton)gr.FindControl("btnView");
                        btnView.Enabled = false;

                    }

                }
        foreach (GridViewRow gr in srchgrid.Rows)
            {
                Label Edit = gr.Cells[3].Controls[3] as Label;
                if (Edit.Text == "false")
                {
                    LinkButton btnEdit = (LinkButton)gr.FindControl("btnEdit");
                    btnEdit.Enabled = false;

                }

            }

    }

1 个答案:

答案 0 :(得分:0)

创建每个RowDataBound后,将调用<GridView Row个事件。因此,RowDataBound事件会在e.Row事件中获得一个行元素。利用它来查找每一行中的适当元素,如

e.Row.FindControl("<control_name>")

您的代码可以更改为

protected void srchgrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if(e.Row.RowType == DataControlRowType.DataRow) //to access data rows only (not to deal with HeaderRow and FooterRow
       {
           Label View = e.Row.FindControl("View") as Label;
           if(View != null)
           {
              LinkButton btnView = e.Row.FindControl("btnView") as LinkButton;
              if(btnView != null && View.Text.Trim() == "false")
              {
                 btnView.Enabled = false;
              }
           }

           Label Edit = e.Row.FindControl("Edit") as Label;
           if(View != null)
           {
              LinkButton btnEdit = e.Row.FindControl("btnEdit") as LinkButton;
              if(btnEdit != null && Edit.Text.Trim() == "false")
              {
                 btnEdit.Enabled = false;
              }
           }
       }
    }
相关问题