How to handle variable number of textboxes in asp.net?
I'm developing an app that should allow the client to add several content sections to a page (in tabs). However I haven't found a way to easily create a form for editing the tabs.
Assume for now that a tab just contains a title. I have a form that adds a tab, that's simple. But I also want a form that displays a text input for each tab and allows the titles to be edited and saved. I'm struggling with two main concepts:
- How to dynamically display the fields after grabbing 开发者_运维问答the tab data from the database (each tab has its own row in the table).
- How to loop through the submitted data and update each tab in the database.
- How to select a particular text field from the number (e.g. given "1", how to select the element with ID "TabTitle_1".
Currently I am limiting it to five fields and using code like this:
1. <asp:TextBox ID="TabTitle_1" runat="server"></asp:TextBox><br>
....
5. <asp:TextBox ID="TabTitle_5" runat="server"></asp:TextBox>
With this on the server side to set up the tabs:
// numrows is the number of current tabs
If numrows > 0 Then
Me.TabTitle_1.Text = dtClientNotes.Rows(0).Item("title")
Me.TabTitle_1.Visible = True
End If
....
If numrows > 4 Then
Me.TabTitle_5.Text = dtClientNotes.Rows(4).Item("title")
Me.TabTitle_5.Visible = True
End If
With code like this to handle the form subnmission:
If numrows > 0 Then
Clients.EditTab(dtClientNotes.Rows(0).Item("id").ToString, Me.TabTitle_1.Text)
End If
I thought something like TabTitle[0]
would be acceptable and allow for easy looping (as it does in PHP) but it's "not a valid identifier" apparently.
It's very possible I have the approach completely on its head. I've used asp.net and VB quite a lot for simple forms, but I am more used to PHP. If anyone can provide some pointers I'd be very grateful!!
I would suggest using a repeater control. You can customize your layout however you wish (see examples provided via the 'repeater control' link). Accessing the data in the control is just as easy (itereating each item in the repeater, again have a look at the examples provided on msdn website).
精彩评论