请教一个问题

akey307 2010-01-23 08:18:42
在下面的代码中,我想实现的是:
(1) 第一次打开网页,TextBox中的文本为"Good Morning."
(2) 单击Button,形成Postback,然后更改TextBox的文本为"Good Afternoon"

可是不管怎样,TextBox的文本总是 "Good Morning.", 请各位帮我看看是什么原因。

(我不想捕捉Button的Onclick事件)



public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button button1 = new Button();
button1.Text = "Click";
button1.ID = "button1";

TextBox textBox1 = new TextBox();
textBox1.Text = "Good Morning.";
textBox1.ID = "textBox1";

if (IsPostBack)
{
textBox1.Text = "Good Afternoon.";
}

Form.Controls.Add(button1);
Form.Controls.Add(textBox1);
}
}

...全文
74 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuy052 2010-01-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 abigfrog 的回复:]
C# codepublicpartialclass _Default : System.Web.UI.Page
{protectedvoid Page_Load(object sender, EventArgs e)
{
Button button1=new Button();
button1.Text="Click";
?-
[/Quote]
你每次页面是否重载后都赋值,肯定不会变了


想半天也没想出咋整。。
期待结果。学习了。。
yieryi_ 2010-01-23
  • 打赏
  • 举报
回复
是不是初始化的时候有问题呢?
还有是不是没有对变量进行相应的变化?
单击之后,文本变化之后,是不是应该对变量进行处理啊!
yieryi_ 2010-01-23
  • 打赏
  • 举报
回复
不懂!
akey307 2010-01-23
  • 打赏
  • 举报
回复
不好使,

[Quote=引用 1 楼 abigfrog 的回复:]
C# codepublicpartialclass _Default : System.Web.UI.Page
{protectedvoid Page_Load(object sender, EventArgs e)
{
Button button1=new Button();
button1.Text="Click";
?-
[/Quote]
akey307 2010-01-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yangglemu 的回复:]
晕死,将变量声明放在函数里,那是局部变量
把函数的内容全删掉,再在前台代码里拖控件进去,ButtonClick事件里写代码
前台代码:
<asp:TextBox ID="txt" Text="good morning" runat="server"/>
<asp:Button ID="btn" Text="Test" OnClick="btn_Click" runat="server"/>

后台代码:
void btn_Click(object sender, EventArgs e)
{
txt.Text="good afternoon";
}
[/Quote]

我已经说了,我不想捕捉Button的OnClick事件。

我要动态添加组件到页面中。
  • 打赏
  • 举报
回复
晕死,将变量声明放在函数里,那是局部变量
把函数的内容全删掉,再在前台代码里拖控件进去,ButtonClick事件里写代码
前台代码:
<asp:TextBox ID="txt" Text="good morning" runat="server"/>
<asp:Button ID="btn" Text="Test" OnClick="btn_Click" runat="server"/>

后台代码:
void btn_Click(object sender, EventArgs e)
{
txt.Text="good afternoon";
}
iStringTheory 2010-01-23
  • 打赏
  • 举报
回复

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button button1 = new Button();
button1.Text = "Click";
button1.ID = "button1";

TextBox textBox1 = new TextBox();
textBox1.ID = "textBox1";

if (IsPostBack)
{
textBox1.Text = "Good Afternoon.";
}else{
textBox1.Text = "Good Morning.";
}

Form.Controls.Add(button1);
Form.Controls.Add(textBox1);
}
}


111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧