Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET textbox losing value after postback when value is changed client-side via javascript
    text
    copied!<p>I am setting the value of a textbox via javascript client-side. When I refresh the page the value is lost.</p> <p>The textbox is not disabled or set to read-only.</p> <p>I have read suggestions to put the value in a hidden field, I'd prefer not to do this. It seems like a hack.</p> <p>The textbox is in a user control as such:</p> <pre><code>&lt;div id="Row" class="RaidRow" runat="server"&gt; &lt;asp:Button ID="RaidRowButton" runat="server" CssClass="RaidRowButton" OnClick="RaidRowButton_Click" /&gt; &lt;asp:TextBox ID="CharacterNameTextBox" runat="server" CssClass="RaidRowControl SlotTextBox" MaxLength="12" /&gt; &lt;asp:Label ID="ClassNameLabel" runat="server" CssClass="RaidRowControl ClassLabel" /&gt; &lt;/div&gt; </code></pre> <p>This control is on the .aspx page.</p> <p>When the user double-clicks the textbox, presses enter while the textbox has focus, or changes the text, the following javascript function is called:</p> <pre><code>function VerifyCharacter(slotNumber, event) { var enterWasPressed = false; if (event &amp;&amp; event.which == 13) { enterWasPressed = true; } else if (window.event &amp;&amp; window.event.keyCode == 13) { enterWasPressed = true; } if (event == null || enterWasPressed) { var realmName = 'Lightninghoof'; var characterNameTextBox = GetCharacterNameTextBox(slotNumber); characterNameTextBox.style.color = '#CCCCCC'; var classLabel = GetClassNameLabel(slotNumber); classLabel.style.color = '#CCCCCC'; classLabel.innerHTML = 'Unknown'; WoW_Stats.CharacterInformation.GetCharacterInformation(realmName, characterNameTextBox.value, slotNumber, GetCharacterInformation_Success, GetCharacterInformation_Failed); } } </code></pre> <p>Once the web-service call is complete, the following javascript function is called:</p> <pre><code>function GetCharacterInformation_Success(characterInformationResult) { var characterInfo = Sys.Serialization.JavaScriptSerializer.deserialize(characterInformationResult, true); var classLabel = GetClassNameLabel(characterInfo.SlotNumber); classLabel.style.color = characterInfo.ClassColor; classLabel.innerHTML = characterInfo.ClassName; var characterNameTextBox = GetCharacterNameTextBox(characterInfo.SlotNumber); characterNameTextBox.style.color = characterInfo.ClassColor; } </code></pre> <p>The only problem is that when I refresh the page, the text in the CharacterNameTextBox and ClassNameLabel, which were set client-side via javascript, are lost. </p> <p>How can I make sure the controls retain their value between postbacks?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload