Checkbox inside a repeater control..

  • Thread starter Thread starter Thomas R
  • Start date Start date
T

Thomas R

Is is possible?

In VS.NET 2003, Binding data to the repeater is easy, but when I try to add
an ASP:checkbox to the .aspx page, the designer won't recognize it, hence
the code view doesn't allow me to access it. The funny thing is that it
compiles fine, and the checkboxes are there, I just can't write to them!

Is there a way around this problem?

My HTML is as follows:

<asp:Repeater ID="Overview" Runat="server" Visible="False">
<ItemTemplate>
<tr>
<td width="200">
<asp:CheckBox ID="chkModule" Enabled=True Runat="server"
Text='<%#DataBinder.Eval(Container, "DataItem.Module")%>'>
</asp:CheckBox>
</td>
<td>
Pris:
<%#DataBinder.Eval(Container, "DataItem.Price")%>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>

This compiles fine, but if I go to the code view and tries to write
"this.chkModule...." it isn't recognized!

Thanks for your help,

Thomas Regin.
 
Because the checkbox is contained within a repeater
control, it is now a child of the repeater control and you
cannot access it directly the way you are trying. (just
remember there could be any number of instances of this
checkbox, one for each record in your repeater control)

You need to access the checkbox in the repeaters
onDataBind event to set the value for it.

Eg.
Dim myCheckbox As CheckBox
myCheckBox = CType(e.Item.FindControl
("myCheckBox"),CheckBox)

this code would be typically placed in the databind event
and the variable 'e' would be an eventArgs parameter.

Hope this helps

-----Original Message-----
Is is possible?

In VS.NET 2003, Binding data to the repeater is easy, but when I try to add
an ASP:checkbox to the .aspx page, the designer won't recognize it, hence
the code view doesn't allow me to access it. The funny thing is that it
compiles fine, and the checkboxes are there, I just can't write to them!

Is there a way around this problem?

My HTML is as follows:

<asp:Repeater ID="Overview" Runat="server" Visible="False">
<ItemTemplate>
<tr>
<td width="200">
<asp:CheckBox ID="chkModule" Enabled=True Runat="server"
Text='<%#DataBinder.Eval(Container, "DataItem.Module")%>'>
</asp:CheckBox>
</td>
<td>
Pris:
<%#DataBinder.Eval
(Container, "DataItem.Price")%>
 
Rory,

Thank you for your answer!

The problem is, that I need/would like to access the checkboxes at design
time, and set their on/off status while binding the repeater control. How
would I do that using the eventhandler?

I am a C# developer, and this is what I have:

OverviewRepeater.DataBinding += new
Eventhandler(this.OverviewRepeater_DataBinding); //Just initializing the
eventhandler here.

//And here's the handler function called on every databind.
private void OverviewRepeater_DataBinding(object sender, EventArgs e)
{
//What should I do here? Add bool on/off checkbox argument to this
function like "bool isChecked"?
}

I am not sure that the above would work, hence I don't see how I can set the
state of a checkbox at design time. The only way is to iterate through the
checkboxes afterwards and set their state, but that is a really stupid,
inefficient approach if you ask me!

Thanks for your help again, I hope that you or someone else can help med
solve this! :)

Thomas Regin.
 
Back
Top