Repeater

M

MattB

I'm trying to build a repeater to generate text boxes based on a value in a
table. The number of boxes will vary so the repeater seemed like the logical
choice. I want to make the id property of each text box correspond to a
value in the datatable. I have the following which does not work (compile
errors). Can anyone tell me if this can be done, and if so what am I doing
wrong?

<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "Descrip")%>:
<asp:textbox id="<%#DataBinder.Eval(Container.DataItem, "Field")%>"
tabIndex="1" runat="server" Width="100px" MaxLength="30"></asp:textbox>
<br>
</ItemTemplate>
</asp:Repeater>

If I set a static id property(id="x"), it will compile and render the page.
Unfortunately that won't work for me. Any suggestions? TIA!

Mat
 
J

Jos

MattB said:
I'm trying to build a repeater to generate text boxes based on a value in a
table. The number of boxes will vary so the repeater seemed like the logical
choice. I want to make the id property of each text box correspond to a
value in the datatable. I have the following which does not work (compile
errors). Can anyone tell me if this can be done, and if so what am I doing
wrong?

<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "Descrip")%>:
<asp:textbox id="<%#DataBinder.Eval(Container.DataItem, "Field")%>"
tabIndex="1" runat="server" Width="100px" MaxLength="30"></asp:textbox>
<br>
</ItemTemplate>
</asp:Repeater>

If I set a static id property(id="x"), it will compile and render the page.
Unfortunately that won't work for me. Any suggestions? TIA!

Mat

I guess the textbox is assigned an ID at creation time.
Therefore, you can not change it anymore at databinding time.

I suggest creating the textbox dynamically in the ItemDataBound
event handler of the repeater.

Something like this (VB):

Sub OnItemDataBound(sender As Object, e As RepeaterItemEventArgs)
Dim tb As New Textbox()
tb.ID = DataBinder.Eval(e.Item.DataItem,"Field")
e.Item.Controls.Add(tb)
End Sub

Also make sure that you re-create the textbox in the same way
after postback. Otherwise, postback will not work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top