[英]Why rowdatabound can't find a link button in gridview?
I am trying to get link button from gridview via RowDataBound but it returns null, Why ? 我试图通过RowDataBound从gridview获取链接按钮,但是它返回null,为什么? The name is correct.
这个名字是正确的。 Even you can see in the code but still it doesn't work.
即使您可以在代码中看到,但仍然无法正常工作。
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 的.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 is always null. lb始终为空。 Why ?
为什么呢
before putting anything on the rowdatabound event you have to use this condition 在将任何内容添加到rowdatabound事件之前,您必须使用此条件
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = e.Row.FindControl("btnShowContractors") as LinkButton;
ScriptManager.GetCurrent(this).RegisterPostBackControl(lb);
}
Try this way 试试这个
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = e.Row.FindControl("btnShowContractors") as LinkButton;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.