C# 从RowDatBound方法访问Gridview中的UserControl
我有一个gridview,在item模板中有一个usercontrol,它在回发时丢失了它的值。我希望能够从c代码隐藏中的RowDataBound方法访问控件,并重新分配CandidateID值 GridView控件C# 从RowDatBound方法访问Gridview中的UserControl,c#,asp.net,gridview,user-controls,rowdatabound,C#,Asp.net,Gridview,User Controls,Rowdatabound,我有一个gridview,在item模板中有一个usercontrol,它在回发时丢失了它的值。我希望能够从c代码隐藏中的RowDataBound方法访问控件,并重新分配CandidateID值 GridView控件 <asp:TemplateField> <ItemTemplate> <Controls:LikeButton ID="CandidateLikeButton" runat="server" LikeType="Candida
<asp:TemplateField>
<ItemTemplate>
<Controls:LikeButton ID="CandidateLikeButton" runat="server" LikeType="Candidate"
LikedObjectID='<%# Bind("CandidateID") %>' />
</ItemTemplate>
</asp:TemplateField>
如何执行此操作?您可以像访问任何其他控件一样使用FindControl访问它,并将其强制转换回其类型
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LikeButton LB = e.Row.FindControl("CandidateLikeButton") as LikeButton;
LB.CandidateID = 1234;
}
}