在回发时丢失动态创建的上传文件

时间:2011-07-06 13:18:03

标签: c# asp.net postback

我有一个网络应用程序。   在我的页面上我得到了这个:

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

在代码背后我有这个:

    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
}

我的问题是: 当我第一次点击按钮更改它会将文本框和按钮更改为上传控件,没有问题,但是当我单击保存按钮时,加载页面将从LoadForm()重新传递让我的页面创建一个文本框,当我把它改成上传文件时,我该如何解决呢? 我不知道我是否应该添加更多细节,或者清楚, 提前谢谢

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 个答案:

答案 0 :(得分:2)

问题在于,因为您正在动态创建文件上载,要使其再次显示,必须将其重新添加到页面中。这里的一个选项是始终在页面上控制,只需切换可见性。

答案 1 :(得分:0)

将LoadForm方法放在if(!isPostback)条件中,因为您不想在单击“保存”按钮时再次创建文本框和按钮。 您还可以使用Request.Forms集合访问dyncamically创建的fileUpload