Communications between controls

O

Oleg Slyuasrchuk

Hi, developers.

I'm sorry for the long posting, However, without the details it'd be hard to
understand the problem.


I have 2 controls on a page:



1. web control (ServiceConsole.ascx)



<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="ServiceConsole.ascx.cs" Inherits="ServiceClient.ServiceConsole"
%>


<asp:Button id=SwitchModes runat="server" Text="Switch to Live
mode"></asp:Button>


with its codebehind:

public class ServiceConsole : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button SwitchModes;

private void InitializeComponent()
{this.SwitchModes.Click += new System.EventHandler(this.SwitchModes_Click);
}

private void SwitchModes_Click(object sender, System.EventArgs e)
{
if (((Button)sender).Text.IndexOf("Live") != -1) ((Button)sender).Text
="Switch to Edit mode";
else ((Button)sender).Text ="Switch to Live mode";
}
}



2. and composite server control (in DesignControl.cs file ):

This control contains one label to keep text and one button ("edit") placed
on a panel.

ToolboxData("<{0}:DesignControl runat=server></{0}:DesignControl >")]
public class DesignControl : Control, INamingContainer
{
.....

private void InitializeComponent(){
this.PreRender += new System.EventHandler(Control_PreRender);
}

protected Panel PanelForButtons;

protected override void CreateChildControls(){
Label phToKeepHtml = new Label();
phToKeepHtml.Text ="text to be edited";
Controls.Add (phToKeepHtml);


PanelForButtons = new Panel ();
Button editButton = new Button ();
editButton.Text ="edit";
editButton.Click += new System.EventHandler(OnEdit_Click);
PanelForButtons.Controls.Add (editButton );

Controls.Add (PanelForButtons);
}

private void Control_PreRender(object sender, EventArgs e){
Control cf = (Control)this.Page.FindControl ("ServiceConsole1") ;
String buttonText= (string)((Button)cf.FindControl("SwitchModes")).Text;
if ( buttonText.IndexOf("Live") != -1 )
ViewState["UserSelectedPresentationMode"] ="Edit";
else ViewState["UserSelectedPresentationMode"] ="Live";

if( (string)ViewState["UserSelectedPresentationMode"] == "Edit")
PanelForButtons.Visible = true;
else PanelForButtons.Visible = false;
}


I registered the second control (DesignControl) to the toolbox and can drop
a couple of them on a web form



How should it work?

If I click a SwitchModes button in a first (ServiceConsole) control, I'd
like to hide
the editButton button on every DesignControl. I do that by setting



PanelForButtons.Visible = false;

for the panel containing the button (in fact, I have more buttons there, so
I use panel).



What have I done (the solution is presented in the code above)?


when user clicks SwitchModes button,

in SwitchModes_Click() I change text of the button.

Then, in Control_PreRender() for the DesignControl (this event occurs after
SwitchModes_Click()) I get a reference to first control as


Control cf = (Control)this.Page.FindControl ("ServiceConsole1") ;
String buttonText= (string)((Button)cf.FindControl("SwitchModes")).Text;

and change PanelForButtons.Visible value depending on the buttonText value.





The problem with this solution highlighted in green.

In FindControl method I rely on id of ServiceConsole as ServiceConsole1
(default id ).
However, a user can change the Id editing the control manually.



My questions are:
1. Can this approach be fixed in some way?
2. Is there any more common way to communicate between controls on a page
level (for example, using page level variable)?


Any feedback is greatly appreciated.



Oleg
 
K

Kelly Leahy

My questions are:
1. Can this approach be fixed in some way?
2. Is there any more common way to communicate between controls on a page
level (for example, using page level variable)?

Privet Oleg,

Can you use GetType on the elements of the controls
collection to check for other controls of this type?
Then you could do something special for each of the
controls (that isn't the sender) in a loop on the
controls collection (for example with foreach). This
would eliminate the problem of having to know the
controls names in advance. Presumably, you could even
put this code in your control itself (if you use
parent.controls or something like that). The problem is
that this would not find elements directly, so you need
to step through the controls using a recursive function
of some sort, I think.

Hope this helps.

Kelly
 
O

Oleg Slyuasrchuk

Thanks, Kelly
You confirmed my thouhts.
The snipped below does the job:


foreach (Control c in this.Parent.Controls)
{
Type t = c.GetType ().BaseType ;
Type t2 = typeof (CmsWebServiceConsole);
if (t.Equals(t2))
// We are where we need
}

I tried your approach yesterday and I was missing one small piece, namely
Type t = c.GetType ().BaseType ;

Oleg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top