Use FileUpload to upload file code on UpdatePanel

Source: Internet
Author: User

Use FileUpload to upload file code on UpdatePanel

Use fileupload to upload file code 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> upload team leaders and associated objects </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">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<Script>
Window. top. callback (filename );
</Script>

</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: fileupload id = "fileupload1" runat = "server"/>
<Asp: button id = "button1" runat = "server" text = "OK" onclick = "button#click"/>
</Div>

</Form>
</Body>
</Html>

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.html controls;

Public partial class attachment: system. web. ui. page
{
Protected void page_load (object sender, eventargs e)
{

}

 


Protected void button#click (object sender, eventargs e)
{
If (fileupload1.hasfile = false)
{
Scriptmanager. registerstartups Tutorial example (this. page, this. gettype (), "alert", "alert ('upload file unspecified ');", 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 2

<Asp: updatepanel id = "updatepanel1" runat = "server">
<Contenttemplate>
<Asp: fileupload id = "fileupload1" runat = "server"/>
<Asp: button id = "button1" runat = "server" text = "Upload" onclick = "button#click"/>
</Contenttemplate>
<Triggers>
<Asp: postbacktrigger controlid = "button1"/>
</Triggers>
</Asp: updatepanel>


If you want to do some tricks on this updatepanel, such as adding an asp: panel, you can trigger hidden or displayed events by Using Button events, you will find that fileupload1 cannot find the file...

In fact, the truth is very simple. The content in updatepanel is filled in real-time through xmlhttp. Before you show it, It is blank in the page source code. It's okay to update normal data with a dynamic control, but it won't work to upload files. My solution is to replace asp: panel with a common div, and write two functions to dynamically send control scripts, you only need to call this function in the button event:

<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 );
}

 

Related Article

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.