简体   繁体   English

ASP.Net:回发后如何维护TextBox状态

[英]ASP.Net: How to maintain TextBox State after postback

I would like to know how to maintain the control state that has been modified in Javascript. 我想知道如何保持已在Javascript中修改的控件状态。
I have two TextBoxes, one DropDownList and a button (all Runat=Server) in C# ASP.net 2010 Express. 我在C#ASP.net 2010 Express中有两个TextBoxes,一个DropDownList和一个按钮(所有Runat = Server)。

First textbox is just accept whatever data user input. 第一个文本框只是接受任何数据用户输入。
Second textbox enable state will change based on DDL selected value. 第二个文本框启用状态将根据DDL选择的值而改变。
If ddl selected value is "-", second textbox will become Enabled = False. 如果ddl选择的值为“-”,则第二个文本框将变为Enabled = False。
If not "-", it will become Enabled = True. 如果不是“-”,它将变为Enabled = True。 This enable is done through Javascript. 此启用通过Javascript完成。


In my Page Load event, I have below code. 在我的页面加载事件中,我有以下代码。

if (!IsPostBack)
{
     txtKey2.Text = "";
     txtKey2.BackColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC");
     txtKey2.Enabled = false;
}


And in my aspx page, I have some javascript which will clear the textbox data and disable the textbox. 在我的aspx页面中,我有一些JavaScript可以清除文本框数据并禁用该文本框。

Here is for Second Textbox. 这是第二个文本框。

<asp:TextBox ID="txtKey2" runat="server" Width="425px" EnableViewState="False"></asp:TextBox>


And here is for DDL. 这是针对DDL的。

<asp:DropDownList ID="selKey1" runat="server" onchange="EnableSelkey(this.value,1)">
    <asp:ListItem Value="0">-</asp:ListItem>
    <asp:ListItem Value="1">AND</asp:ListItem>
    <asp:ListItem Value="2">OR</asp:ListItem>
</asp:DropDownList>

Here is the code for my Javascript. 这是我的Javascript代码。 (I have a plan to implement other textbox and ddl so in my code I have Else if condition). (我有一个计划来实现其他文本框和ddl,因此在我的代码中我有if条件)。

function EnableSelkey(val, strID) {
    var txtBox;
    if (strID == "1")
        txtBox = document.getElementById('<%= txtKey2.ClientID %>');
    else if (strID == "2")
        txtBox = document.getElementById('<%= txtKey3.ClientID %>');
    else
        txtBox = document.getElementById('<%= txtKey4.ClientID %>');

    if (val != 0) {
        txtBox.disabled = false;
        txtBox.style.backgroundColor = "White";
        txtBox.value = "";
        txtBox.select();
    }
    else {
        txtBox.disabled = true;
        txtBox.style.backgroundColor = "#CCCCCC";
        txtBox.value = "";
    }
}

I have nothing in button click event. 按钮单击事件中没有任何内容。

By using above all code, when I run the project, the page loads Ok. 通过使用以上所有代码,当我运行项目时,页面将加载好。 The second textbox enabled state is set to False (through Page_Load event). 第二个启用文本框的状态设置为False(通过Page_Load事件)。 So far Ok. 到目前为止,好的。

Then from my browser, I choose ddl value to other instead of "-", the textbox become enable because of javascript. 然后从我的浏览器中,将ddl值选择为other而不是“-”,由于JavaScript,文本框变为启用状态。 This is Ok. 还行吧。

I input the value and click on the button. 输入值,然后单击按钮。 Page PostBack happens here. 页面回发发生在这里。 Textbox is still enabled (because of EnableViewState = False for my textbox). 文本框仍处于启用状态(因为我的文本框的EnableViewState = False)。

I choose ddl value to "-", second textbox became disabled. 我将ddl值选择为“-”,第二个文本框被禁用。
Click on the button, page postback happen, but this time the textbox is still enabled. 单击该按钮,发生页面回发,但是这次文本框仍处于启用状态。 << This is the issue I'm trying to solve. <<这是我要解决的问题。 I change EnableViewState, ViewStateMode in different values but still the same. 我将EnableViewState,ViewStateMode更改为不同的值,但仍然相同。

Is there any solution for this one? 有什么解决方案吗?

Here is my test image URL. 这是我的测试图像网址。 State 1 , State 2 , State 3 状态1状态2状态3

Sorry for the long post. 抱歉,很长的帖子。

I have tried and found no solution beside using additional HiddenField control. 我尝试过,但没有发现使用其他HiddenField控件的解决方案。

I update the hidden field value when I update the status of my textbox in Javascript. 当我在Javascript中更新文本框的状态时,会更新隐藏字段值。 And on my Page Load event, I checked all the hidden field values and based on the hiddenfield values, I will disable/enable my textboxes which is for me not a good solutions. 在页面加载事件中,我检查了所有隐藏字段值,并基于隐藏字段值,将禁用/启用我的文本框,这对我来说不是一个好的解决方案。

Imagine I have 10 or 15 textboxes on my form, I need to have 10 or 15 hidden field just to maintain client side action result. 想象一下,我的表单上有10或15个文本框,我需要有10或15个隐藏字段才能保持客户端操作结果。

Currently, this is the only solution for me. 目前,这是我唯一的解决方案。

I'm not sure can this consider as 'Answer' so I haven't close this question yet. 我不确定这是否可以视为“答案”,所以我还没有结束这个问题。

<asp:DropDownList ID="selKey1" runat="server" onchange="EnableSelkey(this.value,1)">
    <asp:ListItem Value="0">-</asp:ListItem>
    <asp:ListItem Value="1">AND</asp:ListItem>
    <asp:ListItem Value="2">OR</asp:ListItem>
</asp:DropDownList>

function EnableSelkey(val, strID) {
    var txtBox;        
    if (strID == "1")
        txtBox = document.getElementById('<%= txtKey2.ClientID %>');
    else if (strID == "2")
        txtBox = document.getElementById('<%= txtKey3.ClientID %>');
    else
        txtBox = document.getElementById('<%= txtKey4.ClientID %>');

    if (val != 0) {
        txtBox.disabled = false;
        txtBox.style.backgroundColor = "White";
        txtBox.value = "";
        txtBox.select();
    }
    else {
        txtBox.disabled = true;
        txtBox.style.backgroundColor = "#CCCCCC";
        txtBox.value = "";
    }
}

You Have to call you javascript function on every postback. 您必须在每次回发中都调用javascript函数。

if (!IsPostBack)
{
     txtKey2.Text = "";
     txtKey2.BackColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC");
     txtKey2.Enabled = false;
}

ScriptManager.RegisterClientScriptBlock(this, typeof(System.Web.UI.Page), "MyJSFunction", "EnableSelkey("+selKey1.SelectedValue+",1);", true);

It May be help you, Let me know for further help. 可能会对您有所帮助,请让我知道进一步的帮助。

我发现一个有效的解决方案是将我放入!IsPostBack部分的代码也直接在页面加载事件中启用和禁用文本框。

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

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