[英]ASP.NET dynamically created textbox doesn't change value
I have the following issue: I create a TextBox dynamically in my web page, its value is "initialVal" in the beginning. 我有以下问题:我在网页中动态创建一个TextBox,其开头的值为“ initialVal”。 Now I need to make a postback (not callback) to the server, and during this operation, I need to compute/change the value of my textbox to other value.
现在,我需要对服务器进行回发(而不是回调),并且在此操作期间,我需要将文本框的值计算/更改为其他值。
Here's an example: 这是一个例子:
protected void Page_Load(object sender, EventArgs e)
{
TextBox txtBox = new TextBox();
txtBox.ID = "newButton";
form1.Controls.Add(txtBox);
txtBox.Text = "initialVal";
if (IsPostBack && Session["change"] == null)
{
txtBox.Text = "change";
Session["change"] = true;
}
}
The problem: even if I change the value via code, the textbox will keep the text "initialVal". 问题:即使我通过代码更改了值,文本框也会保留文本“ initialVal”。 I feel this is something related to the view state, but I don't understand.
我觉得这与视图状态有关,但我不明白。 Coudl anyone please help me here?
库德尔有人请在这里帮助我吗?
Thanks. 谢谢。
Create your dynamic textbox creation in !IsPostBack 在!IsPostBack中创建动态文本框
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack){
TextBox txtBox = new TextBox();
txtBox.ID = "newButton";
form1.Controls.Add(txtBox);
txtBox.Text = "initialVal";
}
if (IsPostBack && Session["change"] == null)
{
txtBox.Text = "change";
Session["change"] = true;
}
}
Thanks and let me know if your issue still pending 谢谢,让我知道您的问题是否仍未解决
Everytime you load the page it is running this: 每次加载页面时,它都会运行以下命令:
txtBox.Text = "initialVal";
You should wrap this in a check for postback: 您应该将其包装在检查中以进行回发:
if (!Page.IsPostback)
{
txtBox.Text = "initialVal";
}
That said, onLoad
is the wrong event to do the creation, for it to be valid in the early enough in the page lifecycle, use OnInit
. 就是说,
onLoad
是进行创建的错误事件,要使其在页面生命周期的足够早的时间内有效,请使用OnInit
。
See this article on MSDN . 请参阅MSDN上的这篇文章 。
Here is the final code from @user2890888: 这是@ user2890888的最终代码:
public partial class WebForm1 : System.Web.UI.Page
{
TextBox txtBox = null;
protected void Page_Init(object sender, EventArgs e)
{
txtBox = new TextBox();
txtBox.ID = "newButton";
form1.Controls.Add(txtBox);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtBox.Text = "initialVal";
}
if (IsPostBack && Session["change"] == null)
{
txtBox.Text = "change";
Session["change"] = true;
}
}
}
Find the TextBox
and use it 找到
TextBox
并使用它
TextBox txtBox = (TextBox)FindControl("txtBox");
txtBox.Text = "change";
Even now you are having issue and your textBox is not getting the value you needed. 即使现在您遇到了问题,您的textBox也无法获得所需的值。 Store your new value for TEXTBOX in a hiddenfield.
将您的TEXTBOX新值存储在一个hiddenfield中。 I mean ,
我的意思是 ,
if (IsPostBack && Session["change"] == null)
{
hiddenfield1.value = "change";
}
and later in your page script, you can assign back the value in this hiddenfield1 to textbox. 然后在页面脚本中,您可以将此hiddenfield1中的值重新分配给文本框。
$(document).ready(
{
$('#txtBoxID').value=$('#hiddenfield1').value;
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.