2017-02-28 57 views
0

我想通過RowDataBound從gridview中獲取鏈接按鈕,但它返回null,爲什麼?名字是正確的。即使你可以在代碼中看到,但它仍然不起作用。爲什麼rowdatabound在gridview中找不到鏈接按鈕?

GridView控件:

 <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); 
    } 
} 

磅總是空。爲什麼?

回答

0

抹任何東西的RowDataBound事件之前,你必須使用這個條件

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

嘗試這種方式

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