Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to FindControls in repeater control?

I want to enable or disable 'ParticipateBtn' depending on EventStartDate. I am getting this error:Object reference not set to an instance of an object.

Start Date : <%# CheckEnability((DateTime)Eval("Event_Start_Date")) %>

        <asp:Button runat="server" 
            ID="ParticipateBtn" 
            CommandName="Participate" 
            CommandArgument='<%# Eval("Event_Id") + "|" + Eval("Event_Name") + "|" + Eval("Volume") + "|" + Eval("Tournament_Id") %>' 
            Text="Participate" />&nbsp;&nbsp;

    </ItemTemplate>

    <FooterTemplate></FooterTemplate>

    <SeparatorTemplate>
        <hr style="color:Silver; height:1px;" />
    </SeparatorTemplate>

</asp:Repeater>

The code behind...

//Code behind
protected  string CheckEnability(DateTime eventstartdate)
{

    if (eventstartdate.Date < DateTime.Now.Date)
    {
        Button btn = (Button)Repeater1.FindControl("ParticipateBtn");
        btn.Enabled = false;              
    }           
    return eventstartdate.ToString("yyyy-MM-dd");
}
like image 904
Jignesh Avatar asked Mar 01 '23 01:03

Jignesh


1 Answers

Controls don't exist in the repeater until it has been databound, and then each control in the ItemTemplate exists once per item - so if you bind to a source with 3 items, there will be 3 ParticipateBtns. You need to know which one you want before you can find it. Once you do, you can get it like so:

myRepeater.Items[1].FindControl("ParticipateBtn");
like image 77
Rex M Avatar answered Mar 10 '23 22:03

Rex M