Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 将母版页标题中的文本框值访问到.aspx.vb页_Asp.net_Master Pages - Fatal编程技术网

Asp.net 将母版页标题中的文本框值访问到.aspx.vb页

Asp.net 将母版页标题中的文本框值访问到.aspx.vb页,asp.net,master-pages,Asp.net,Master Pages,我在母版页的标题(用户控件)中创建了一个文本框和一个提交按钮 在我的.aspx.vb页面中单击“提交”后,我想使用该文本框值 当vb页面先加载,然后母版页加载时,如何访问该页面? 从UserControl到Page/MasterPage的最佳通信方式是使用事件 在母版页之间进行交流的最佳方式是使用事件 通过这种方式,您不会将用户控件与其页面/母版页及其母版页硬链接 将用户单击按钮时引发的事件添加到UserControl,例如: 在类型为MyControl的用户控件中: public deleg

我在母版页的标题(用户控件)中创建了一个文本框和一个提交按钮

在我的.aspx.vb页面中单击“提交”后,我想使用该文本框值

当vb页面先加载,然后母版页加载时,如何访问该页面?

  • 从UserControl到Page/MasterPage的最佳通信方式是使用事件
  • 在母版页之间进行交流的最佳方式是使用事件
通过这种方式,您不会将用户控件与其页面/母版页及其母版页硬链接

将用户单击按钮时引发的事件添加到UserControl,例如:

在类型为MyControl的用户控件中:

public delegate void SubmittedHandler(MyControl ctrl);
public event SubmittedHandler Submitted;

protected void BtnCLick(object sender, EventArgs e) {
    Submitted(this);
}
然后向母版页中的此事件添加处理程序,处理它并再次引发母版事件:

在Master's codebehind中:

public delegate void MyControlSubmittedHandler(MyControl ctrl);
public event MyControlSubmittedHandler ControlSubmitted;

protected void Page_Init(Object sender, EventArgs e) {
    this.MyControl1.Submitted += MyControlSubmitted;
}

protected void MyControlSubmitted(MyControl sender) {
    ControlSubmitted(sender);
}
然后将此事件的处理程序添加到页面:

在您的页面中:

protected void Page_Init(object sender, EventArgs e) {
    ((SiteMaster)Master).ControlSubmitted += MasterControlSubmitted;
}

protected void MasterControlSubmitted(MyControl sender){
    // do whatever you need to do
}
如果您只需要从页面访问
文本框
,而不需要处理单击事件,您也可以使用属性来实现这一点:

  • 在获取/设置
    TextBox.Text
    属性的
    UserControl
    中添加公共属性f.e.
    MyControlText
  • Master
    中添加公共属性f.e.
    MyControlText
    ,以获取/设置用户控件的
    MyControlText
    属性
  • 现在,您可以通过以下方式从页面获取/设置此属性:
    ((SiteMaster)Master.MyControlText=“Hello World”
TextBox Val=(TextBox)this.Master.FindControl(“TextBoxID”)