(ASP.NET)在page_load()开始时更改输入
我有一个应该具有缺陷值的表单,用户可以决定在某些字段中更改。这样的输入是这样的:
<input type="text" id="username" clientidmode="static" runat="server" />
我在page_load()的开头设置默认数据:
username.Value = "test"
我以后在if(ispostback)中以这样的值来设置默认数据:
string sUsername = username.Value;
想法是他们将能够更改有关用户的某些值,或者如果不想更改该字段,则只剩下默认值。
问题是,当我在(iSpostback)中获取字段值时,即使用户更改了表单值,我也只是在page_load()开始时设置了我设置的值。
我在做什么错?
这是复制此事的简单方法。创建一个ASP.NET项目并添加WebForm。
然后将其添加到页面上:
<input type="text" id="username" runat="server" />
<input type="submit" />
并将其添加到背后的代码中:
protected void Page_Load(object sender, EventArgs e)
{
username.Value = "Test";
if (IsPostBack)
{
string sUsername = username.Value;
}
}
I have a form that is supposed to have defalt values, which the user can decide to change in some fields. The inputs are set up like this:
<input type="text" id="username" clientidmode="static" runat="server" />
I am setting the default data at the start of Page_Load() like this:
username.Value = "test"
And I'm getting the values later in if(isPostBack) like this:
string sUsername = username.Value;
The idea is that they will be able to change certain values about the user, or just leave the default values if they don't want to change that field.
The issue is that when I'm getting the field values in if(isPostBack) I'm just getting the values I've set at the start of Page_Load() even if the user changed the form values.
What am I doing wrong?
Here is a simple way to reproduce this. Create an asp.net project and add a WebForm.
Then add this to the page:
<input type="text" id="username" runat="server" />
<input type="submit" />
and add this to the code behind:
protected void Page_Load(object sender, EventArgs e)
{
username.Value = "Test";
if (IsPostBack)
{
string sUsername = username.Value;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的变量声明应该在内部!iSpostback
Your variable declaration should be inside !IsPostBack
问题是,当您单击其他内容时,您的页面加载事件触发和页面加载,然后您的按钮单击事件。
您已将!iSpostback放在错误的位置。您需要将默认数据保留在page_load()内部!iSpostback()的开头,然后在任何地方获取值的值,任何button_click事件。
请查看此页面:
edit textbox” (它没有将新数据插入和提交)
,尽管如果您可以更好地了解代码,那将很容易分辨。
The issue is that as soon as you click on something else, your page load event triggers and your page loads first then your button click event occurs.
You have put the !IsPostBack at the wrong place. You need to keep the default data at the start of Page_Load() inside !IsPostBack(), then get the values later anywhere you want, inside any button_click event.
Have a look at this page:
Edit Textbox not working (It is not taking the new data inserted and submitted)
Although, if you could have given some better view of code it would have been easy to tell.