Ok, I got it working!
I got rid of the page load code which wasn't the proper way of doing
things...
I use the code on the button click itself and it works like a charm!
Now, what I want to do is a bit more tricky!
On my user control, I added a button. On the click of that button, I
want to change the text of the Page's textbox!
Here's the updated code of the page with the new stuff I coded, if
anyone knows if it's possible to achieve and has suggestions, it would
be great!
Thanks!
Stef
Page code:
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
}
private void LoadControls()
{
}
#region Code généré par le Concepteur Web Form
override protected void OnInit(EventArgs e)
{
//
// CODEGEN : Cet appel est requis par le Concepteur Web Form
ASP.NET.
//
InitializeComponent();
base.OnInit(e);
if (ViewState["ts1Text"]==null) ViewState["ts1Text"] = "opop";
}
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne
modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </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)
{
ViewState["ts1Text"] = TextBox1.Text;
test1 ts1 = (test1)FindControl("test11");
ts1.Text = (string)ViewState["ts1Text"];
ts1.btn.Click += new EventHandler(this.SetValue);
}
//This one never gets called....
private void SetValue(object sender, System.EventArgs e)
{
TextBox1.Text = "UserControl button click event is working!";
}
}
UserControl code:
public class test1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
public Button btn
{
get
{
return Button1;
}
}
public string Text
{
get
{
return (string)ViewState["Text"];
}
set
{
ViewState["Text"] = value;
Label1.Text = (string)ViewState["Text"];
}
}
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Code généré par le Concepteur Web Form
override protected void OnInit(EventArgs e)
{
//
// CODEGEN : Cet appel est requis par le Concepteur Web Form
ASP.NET.
//
InitializeComponent();
base.OnInit(e);
if (ViewState["Text"]==null) ViewState["Text"]=string.Empty;
}
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne
modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </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)
{
}
}
Stef - 17 Apr 2006 15:47 GMT
Me again...
I should have created a blog with this topic... I have solved that
event stuff already!
Anyways, maybe this post will be helpful to somebody sometime!
What I did is this:
On the page load, I find the user control and call a method, declared
in the usercontrol, named "AttachEvent" which is defined like this:
public void AttachEvent(ref EventHandler addressOf)
{
Button1.Click += addressOf; //I should name the method properly on
what's been assigned etc...
}
Note that I've removed the getter of the usercontrol's button, which
shouldn't exists in any ways...
Then, on the main page, the main button click looks like this:
private void Button1_Click(object sender, System.EventArgs e)
{
ViewState["ts1Text"] = TextBox1.Text;
test1 ts1 = (test1)FindControl("test11");
ts1.Text = (string)ViewState["ts1Text"];
}
And here's the SetValue method which is defined in the main page's code
behind:
private void SetValue(object sender, System.EventArgs e)
{
TextBox1.Text = "UserControl button click event is working!";
}
Finally, here's the page load event, where I wire the usercontrol's
button event:
private void Page_Load(object sender, System.EventArgs e)
{
test1 ts1 = (test1)FindControl("test11");
EventHandler evt = new EventHandler(this.SetValue);
ts1.AttachEvent(ref evt);
}
It really works like charm right now!
Anyone can tell me if it's a proper way for doing this or if any of you
guys have concerns toward this approach?
Thanks in advance for the comments!
Stef