Ok, here is the scenario and the solution:
-I have 2 WebUserControls :
ProviderControls.ascx : it contains a TextBox and a Button.
ConsumerControl.ascx : it contains a Label
I have a WebForm called HostPage.aspx
The communication consist to pass the content of the textbox to the label
only when we click on the button
Code in App_Code folder : MyHandlers.cs
--------------------------------------------
public delegate void ConsumerControlEventHandler(object sender,
ConsumerControlEventArg e);
public class ConsumerControlEventArg : EventArgs
{
public string TheValue = string.Empty;
}
Code in ProviderControl.ascx.cs
-----------------------------------
public partial class UserControlsCommunication_ProviderControl :
System.Web.UI.UserControl
{
public event ConsumerControlEventHandler NewValue;
protected void cmdOK_Click(object sender, EventArgs e)
{
ConsumerControlEventArg arg = new ConsumerControlEventArg();
arg.TheValue = txtValue.Text;
NewValue(this, arg);
}
}
Code in ConsumerControl.ascx.cs
-----------------------------------
public partial class UserControlsCommunication_ConsumerControl :
System.Web.UI.UserControl
{
public string Param1
{
get
{
String s = (String)ViewState["Param1"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Param1"] = value;
}
}
void Page_PreRender(object sender, EventArgs e)
{
lblResult.Text = Param1;
}
}
Code in HostPage.aspx.cs
---------------------------
public partial class UserControlsCommunication_HostPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ProviderControl1.NewValue += new
ConsumerControlEventHandler(ProviderControl1_NewVa lue);
}
void ProviderControl1_NewValue(object sender, ConsumerControlEventArg e)
{
ConsumerControl1.Param1 = e.TheValue;
}
}
Have fun :)
--
Daniel TIZON
MCP - MCSD.NET - MCT
<je************@yahoo.se> a écrit dans le message de news:
11*********************@g47g2000cwa.googlegroups.c om...
Hi, thanks for your answer. Think you have right that the main
usercontrol is trying to get the value before a new has been set.
Will try to get it in the PreRender method.
But if you have time i would really like to see a short exemple of your
solution for it. What to learn a new way to handle this and your
solution sounds intressting.
Thanks
Jesper