How to FindControls in repeater control?

浪子不回头ぞ 提交于 2019-11-29 08:41:40

You can toggle the enabled property of the button control using declarative syntax and display and format the Event_Start_Date using the Eval methods format parameter. This way there is no need for the CheckEnability method.

<asp:Repeater>
    <table>
         <tr>
            <td align="right" style=" font-weight:bold">Start Date : </td>
            <td><%# Eval("Event_Start_Date", "{0:yyyy-MM-dd}")%></td>
        </tr>
         <asp:Button ID="ParticipateBtn" Enabled='<%# Convert.ToDateTime(Eval("Event_Start_Date") ) < DateTime.Now %>' CommandName="Participate" CommandArgument='<%# Eval("Event_Id") + "|" + Eval("Event_Name") + "|" + Eval("Volume") + "|" + Eval("Tournament_Id") %>' runat="server" Text="Participate" />&nbsp;&nbsp;
    </ItemTemplate>
    <FooterTemplate></FooterTemplate>
    <SeparatorTemplate><hr style="color:Silver; height:1px;" /></SeparatorTemplate>
</asp:Repeater>

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");
Brett Allen

Add the attribute OnItemDataBound="repeater_ItemDataBound" to your repeater.

Then in your code behind do:

void repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
{
    if (((DateTime)e.Item.DataItem).Date < DateTime.Now.Date)
    {
        Button participate = (Button)e.Item.FindControl("ParticipateBtn");
        participate.Enabled = false;              
    }      
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!