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}esignControl runat=server></{0}esignControl >")]
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
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}esignControl runat=server></{0}esignControl >")]
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