1
votes

I have a web application. On my page ive got this:

<div id="myDiv" runat="server" />
//and a button that will call a method to save my inputs

on code behind i have this :

    protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    if (!this.IsPostBack)
    {
        .....
    }
    LoadForm(); // this will create a textbox with values inside it and a button called change
                   // into the div .
                   //when i click the button , it will create a fileupload control
                  //and add this control into the div
}

my problem is: when i first click the button change it will change the textbox and the button to an upload control, no problem with this one, but when i click the save button, on load the page will re-pass from the LoadForm() letting my page create a textbox, while i had changed it to an upload file, how can i solve this???
I dont know if i should add more details, or its clear, thanks in advance

private void LoadForm()
        {
            ....
            button.Click += new EventHandler(button_Click);
            this.myDiv.Controls.Add(textBox);
            this.myDiv.Controls.Add(button);
    }
void button_Click(object sender, EventArgs e)
        {
            ...
            this.myDiv.Controls.Clear();
            this.myDiv.Controls.Add(here will be the fileupload control);
    }
2
Can you post the code for your Page_Load event and button click event? Without enough code, I can only speculate, but it looks like your LoadForm will probably need to be within the !this.IsPostBack block. - fizch
i cant put my loadform inside the !ispostback becoz im creating my button dynamically and im adding an eventhandler on the button,if i dont pass from the loadform it will not see the event on click becoz it will not re-create the button. I will edit my question with some codes - Grace

2 Answers

2
votes

The problem is that because you are creating the file upload dynamically, for it to show up again, it has to be re-added to the page. An option here would be to always have the control on the page, just toggle the visibility.

0
votes

Put your LoadForm method in the if(!isPostback) condition as you don't want to create the textbox and button again while you click on Save button. Also you can access the dyncamically created fileUpload using the Request.Forms collections