在博客园中看到一个向用户自定义页面传参数的问题,自己以前没有用到过,想了想就动手实验了一下,结果用session也能做到,想象会不会还有别的方法,在google中找答案,发现的确有别的方法(自己知识浅,没有想到也没有用到过),在这里参考别人的回答做个笔记。
问题描述:在aspx页面中添加一个用户自定义控件(id:WebUserControl1)(自包含一个label (id:Label1)控件),aspx页面中放置一个textbox(id:TextBox1)控件和一个button(id:Button1)控件,当点击button时,把textbox值赋给ascx页面的lable控件。
方法一: 为用户自定义控件设置属性 代码如下
1 public string GetValue
2 {
3 get {
4 return this.Label1.Text;
5 }
6 set {
7 this.Label1.Text = value;
8 }
9 }
在aspx页面 button的onclick事件中编写如下代码
1 protected void Button1_Click(object sender, EventArgs e)
2 {
3 WebUserControl control = this.WebUserControl1;
4 control.GetValue = this.TextBox1.Text.Trim();
5 }
方法2:直接在aspx页面buttonde onclick事件中编写如下代码
1 protected void Button1_Click(object sender, EventArgs e)
2 {
3 (this.WebUserControl1.FindControl("Label1") as Label).Text = this.TextBox1.Text.Trim();
4 }