Pergunta

I need to create the following functionality, and looking for the best way to store info on multiple postbacks:

Say I have a TextBox, that is related to a question, i.e. "Places I've visited". Once TextBox will be loaded on first load. Under that, there is a button to "Add another place". This will postback, which will then add another TextBox underneath the first. This can be done x number of times.

The question I have is, how do I store/remember the number of controls that have been added to the page by the user, considering that this load needs to be done in the Init event, and ViewState is not loaded at this point?

EDIT*:

I forgot to mention, that when the user saves, there would be some validation, so if validation fails, need to show all the TextBoxes, with their posted data.

Foi útil?

Solução 3

After some thinking, and considering the answers given, came up with this solution.

In the controls placeholder, have a hidden input field which will store the number of controls added to the page. Then, on Init, I can have the following code (testing only):

protected override void OnInit(EventArgs e)
{
    int i = 0;
    i = Int32.Parse(hdnTestCount.Value);

    if(Request.Params[hdnTestCount.UniqueID] != null)
    {
        i = Int32.Parse(Request.Params[hdnTestCount.UnitueID]);
    }

    for (int j = 1; j <= i; j++)
    {
         TextBox txtBox = new TextBox();
         txtBox.ID = "Test" + j.ToString();
         plhTest.Controls.Add(txtBox);
    }
}

protected void btnAdd_OnClick(object sender, EventArgs e)
{
    int i = 0;
    i = Int32.Parse(hdnTestCount.Value) + 1;

    TextBox txtBox = new TextBox();
    txtBox.ID = "Test" + i.ToString();
    plhTest.Controls.Add(txtBox);
    hdnTestCount.Value = i.ToString();
}

Of course the only issue with this, is that the value could be manipulated by the user in the hidden field. The only other option would be to use Session, which I do not want to use as it sticks around, whereby if the page is refreshed this way, the form will reset itself, which is what should happen.

Outras dicas

If you were going to only allow a finite number of textboxes on your form, you could create that number of textboxes during Page_Init and set their visibility to false so they would not be rendered in the browser. On the button's click event, you could find the first invisible textbox and change the visibility to true. Something like this

protected void Page_Init(object sender, EventArgs e)
{
    for (int i = 0; i < 20; i++)
    {
        this.Form.Controls.Add(new TextBox() { Visible = false });
    }
}

protected void addTextboxButton_Click(object sender, EventArgs e)
{
    TextBox tb = this.Form.Controls.OfType<TextBox>().FirstOrDefault(box => box.Visible == false);
    if (tb != null) tb.Visible = true;
}

Using this approach, the textboxes become visible one by one on each button click, and the postback values stick.

Obviously, you'd want to put some more work into it, such as perhaps definining some literal controls to create line breaks and prompts for the textboxes, as well as displaying a message when the user hit whatever finite limit you set.

Option 1 : You can use Session to store the number of Textboxes...

Option 2 : You can even add controls in the Load event of the page, wherein you will have the ViewState information.

Here are the links that could help you...

TRULY understanding Dynamic Controls

Truly Understanding ViewState

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top