Updatepanel and postbacktrigger Application Instances

Source: Internet
Author: User

The purpose of this Code is to download files from the database and package the files into a zip package. There are some download options on the front-end page, consisting of a radiobuttonlist and textbox. Textbox is used to fill in the file name, radiobuttonlist is used to set the download option, you can choose to download the default image or thumbnail.

 

Considering that downloading a file requires bandwidth, if the file is large, such as 100 MB, it takes some time to calculate the download speed of KB based on the current normal ADSL, therefore, it is more practical to control the file size within a certain range.

 

Therefore, when you click the submit button, the size of the downloaded file is calculated first. If the size exceeds 10 MB, a dialog box is displayed, prompting you whether to continue the download. Similarly, when you change the download option, you also need to calculate the size of the downloaded file. I personally think this is not the best practice. If you do a good job, you should click the submit button to calculate the file size, which can save the cost, but due to my limited ability, currently, only the size of the downloaded file can be calculated when the radiobuttonlist option is changed.

 

The updatepane option is used to avoid page refresh because PostBack is performed when the radiobuttonlist option is changed. However, there is a problem because the submit button requires PostBack instead of asyncpostback.

 

I have referenced some materials, mainly about the use of trigger in updatepanel, and finally added such a code to solve the problem.

<Triggers>
<Asp: postbacktrigger controlid = "btndownloadpictures"/>
</Triggers>

This specifies that the submit button will trigger PostBack, even if the control is placed in updatepanel.

 

For more information about the use of updatepanel and trigger, see my previous two posts.

 

 

Front-end code:

<Asp: updatepanel runat = "server" id = "updatepanel1" updatemode = "Conditional">
<Contenttemplate>
<Table class = "table100" cellpadding = "0" cellspacing = "0">
<Tr>
<TD class = "labeltdstyle">
<Asp: Label id = "lbldownloadoptions" runat = "server" text = "download options:"> </ASP: Label>
</TD>
<TD>
<Asp: radiobuttonlist id = "rbtdownloadoptions" runat = "server" autopostback = "true" onselectedindexchanged = "rbtdownloadoptions_selectedindexchanged">
<Asp: listitem value = "true" selected = "true"> download default pictures. </ASP: listitem>
<Asp: listitem value = "false"> download thumbnail pictures. </ASP: listitem>
</ASP: radiobuttonlist>
</TD>
</Tr>
<Tr>
<TD class = "labeltdstyle">
<Asp: Label id = "lblzipfilename" runat = "server" text = "zip file name:"> </ASP: Label>
</TD>
<TD>
<Asp: textbox id = "txtzipfilename" runat = "server"> </ASP: textbox>
<Asp: requiredfieldvalidator id = "rfvzipfilename" runat = "server" errormessage = "zip file name is required ."
Validationgroup = "vgdownloadpictures" controltovalidate = "txtzipfilename"> * </ASP: requiredfieldvalidator>
<Asp: regularexpressionvalidator id = "revzipfilename" runat = "server" errormessage = "the ZIP file name can contain only letters, numbers, periods (.), hyphens (-), and underscores (_). <br/> 4-character minimum; 25-character maximum"
Controltovalidate = "txtzipfilename" validationgroup = "vgdownloadpictures" validationexpression = "^ [/W/-/.] {4, 25} $"> * </ASP: regularexpressionvalidator> </TD>
</Tr>
<Tr class = "tr10">
<TD class = "labeltdstyle">
& Nbsp;
</TD>
<TD>
<Asp: validationsummary id = "vs" runat = "server" validationgroup = "vgdownloadpictures"/>
</TD>
</Tr>
<Tr>
<TD colspan = "2" align = "right">
<Asp: button id = "btndownloadpictures" runat = "server" text = "Download" onclick = "btndownloadpictures_click" validationgroup = "vgdownloadpictures">
</ASP: button>
<Asp: button id = "btncancel" runat = "server" text = "cancel" onclick = "btncancel_click"> </ASP: button>
</TD>
</Tr>
<Tr>
<TD colspan = "2" align = "right">
<Asp: Label id = "lbldownloadconfirm" runat = "server" text = "are you sure to continue to download this ZIP file, it wocould be more than 10 megabytes? "> </ASP: Label>
<Asp: Label id = "lblnogiftpicturestodownload" runat = "server" text = "no gift pictures to download."> </ASP: Label>
</TD>
</Tr>
</Table>
</Contenttemplate>
<Triggers>
<Asp: postbacktrigger controlid = "btndownloadpictures"/>
</Triggers>
</ASP: updatepanel>

<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function downloadconfirm ()
{
If (typeof (page_validators )! = "Undefined ")
{
For (VAR I = 0; I <page_validators.length; I ++)
{
Validatorvalidate (page_validators [I]);
If (! Page_validators [I]. isvalid & page_validators [I]. validationgroup = 'vgdownloadpictures ')
{
Return true;
}
}
Return window. Confirm (document. getelementbyid ("<% = This. lbldownloadconfirm. clientid %>"). innerhtml );
}
}
</SCRIPT>

 

 

Background code:

Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
If (isgiftpicturesizeexceedmaximumlimitation ())
{
This. btndownloadpictures. Attributes. Add ("onclick", "javascript: If (! Downloadconfirm () return false ;");
}
}
}

Protected void rbtdownloadoptions_selectedindexchanged (Object sender, eventargs E)
{
If (isgiftpicturesizeexceedmaximumlimitation ())
{
This. btndownloadpictures. Attributes. Add ("onclick", "javascript: If (! Downloadconfirm () return false ;");
}
Else
{
This. btndownloadpictures. Attributes. Remove ("onclick ");
}
}

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.