[英]ASP.NET C# why cant I change the text in a textbox in a webpage
I have an ASP.NET project with a simple webpage.When I load the page the textbox display "Hello".我有一个带有简单网页的 ASP.NET 项目。当我加载页面时,文本框显示“Hello”。 When I click the "btnUpload" the text goes away.
当我单击“btnUpload”时,文本消失了。 I tried !IsPostBack in the Load_Form function gut the just make the text stay the same.
我在 Load_Form 函数中尝试了 !IsPostBack 只是让文本保持不变。
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "Hello";
}
protected void btnUpload_Click(object sender, EventArgs e)
{
TextBox1.Text = "Good Bye";
}
This is because you have not used这是因为你没有使用
if(!IsPostBack) { }
inside Page_load()
method在
Page_load()
方法中
so modify your code as:所以修改你的代码:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
TextBox1.Text = "Hello";
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
TextBox1.Text = "Good Bye";
}
Because, you have not used this, thats why, on the postback of button it changes the value to TextBox1.Text = "Good Bye";
因为,你还没有使用过这个,这就是为什么,在按钮的回发时,它会将值更改为
TextBox1.Text = "Good Bye";
but it follows completing the postback with Page_Load()
so it changes it again to TextBox1.Text = "Hello";
但它在使用
Page_Load()
完成回发之后,再次将其更改为TextBox1.Text = "Hello";
that's the reason default value of Page_Load()
is shown after render.这就是在渲染后显示
Page_Load()
默认值的原因。
You have to use the following condition in the Page_Load
event handler, not in the Load_Form
:您必须在
Page_Load
事件处理程序中使用以下条件,而不是在Load_Form
:
if (!IsPostBack) {
TextBox1.Text = "Hello";
}
Your button click event handler is ok.您的按钮单击事件处理程序没问题。
Hope it helps!希望能帮助到你!
Please see below code :请看下面的代码:
Default.aspx:默认.aspx:
<asp:TextBox ID="text1" runat="server"></asp:TextBox>
<asp:Button ID="btn" runat="server" OnClick="btn_Click" />
Make sure aspx page contain same code for button as shown above.确保 aspx 页面包含与上图相同的按钮代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.