Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Ok, so this is my problem. I have a custom ASP control that I have put in a Repeater control. My control consists of a CheckBox, a TextBox, and a HiddenField. On the control I have a public method called Save. All I want to do is loop through the controls of the repeater and when it finds my control is Checked, it must call the Save method.
I have a public property for Checked that I can access, my problem is that when I loop through the controls like so:
C#
<pre>foreach (Control c in myrepeater.Controls)
     if (c.GetType() == typeof(MyControl)
     {
        MyControl mc = c as MyControl;
        if (mc.Checked)
            mc.Save();
     }

I have also checked c.GetType().BaseType and c.GetType().BaseType.BaseType, and neither help at all. Any help would be greatly appreciated!
Posted
Updated 12-Apr-11 8:13am
v2

Hi,

If your markup looks something like this where consider your custom control in the place of textbox then

XML
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" >
    <ItemTemplate>
        <asp:TextBox ID="Text1" runat="server" Text='<%# Eval("ProductName") %>' ></asp:TextBox>
        <asp:CheckBox ID="check1" runat="server" Checked='<%# Eval("Test") %>' />
    </ItemTemplate>
</asp:Repeater>


From code behind

C#
foreach(Control ctrl in Repeater1.Controls)
{
    TextBox txt;
    //Text1 here is the control id given in the markup
    if( ( txt= (TextBox)ctrl.FindControl("Text1"))!=null){
        Response.Write(txt.Text);
    }
}
 
Share this answer
 
Comments
DominicZA 12-Apr-11 15:08pm    
Works perfecly...
There is an article in project which might help. Link[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900