Usercontrol always needs a second postback

S

Stef

I folks,
I have a simple page with a user control dropped on it, a textbox
and a submit button.
That usercontrol should display the value entered in a textbox after
I've clicked the submit button but it doesn't on the first click! I
must click a second time to get the textbox value displayed in my
usercontrol! If I change the value in the textbox on each postback,
that value will always be displayed 2 clicks after!

Someone can help on this one?

Thanks!

Here's my code: (Note it's a test page to reproduce the problem)

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)
{
LoadControls();
}

private void LoadControls()
{
test1 ts1 =
(test1)FindControl("test11");//(test1)LoadControl("test1.ascx");
ts1.ID = "aa";
ts1.Text = (string)ViewState["ts1Text"];
}

#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"]=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)
{
ViewState["ts1Text"] = TextBox1.Text;
}
}

Usercontrol code:
public class test1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Label1;

public string Text
{
get
{
return (string)ViewState["Text"];
}
set
{
ViewState["Text"] = value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = Text;
}

#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);
}

/// <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.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
 
S

Stef

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)
{

}
}
 
S

Stef

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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top