The value of the hidden field cannot be returned (PostBack) to the backend _ ax

Source: Internet
Author: User

Yes, you are not mistaken. It is indeed that the value of the hidden field has not been PostBack to the background, and I have not found any relevant content on Baidu and Google.

Looking for n times failed, I thought it was because of the relationship between the project type (class library type). Until one day, I accidentally thought it could be the problem of the script event onbeforeunload. After testing, the cause is indeed this.

[Summary]
Namevaluecollection is saved before the onbeforeunload event is executed. Therefore, changing the input value in the onbeforeunload event does not take effect and is reflected in namevaluecollection.
However, if the return onbeforeunload event is used, the changed value is reflected in namevaluecollection and PostBack to the background.

TestCode]
Front-end code:

<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Default. aspx. CS " Inherits = " _ Default "   %>

<! 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 Language = "JavaScript" Type = "Text/JavaScript" >
Function Changehiddenvalue ()
{
Document. getelementbyid ( " Hidax " ). Value = " Assigned value! " ;
Alert (document. getelementbyid ( " Hidax " ). Value );
// Add return to display the dialog box and return content when you exit the page.
// Return "hello ";
}
</ Script >
</ Head >
< Body Onbeforeunload = "Changehiddenvalue ();" >
<% -- <Body onbeforeunload="Return changehiddenvalue ();"> -- %>
< Form ID = "Form1" Runat = "Server" >
< Div >
< ASP: hiddenfield ID = "Hidax" Runat = "Server" Value = "Default Value"   />
< ASP: button ID = "Btnax" Runat = "Server" Text = "Click my PostBack"   />
<% -- < ASP: button ID = " Btntest " Runat = " Server " Onclientclick = " Changehiddenvalue (); " Text = " Click "My PostBack ". "   /> -- %>
</ Div >

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

background code: Public partial class _ default: system. web. UI. page
{< br> protected void page_load ( Object sender, eventargs e)
{< br> response. write (hidax. value);
}< BR >}

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.