Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / April 2006

Tip: Looking for answers? Try searching our database.

Usercontrol always needs a second postback

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stef - 17 Apr 2006 14:45 GMT
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 - 17 Apr 2006 15:30 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.