views:

106

answers:

1

I'm writing some form. I have one field in this form that contains a number. If this number for example is 3, i want this form to enable 9 new fields so i can add the values for those 3 new participants.

            <li>
                <asp:Label ID="lblAantaldeelnemendeploegen" runat="server" Text="Label">Aantal deelnemende ploegen: </asp:Label>
                <asp:TextBox ID="tbAantaldeelnemendeploegen" AutoPostBack="true" runat="server"></asp:TextBox>
            </li>

This is the Label / textbox i'm posting back to see how many new fields i have to enable for the user. My codebehind now has this code to check for a postback:

 if (Page.IsPostBack)
    {
        Request.Form
    }

How can I now add for example 3 fields to this form? What kind of control can i best use for this? Or how do i dynamicly just add those new fields? I'm new to this so that's why i ask alot.

+1  A: 

uh ... i hope you are not a novice to asp.net. because otherwise it may end up in a horror!

reasons: if you dynamically create controls, they should be present, after the first postBack, each postBack before AttachPostData SecondTry. you have to give the controls the same ids after postBack again - otherwise viewState/postBackData won't get attached and it will end up in an exception.

so assume you all know this, here's some basic sceleton:

<asp:PlaceHolder runat="server" ID="phDynamicControls" />

and codeBehind

function /* add name and correct signature here - eventHandler for saveButton click-event */
{
    // TODO rebuild controls from other postBacks somewhere
    var control = new TextBox
    {
        ID = "myUniqueID"
    }
    this.phDynamicControls.Controls.Add(control);
}

extending it, to avoid pitfalls, do the following for your page!

protected override void LoadViewState(object savedState)
{
    base.LoadViewState(savedState);
    // this will only be executed if postback
    // recreate controls from former postback
}

protected override object SaveViewState()
{
    // TODO persist control hirarchie in here!! to allow restoring on next LoadViewState()
    return base.SaveViewState();
}

a very easy approach would be: why not use a repeater

<asp:Repeater runat="sever" ID="repDynamicControls">
    <ItemTemplate>
        <asp:TextBox runat="server" ID="txtCustom"/>
    </ItemTemplate>
</asp:Repeater>

your codebehind

function /* add name and correct signature here - eventHandler for saveButton click-event */
{
    // TODO find out number of items!
    var items = 3;
    this.repDynamicControls.DataSource = Enumerable.Range(0, items); // zero-offset here
    this.repDynamicControls.DataBind();
}

but you still got following problem: preserve repeater data (binding) also if no event of your generate new item-button is fired.

in the end you will end up with binding at least 2 times:

  • 1st on LoadViewState
  • 2nd (optional) on eventHandler
Andreas Niedermair
To be honest i don't really know how to work with this viewstate. I think i have to read about that first :). And no i'm not totally new to asp.net but this is the first time i'm trying smth like this :).
Younes