当值通过JavaScript修改客户端回发后ASP.NET文本框失去价值 [英] ASP.NET textbox losing value after postback when value is changed client-side via javascript
问题描述
我是通过javascript客户端设置一个文本框的值。当我刷新页面的价值会丢失。
I am setting the value of a textbox via javascript client-side. When I refresh the page the value is lost.
文本框未禁用或者设置为只读。
The textbox is not disabled or set to read-only.
我已阅读建议把价值在一个隐藏字段,我想preFER没有做到这一点。这似乎是一个黑客。
I have read suggestions to put the value in a hidden field, I'd prefer not to do this. It seems like a hack.
文本框在用户控件这样:
The textbox is in a user control as such:
<div id="Row" class="RaidRow" runat="server">
<asp:Button ID="RaidRowButton" runat="server" CssClass="RaidRowButton" OnClick="RaidRowButton_Click" />
<asp:TextBox ID="CharacterNameTextBox" runat="server" CssClass="RaidRowControl SlotTextBox" MaxLength="12" />
<asp:Label ID="ClassNameLabel" runat="server" CssClass="RaidRowControl ClassLabel" />
</div>
这是控制在.aspx页面上。
This control is on the .aspx page.
当用户双击文本框,presses进入,而文本框具有焦点,或更改文本,JavaScript函数以下称为:
When the user double-clicks the textbox, presses enter while the textbox has focus, or changes the text, the following javascript function is called:
function VerifyCharacter(slotNumber, event)
{
var enterWasPressed = false;
if (event && event.which == 13)
{
enterWasPressed = true;
}
else if (window.event && 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);
}
}
一旦Web服务调用完成后,下面的JavaScript函数被调用:
Once the web-service call is complete, the following javascript function is called:
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;
}
唯一的问题是,当我刷新页面,在CharacterNameTextBox和ClassNameLabel文本,这是通过JavaScript设置客户端,都将丢失。
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.
我怎样才能确保控件保留回发之间的价值?
How can I make sure the controls retain their value between postbacks?
推荐答案
刷新页面(pressing F5)比回传不同的。我不相信你的文本框中的值将被调回上刷新服务器。在文本框中的值将得到回发到上发的服务器虽然(只要文本框的只读属性未设置为true,但是这是一个不同的故事)。
Refreshing the page (pressing F5) is different than a postback. I don't believe the value in your text box is going to be posted back to the server on a refresh. The value in the text box will get posted back to the server on a postback though (as long as the ReadOnly attribute of the TextBox is not set to true, but that's a different story).
这篇关于当值通过JavaScript修改客户端回发后ASP.NET文本框失去价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!