In my .aspx page I have a 'input' html tag, also an asp button.
在我的.aspx页面中,我有一个'input'html标签,也是一个asp按钮。
<input id="Name" type="text" runat="server" clientidmode="Static" />
<asp:Button Width="100" type="submit" ID="sendOrder" runat="server" OnClick="SubmitForm" Text="Submit" />
On page load, I am filling the value in input tag from code behind, like this:
在页面加载时,我从后面的代码填充输入标记中的值,如下所示:
Name.Value= "X";
But now if I change value of this text box from browser, lets say "Y", and click on Submit button, then I get the old value, but not the new one.
但是现在如果我从浏览器更改此文本框的值,让我们说“Y”,然后单击“提交”按钮,然后我得到旧值,但不是新值。
protected void SubmitForm(object sender, EventArgs e)
{
var test= Name.Value; // here I get old value
}
How can I get the altered value?
我怎样才能获得改变的价值?
4 个解决方案
#1
4
Make sure you are only setting the value to "X" when its not a postback:
当它不是回发时,请确保您只将值设置为“X”:
if (!Page.IsPostBack){
Name.Value= "X";
}
Otherwise when clicking the submit button, the Page_Load()
event will change the value from "Y" back to "X".
否则,单击提交按钮时,Page_Load()事件会将值从“Y”更改回“X”。
#2
1
You need to use !IsPostBack
on Page_Load
shown as below:
你需要在Page_Load上使用!IsPostBack如下所示:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Value= "X";
}
}
Suggestion:
We can use use <input></input>
control in asp.net but best practice is to use <asp:TextBox></asp:TextBox>
control instead.
我们可以在asp.net中使用控件,但最佳实践是使用
Here is the sample example: HTML
以下是示例示例:HTML
<asp:TextBox ID="Name" runat="server"></asp:TextBox>
<asp:Button Width="100" ID="sendOrder" runat="server" OnClick="SubmitForm"
Text="Submit" />
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Text = "Some Value";
}
}
protected void SubmitForm(object sender, EventArgs e)
{
var test = Name.Text; //now get new value here..
}
#3
0
Check for IsPostback in Page_Load so you don't overwrite the values that are submitted!
在Page_Load中检查IsPostback,这样就不会覆盖提交的值!
#4
0
You don't need all the else portion, just do this
你不需要所有的其他部分,只需这样做
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//code to execute here only when an action is taken by the user
//and not affected by PostBack
}
//these codes should be affected by PostBack
}
#1
4
Make sure you are only setting the value to "X" when its not a postback:
当它不是回发时,请确保您只将值设置为“X”:
if (!Page.IsPostBack){
Name.Value= "X";
}
Otherwise when clicking the submit button, the Page_Load()
event will change the value from "Y" back to "X".
否则,单击提交按钮时,Page_Load()事件会将值从“Y”更改回“X”。
#2
1
You need to use !IsPostBack
on Page_Load
shown as below:
你需要在Page_Load上使用!IsPostBack如下所示:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Value= "X";
}
}
Suggestion:
We can use use <input></input>
control in asp.net but best practice is to use <asp:TextBox></asp:TextBox>
control instead.
我们可以在asp.net中使用控件,但最佳实践是使用
Here is the sample example: HTML
以下是示例示例:HTML
<asp:TextBox ID="Name" runat="server"></asp:TextBox>
<asp:Button Width="100" ID="sendOrder" runat="server" OnClick="SubmitForm"
Text="Submit" />
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Text = "Some Value";
}
}
protected void SubmitForm(object sender, EventArgs e)
{
var test = Name.Text; //now get new value here..
}
#3
0
Check for IsPostback in Page_Load so you don't overwrite the values that are submitted!
在Page_Load中检查IsPostback,这样就不会覆盖提交的值!
#4
0
You don't need all the else portion, just do this
你不需要所有的其他部分,只需这样做
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//code to execute here only when an action is taken by the user
//and not affected by PostBack
}
//these codes should be affected by PostBack
}