比如:
用户控件有一个按钮
页面调用了用户控件,我现在想能不能按用户控件里的按钮来改变页面其他服务器控件的呢?
怎么写程序?
vb.net的程序
试试这样在用户控件的后台代码中写:
DropDownList list=(DropDownList)Page.FindControl("DropDownList1");
if(list!=null)
list.Enabled=false;
DropDownList1是包含用户控件的aspx页上的一个控件。
可以给用户控件中的这个按钮点击事件做个公开事件,作为这个控件的事件,然后在调用这个控件的页面中订阅这个控件的这个事件。
public abstract class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Button1;
public event EventHandler Click;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// 设计器支持所需的方法 - 不要使用
/// 代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
Click(this,EventArgs.Empty);
}
}