Updatepanel-based Brushless new upload (user control) in ASP. NET (C #)

Source: Internet
Author: User

Note: This method does not allow you to upload objects without refreshing. It only keeps the position of the scroll bar unchanged after the file is uploaded, which is equivalent to refreshing the file.

1. Create a user control up. ascx

2. Add updatepanel to the user control. The Code is as follows:

<Asp: updatepanel id = "updatepanel1" runat = "server" rendermode = "Block"Updatemode = "Conditional">
<Contenttemplate>
<Asp: fileupload id = "fileup" runat = "server" Height = "24px"/>
<Asp: button id = "uping" runat = "server" text = "Upload" width = "73px" Height = "24px"/>
<Div id = "MSG" runat = "server"> waiting for upload... </div>
</Contenttemplate>
<Triggers>
<Asp:Postbacktrigger controlid = "uping"/>
</Triggers>
</ASP: updatepanel>

3. Add the following code to up. ascx. CS:

Public partial class up: system. Web. UI. usercontrol
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
// Msg. clientid is very important here. If it is referenced directly, the msg id on the parent page may look like this up1_msg, where up1 is the ID of the control referenced in the parent page
// If JS needs to be correctly parsed in the browser, the escape character \ must be considered \
String JS = "javascript: document. getelementbyid ('"+ MSG. clientid + "'). innerhtml = 'uploading... (you can place an animated image or IFRAME here to call other pages (aspx) to check the upload progress and handle upload events )';";
Uping. Attributes. Add ("onclick", JS );
}
}
Public event eventhandler onclick
{
Add
{
Uping. Click + = value;
// Uping. Text = "event loaded" + datetime. Now. tostring () + value. method. tostring ();
}
Remove
{
Uping. Click-= value;
// Uping. Text = "event uninstalled" + datetime. Now. tostring ();
}
}

/// <Summary>
/// Message
/// </Summary>
Public String msg
{
Get
{
// Obtain
Return msg. innerhtml. tostring ();
}
Set
{
// Set
MSG. innerhtml = value;
}
}

/// <Summary>
/// Fileupload (publish component)
/// </Summary>
Public fileupload
{
Get
{
// Obtain
Return fileup;
}
Set
{
// Set
Fileup = value;
}
}
}

4. Create a webpage (note the red code)

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "brushless upload. aspx. cs" inherits ="Mylove"Maintainscrollpositiononpostback = "true"DEBUG = "true" %>
<% @ Register src ="Up. ascx"Tagname =" up "tagprefix =" uc1 "%>

<! 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> resumable upload </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: scriptmanager id = "scriptmanager1" runat = "server">
</ASP: scriptmanager>

<Div style = "height: 500px; Background: # ccffcc;"> I want to see if the uploaded data has not changed after it is submitted. </div>

<Uc1: Up id = "up1" runat = "server"/>

<Asp: updatepanel id = "updatepanel1" runat = "server" rendermode = "Block" updatemode = "Conditional">
<Contenttemplate>
<Uc1: Up id = "up2" runat = "server"/>
</Contenttemplate>

<Triggers>
<Asp:Postbacktrigger controlid = "up2"/>
</Triggers>
</ASP: updatepanel>
</Div>
</Form>
</Body>
</Html>

5. Background code

Public partial classMylove: System. Web. UI. Page
{

Protected void page_load (Object sender, eventargs E)
{
Up1.onclick + = new eventhandler (updomainuping_click); // register an action (event, use a proxy)
Up2.onclick + = new eventhandler (up2_uping_click );
}
/// <Summary>
/// Upload and save Event 1 (usually used by buttons)
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>

Protected void up1_uping_click (Object sender, eventargs E)
{
If (up1.fileupload. hasfile)
{
Long L1 = up1.fileupload. filecontent. Length/1024; // byte stream
Int L2 = up1.fileupload. postedfile. contentlength/1024; // bytes length

String filename = up1.fileupload. filename;

Up1.msg = "instantiated as: up1" + datetime. now. tostring () + "<br/> the length of the uploaded byte stream is:" + l1.tostring () + "kbit" + "<br/> the size of the uploaded file is (kilobytes ):"
+ L2.tostring () + "kb .";

// Save
Up1.fileupload. saveas (server. mappath (@"~ /Temp/") + filename );

}
Else
{
Up1.msg = "don't fool me, no file! "+ Datetime. Now. tostring ();
}

}

/// <Summary>
/// Upload and save event 2
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void up2_uping_click (Object sender, eventargs E)
{

If (up2.fileupload. hasfile)
{
String filename = up2.fileupload. filename;
Up2.msg = "instantiated as: up2" + datetime. Now. tostring () + "<br/> the uploaded file name is:" + filename;

// Save
Up2.fileupload. postedfile. saveas (server. mappath (@"~ /Temp/") + filename );
}
Else
{
Up2.msg = "you just have to worry about it ~ "+ Datetime. Now. tostring ();
}
}
}

 

If a potentially dangerous request. form value is detected. Add this sentence to the. aspx file header: <% @ page validaterequest = "false" %>

Reference URL: I modified it based on the content of this URL. Thank you for the author.

Http://apps.hi.baidu.com/share/detail/15914216

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.