tags:

views:

46

answers:

2

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:

  1. How to dynamically display the fields after grabbing the tab data from the database (each tab has its own row in the table).
  2. How to loop through the submitted data and update each tab in the database.
  3. 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!!

+1  A: 

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).

rickp
A: 

On your front-end add a placeholder.

<asp:PlaceHolder runat="server" id="LabelPlaceHolder" />

In your code-behind, iterate through the number of TextBoxes you want and add them to the page.

TextBox[] txtTextBoxes = new TextBox[NumberOfBoxes];

for (int i = 0; i < NumberOfBoxes; i++) {
   txtTextBoxes[i].ID = "TextBox_" + i.toString();
   LabelPlaceHolder.Controls.Add(txtTextBoxes[i]);
}

I didn't check to see if that worked, but it should.

I just realized you may've been talking about adding them separately in each tab. In that case, just add a new PlaceHolder in each of the tabs you're adding. If you have any more questions, let me know.

treefrog
What about putting the textboxes in a table, or laying them out some other way? Is there a good way to do that?
DisgruntledGoat
You can't add CSS to a PlaceHolder, unfortunately. You can do this with a Panel though, just replace all instances of PlaceHolder above with Panel and the functionality should be the same.As far as using a table, I can't think of a way to do it without using a repeater. like rick said.
treefrog
I thought of a way to add the textboxes. It's not elegant, but you could dynamically add Literals into the PlaceHolder with your table code.
treefrog