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