It's a known issue that asp.net FileUpload control doesn't work inside a UpdatePanel in a ajax enabled web page. There is a work around to solve this problem if you still need to put FileUpload control inside the UpdatePanel for page layout reasons. The work around is as follows:
In the asp.net page, define a FileUpload and a Button control inside ContentTemplate:
<atlas:UpdatePanel ID="multiViewUpdatePanel" runat="server" Mode="Conditional">
<ContentTemplate>
<asp:FileUpload ID="attachmentFile" runat="server" />
<asp:Button ID="uploadButton" runat="server" Text="Upload" />
</ContentTemplate>
</atlas:UpdatePanel>
Still in the asp.net page, put a "fake" link button outside of UpdatePanel as this:
<asp:LinkButton ID="fakeUploadButton" style="display:none" runat="server" Text="upload" OnClick="fakeUploadButton_Click" />
In the code behind file, put this code in the Page_Load event:
protected void Page_Load(object sender, EventArgs e)
{
this.uploadButton.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(fakeUploadButton, ""));
}
After this line of code is executed, when a user click on the uploadButton, which is located beside the FileUpload control, the "fake" link button's OnClick event will be triggered because of the JavaScript added to the client side onclick event of uploadButton.
Then in the server side catch the OnClick event of the link button as below:
protected void fakeUploadButton_Click(object sender, EventArgs e)
{
if (attachmentFile.HasFile)
{
string fileName = attachmentFile.FileName;
// ...
}
}
Then when you click on the Upload button, an explict postback is triggerred which will post the file to the server side.
Another note, if you have a control inside UpdatePanel and you write file down load code directly in your asp.net code behind, the file won't be downloaded to client side. What you could do is to change the LinkButton to a HyperLink, which points to another WebForm, with whatever parameter passed with URL. In the second WebForm, you could write code to down load a file because that page has no UpdatePanel. And it works.
Saturday, 16 September 2006
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment