简体   繁体   English

当文本框通过javascript更改客户端时,ASP.NET文本框在回发后丢失值

[英]ASP.NET textbox losing value after postback when value is changed client-side via javascript

I am setting the value of a textbox via javascript client-side. 我通过javascript客户端设置文本框的值。 When I refresh the page the value is lost. 当我刷新页面时,值会丢失。

The textbox is not disabled or set to read-only. 文本框未禁用或设置为只读。

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>

This control is on the .aspx page. 此控件位于.aspx页面上。

When the user double-clicks the textbox, presses enter while the textbox has focus, or changes the text, the following javascript function is called: 当用户双击文本框时,在文本框具有焦点时按Enter键,或者更改文本,将调用以下javascript函数:

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

Once the web-service call is complete, the following javascript function is called: 完成Web服务调用后,将调用以下javascript函数:

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

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. 唯一的问题是,当我刷新页面时,通过javascript设置客户端的CharacterNameTextBox和ClassNameLabel中的文本将丢失。

How can I make sure the controls retain their value between postbacks? 如何确保控件在回发之间保留其值?

Refreshing the page (pressing F5) is different than a postback. 刷新页面(按F5)与回发不同。 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). 文本框中的值将在回发时回发到服务器(只要TextBox的ReadOnly属性未设置为true,但这是一个不同的故事)。

I think if the textbox is not editable : Enabled = False or ReadOnly= false. 我认为如果文本框不可编辑:Enabled = False或ReadOnly = false。
Asp.net "for security reasones" takes the value from this textbox from its ViewState. Asp.net“出于安全原因”从其ViewState获取此文本框中的值。
That's why when you use something like a HiddenField or Input, you can get the value. 这就是为什么当你使用像HiddenField或Input这样的东西时,你可以获得价值。

In the page preinit event, try accessing the value like this: 在页面preinit事件中,尝试访问如下所示的值:

Request["CharacterNameTextBox"] 请求[ “CharacterNameTextBox”]

I was running into the same issue (a value set via javascript would never post back, but a typed in value would) and a co-worker suggested it. 我遇到了同样的问题(通过javascript设置的值永远不会回发,但输入的值会有)并且同事建议它。 Dead on, works like a charm. 死了,就像一个魅力。

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. 唯一的问题是,当我刷新页面时, 通过javascript设置客户端的CharacterNameTextBox和ClassNameLabel中的文本将丢失。

Since you are setting the value client-side I would suggest using cookies to store that data and checking for that cookie in your client-side, since ViewState will not help here if you are just setting the value client-side regardless of what CodeBehind would set it to be. 由于您设置了客户端的值,我建议使用cookie来存储该数据并在客户端检查该cookie,因为如果您只是设置客户端的值而不管CodeBehind会是什么,ViewState将无法帮助您把它设置为。

Another solution is Session, you can assign a session in the first load or from external page, then check if session is not null, then set value to text box. 另一种解决方案是Session,您可以在第一次加载或外部页面中分配会话,然后检查会话是否为空,然后将值设置为文本框。 Here is example: in external page: 以下是示例:在外部页面中:

   if (id == "txtDD")
        {
          Session["DD"] = calendar.SelectedDate.ToShortDateString();
        }
and in code behind check it:

protected void Page_Load(object sender, EventArgs e)
   {
    if(Session["DD"]!= null)
       txtDD.Text =Session["DD"].ToString();
   }

Instead of making a text box readonly you can try this 您可以尝试这样做,而不是只读文本框

onKeyPress = "javascript: return false;" onPaste = "javascript: return false;" 

it will not loose its value. 它不会失去它的价值。

Storing values in a hidden field is no more of a hack than .NET Web Forms itself is. 将值存储在隐藏字段中并不比.NET Web Forms本身更糟糕。 Much of the application data is passed/stored in the hidden __VIEWSTATE field. 大多数应用程序数据都传递/存储在隐藏的__VIEWSTATE字段中。

That being said, could you clarify what is going on? 话虽这么说,你能澄清一下发生了什么吗? Are you setting the value on page load, or in reaction to some user action? 您是在页面加载时设置值还是对某些用户操作做出反应? Could you provide some of your code so that we can have a clearer picture of what you are doing? 你能提供一些代码,以便我们能够更清楚地了解你在做什么吗?

EDIT 编辑

Are you explicitly setting the value of either of the textbox or the label in your page load/postback codebehind? 您是否在页面加载/回发代码隐藏中明确设置文本框或标签的值? That would override any values being set by the client script. 这将覆盖客户端脚本设置的任何值。 I can't think of any other reason why the values would not persist. 我想不出为什么这些价值观不会持续存在的任何其他原因。 If you're really desperate, you could send the information back to the server via an AJAX call at the end of your javascript function, so that you have it in server memory somewhere. 如果你真的很绝望,你可以在你的javascript函数结束时通过AJAX调用将信息发送回服务器,这样你就可以把它放在服务器内存的某个地方了。

when do you set the value? 你什么时候设定价值? You could set the value onload of the page in javascript as long as you are using the clientId of the textbox. 只要您使用文本框的clientId,就可以在javascript中设置页面的值onload。

The problem could be one of two things. 问题可能是两件事之一。 The refresh could clear then field. 刷新可以清除然后字段。 Also, if you are posting back, do you set the value of the text box on the page load (or unload) event? 此外,如果您要回发,是否在页面加载(或卸载)事件上设置文本框的值?

You have to remember, KISS. 你必须记住,亲吻。

Just think of your existing fields as presentational in nature and add hidden input fields with a runat="server" attribute and set those with JavaScript in your same client-side event. 只需将现有字段视为表现性字段,并使用runat =“server”属性添加隐藏的输入字段,并在同一客户端事件中使用JavaScript设置这些字段。

Those are your real values you want in your code-behind methods and you'll be able to grab those no problem. 这些是您在代码隐藏方法中想要的真正价值,并且您将能够抓住那些没问题。 Since the textbox object is a .NET abstraction, I would suggest when doing heaving client-side work using HTML inputs that are run at the server. 由于textbox对象是.NET抽象,我建议在使用在服务器上运行的HTML输入进行客户端工作时。 It seems to save me a headache when I'm knee-deep in web forms BS. 当我在网络形式BS中膝盖深处时,这似乎让我头疼。

I think there are a lot of solutions here, but I'm all for breaking the wrist and walking away if I can. 我认为这里有很多解决方案,但如果可以的话,我就是为了打破手腕并走开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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