简体   繁体   中英

Problems when assigning values via JavaScript to Asp.net labels and then passing via C# session object

I am assigning variables to asp labels via javascript with a simple innerHTML call

example:

document.getElementById('labelName').innerHTML = parseFloat(var).toFixed(2);


It appears in the label fine, and I am able to continue to manipulate it via javascript.

I then set it up so that that variable is put into a session object via C# codebehind buttonClick event.

example:

protected void btnConfirm_Click ( object sender, EventArgs e )
{
    Session["sessionName"] = labelName.Text;
}


The buttonConfirm_Click method fires it Response.Redirects to another page and populates asp labels with the session object via the c# codebehind page_load method.

example:

lblResult.Text = Session["sessionName"].ToString();


When doing this, the label is empty, no errors or 'null'. I have tried to narrow down the issue by trying various things. When I assign the text explicitly in the c# code behind of the first page and the recieve and assign it to the label on the next page, it shows correctly.

example:

Page 1:
Session["sessionName"].ToString() = "Test";

Page 2:
lblResult.Test = Session["sessionResult"].ToString();


I have tried several other things, such as casting the variables in javascript and in the codebehind, and checking to make sure I had runat="server" within each applicable label.

Anyways, is there something here I am missing? Is asp.net unable to detect the changes that javascript has made to the labels? Are there some incompatibility issues when using innerHTML or anything like this that maybe be causing such a thing to occur?

Thanks in advance!

The problem is that the text in a span tag (that is what asp:Label will render) isn't sent in the post to the server and therefore you can't read your changes server side. You'll need to use a input element (hidden field, textbox etc depending on what your ui should look like).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM