【问题标题】:Why rowdatabound can't find a link button in gridview?为什么rowdatabound在gridview中找不到链接按钮?
【发布时间】:2017-02-28 09:31:49
【问题描述】:

我试图通过 RowDataBound 从 gridview 获取链接按钮,但它返回 null,为什么?名字是正确的。即使您可以在代码中看到,但它仍然不起作用。

网格视图:

     <asp:GridView ID="grdViewWorks" OnRowDataBound="grdViewWorks_RowDataBound" runat="server" OnRowCommand="grdViewWorks_RowCommand" AutoGenerateColumns="false" EmptyDataText="No Data Found"
CssClass="table table-responsive table-bordered table-striped">
    <Columns>
        <asp:TemplateField HeaderText="Work No">
            <ItemTemplate>
                <asp:Label ID="lblWorkNo" runat="server" Text='<%# Eval("WorkNo") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="NIT No">
            <ItemTemplate>
                <asp:Label ID="lblNITNo" runat="server" Text='<%# Eval("NIT_No") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="WorkName" HeaderText="Work Name" />
        <asp:BoundField DataField="OpeningDate" HeaderText="Opening Date" />
        <asp:BoundField DataField="OpeningTime" HeaderText="Opening Time" />
        <asp:BoundField DataField="OrganizationName" HeaderText="Organization" />
        <asp:BoundField DataField="OfficeName" HeaderText="Office" />
        <asp:TemplateField HeaderText="Show Contractors">
            <ItemTemplate>
                <asp:LinkButton ID="btnShowContractors" runat="server" Text="Show Contractors"
                    OnClick="btnShowContractors_Click"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

.cs

protected void grdViewWorks_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        LinkButton lb = e.Row.FindControl("btnShowContractors") as LinkButton;
        ScriptManager.GetCurrent(this).RegisterPostBackControl(lb);
    }
    catch (Exception ex)
    {

        Utility.Msg_Error(this.Master, ex.Message);
    }
}

lb 始终为空。为什么 ?

【问题讨论】:

    标签: c# asp.net .net gridview webforms


    【解决方案1】:

    在对 rowdatabound 事件添加任何内容之前,您必须使用此条件

      if (e.Row.RowType == DataControlRowType.DataRow)
        {
           LinkButton lb = e.Row.FindControl("btnShowContractors") as LinkButton;
            ScriptManager.GetCurrent(this).RegisterPostBackControl(lb);
         }
    

    【讨论】:

      【解决方案2】:

      试试这个方法

      if (e.Row.RowType == DataControlRowType.DataRow)
      {
      LinkButton lb = e.Row.FindControl("btnShowContractors") as LinkButton;
      }
      

      【讨论】:

        猜你喜欢
        • 2014-11-20
        • 1970-01-01
        • 1970-01-01
        • 2012-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多