javascript ASP.Net:如何在回发后维护 TextBox 状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17104535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.Net: How to maintain TextBox State after postback
提问by scsfdev
I would like to know how to maintain the control state that has been modified in Javascript.
I have two TextBoxes, one DropDownList and a button (all Runat=Server) in C# ASP.net 2010 Express.
First textbox is just accept whatever data user input.
Second textbox enable state will change based on DDL selected value.
If ddl selected value is "-", second textbox will become Enabled = False.
If not "-", it will become Enabled = True. This enable is done through Javascript.
我想知道如何维护在Javascript中修改过的控件状态。
我在 C# ASP.net 2010 Express 中有两个 TextBox,一个 DropDownList 和一个按钮(所有 Runat=Server)。
第一个文本框只是接受用户输入的任何数据。
第二个文本框启用状态将根据 DDL 选择的值而改变。
如果 ddl 选择的值为“-”,则第二个文本框将变为 Enabled = False。
如果不是“-”,它将变为 Enabled = True。此启用是通过 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. (I have a plan to implement other textbox and ddl so in my code I have Else if condition).
这是我的 Javascript 的代码。(我计划实现其他文本框和 ddl,因此在我的代码中我有 Else 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). So far Ok.
通过使用上述所有代码,当我运行项目时,页面加载正常。第二个文本框启用状态设置为 False(通过 Page_Load 事件)。到目前为止还好。
Then from my browser, I choose ddl value to other instead of "-", the textbox become enable because of javascript. This is Ok.
然后从我的浏览器中,我将 ddl 值选择为其他而不是“-”,由于 javascript,文本框变为启用。还行吧。
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.
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.
我将 ddl 值选择为“-”,第二个文本框被禁用。
单击按钮,页面回发发生,但这次文本框仍然启用。<< 这是我要解决的问题。我将 EnableViewState、ViewStateMode 更改为不同的值但仍然相同。
Is there any solution for this one?
有没有办法解决这个问题?
Here is my test image URL.
State 1,
State 2,
State 3
这是我的测试图像 URL。
状态 1,
状态 2,
状态 3
Sorry for the long post.
抱歉,帖子太长了。
回答by scsfdev
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. 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.
当我在 Javascript 中更新文本框的状态时,我会更新隐藏字段值。在我的页面加载事件中,我检查了所有隐藏字段值,并根据隐藏字段值,我将禁用/启用我的文本框,这对我来说不是一个好的解决方案。
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.
我不确定这是否可以视为“答案”,所以我还没有结束这个问题。
回答by Mayur Borad
<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.
它可能对你有帮助,让我知道进一步的帮助。
回答by Sun Sun Ku
I found a solution that worked was to put the code that I put in the !IsPostBack section for enabling and disabling the textboxes also directly in the page load event.
我发现一个有效的解决方案是将我放在 !IsPostBack 部分中的代码放在页面加载事件中,用于启用和禁用文本框。