Usercontrol always needs a second postback

Discussion in 'ASP .Net Building Controls' started by Stef, Apr 17, 2006.

  1. Stef

    Stef Guest

    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
    }
     
    Stef, Apr 17, 2006
    #1
    1. Advertising

  2. Stef

    Stef Guest

    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, Apr 17, 2006
    #2
    1. Advertising

  3. Stef

    Stef Guest

    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
     
    Stef, Apr 17, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven
    Replies:
    1
    Views:
    1,528
    Victor Garcia Aprea [MVP]
    Oct 23, 2004
  2. Hywel Jenkins
    Replies:
    1
    Views:
    329
    perseus
    Aug 7, 2003
  3. Ed Sproull [MSFT]
    Replies:
    0
    Views:
    317
    Ed Sproull [MSFT]
    Aug 7, 2007
  4. Mark van Bree

    Postback problem with usercontrol in usercontrol

    Mark van Bree, May 30, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    180
  5. yelipolok
    Replies:
    4
    Views:
    287
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page