Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从RowDatBound方法访问Gridview中的UserControl_C#_Asp.net_Gridview_User Controls_Rowdatabound - Fatal编程技术网

C# 从RowDatBound方法访问Gridview中的UserControl

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

我有一个gridview,在item模板中有一个usercontrol,它在回发时丢失了它的值。我希望能够从c代码隐藏中的RowDataBound方法访问控件,并重新分配CandidateID值

GridView控件

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