Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UserControl Problem

Status
Not open for further replies.

suicidaltendencies

Programmer
Jan 28, 2004
58
US
Here is my problem. I'm putting 3 user controls on my page. In my page load function I'm specifying the visiblity of the user controls. 2 invisible and 1 visible. In the visible control there are buttons. I want the the other controls to be visible if the buttons are pressed. How do I detect the button was clicked on the visible usercontrol? What code should I write in my page?

Thanks,
Harold

Page section:
<pm:usercontrol id="usercontrol" runat="server" />
<pm:usercontrol id="Usercontrol1" runat="server" />
<pm:usercontrol id="Usercontrol2" runat="server" />


CodeBehind section:
private void Page_Load(object sender, System.EventArgs e)
{
Control usercontrol = Page.FindControl("usercontrol");
Control Usercontrol1 = Page.FindControl("Usercontrol1");
Control Usercontrol2 = Page.FindControl("Usercontrol2");

if(!Page.IsPostBack)
{
usercontrol.Visible=true;
Usercontrol1.Visible=false;
Usercontrol2 .Visible=false;
}
}
 
The code to make the other two controls visible should be written in the Button Click event handler in the "visible" control code behind, not on the container page:
Code:
private void Button1_Click(object sender, System.EventArgs e)
{
  Parent.FindControl("Usercontrol1").Visible = true;
  Parent.FindControl("Usercontrol2").Visible = true;
}
 
You can create a custom event handler in your ascx and wire it up to a handler in your aspx like this.
Code:
//In ASCX

public event System.EventHandler MyEvent;

private void Button1_Click(object sender, System.EventArgs e){
  MyEvent(sender, e);
}

//In ASPX

private void InitializeComponent(){
  this.Load += new System.EventHandler(this.Page_Load);
  this.UserControl1.MyEvent += new System.EventHandler(this.MyEvent_Event);
}

public void MyEvent_Event(object sender, System.EventArgs e){
  //Show the buttons
}

You can use this technique to create and "bubble" (though its not strictly event bubbling) events from ascx and custom server controls.

HTH

Rob


Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top