Upload file code using FileUpload on UpdatePanel

Source: Internet
Author: User

Upload file code using FileUpload on UpdatePanel

@page language= "C #" masterpagefile= "~/evamanager.master" autoeventwireup= "true" codefile= "hv_excel.asp tutorial X.cs" inherits= "Hv_excel" title= "Untitled page"%>
<asp:content id= "Content1" contentplaceholderid= "ContentPlaceHolder1" runat= "Server" >

<script>
function callback (filename)
{
document.getElementById (' Attach1 '). Innerhtml=filename;
}
</script>

<fieldset>
<legend> leader and linkage object upload </legend>
<iframe id= "File" name= "file" src= "attachment.aspx" ></iframe>

<asp:textbox id= "Attach1" runat= "Server" ></asp:textbox>
</fieldset>

</asp:content>
Attachment.aspx


<%@ page language= "C #" autoeventwireup= "true" codefile= "Attachment.aspx.cs" inherits= "attachment"%>

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">

<title> Untitled Page </title>
<script>
Window.top.callback (filename);
</script>

<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:fileupload id= "FileUpload1" runat= "Server"/>
<asp:button id= "button1" runat= "Server" text= "OK" onclick= "Button1_Click"/>
</div>

</form>
</body>

Attachment.aspx.cs
using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.web.ui.webcontrols.webparts;
Using System.Web.UI.HtmlControls;

public partial class Attachment:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{

}


protected void Button1_Click (object sender, EventArgs e)
{
if (Fileupload1.hasfile = = False)
{
Scriptmanager.registerstartups Tutorial Cript (This.page, This.gettype (), "Alert", "alert (' Unspecified upload file ');", true);
Return
}
if (fileupload1.filename.substring fileupload1.filename.lastindexof ('. ') + 1, 3). ToString (). ToLower ()!= "XLS")
{
Scriptmanager.registerstartupscript (This.button1, This.gettype (), "Alert", "alert (' File format Error!") Please make sure to download excel! for this page '); ", true);
Return
}

String Savepath = Request.physicalapplicationpath + "\upfile\" + fileupload1.filename;

Fileupload1.saveas (Savepath);

}


}

Method Two

<asp:updatepanel id= "UpdatePanel1" runat= "Server" >
<contenttemplate>
<asp:fileupload id= "FileUpload1" runat= "Server"/>
<asp:button id= "button1" runat= "server" text= "upload" onclick= "Button1_Click"/>
</contenttemplate>
<triggers>
<asp:postbacktrigger controlid= "Button1"/>
</triggers>
</asp:updatepanel>


And if you want to do some tricks on this updatepanel, such as adding a asp:panel, can be triggered by button events hidden or displayed, you will find FileUpload1 can not find the file ...

In fact, the truth is very simple, updatepanel content is filled by XMLHTTP real-time, before you let him show, see the page source code inside is empty. A dynamic control update normal data no problem, but upload file is not, my solution is to use the ordinary div instead of Asp:panel, and wrote 2 functions to dynamically Send control script, button events in the call only the function can:

<div id= "Panel1" ></div>

private void Showpanel ()
{
String script = "document.getElementById (' Panel1 '). style.display= ';";
Scriptmanager.registerstartupscript (This.page, This.gettype (), "Showpanel", script, True);
}
private void Closepanel ()
{
String script = "document.getElementById (' Panel1 '). style.display= ' none ';";
Scriptmanager.registerstartupscript (This.page, This.gettype (), "Closepanel", script, True);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.