User Control Public Property Problem

Discussion in 'ASP .Net Building Controls' started by Dan, Oct 23, 2003.

  1. Dan

    Dan Guest

    I have built a user control that displays the header on a
    web forms. It has a public property ("Msg") that I can set
    to display error messages from the main web form in a
    consistant way.

    In a very simple example I have a web form that has the
    header user control, a button, and a text box. I can set
    the public property "Msg" of the user control on the
    Page_Load event of the web form and it works just fine,
    but......

    ...here is the problem. I have a Button_Click event that
    tries to set the property of the user control ("Msg") to
    the value of the text box, but the user control never
    seems to pick this up.

    I think this has to do with the order of events firing. It
    seems that when you click the button, the form is posted
    to the server... then the page_load event fires... then
    the button_click method is called. It also appears that
    the user control is rendered before the button_click
    event, so I am not able to set the property at that point.

    Is my theory correct? Is the user control in fact rendered
    during the page_load event of the calling webform? Is
    there any way to set public properties of user controls
    from a button click event?

    Any help is greatly appreciated.
    -Dan
     
    Dan, Oct 23, 2003
    #1
    1. Advertising

  2. >>>It also appears that the user control is rendered before the button_click
    event,
    Nope, its not. Please read this[1] to get a well grasp of the control
    execution lifecycle and this[2] if you really want to get your hands dirty.

    [1]
    http://msdn.microsoft.com/library/d...guide/html/cpconcontrolexecutionlifecycle.asp
    [2] http://weblogs.asp.net/vga/posts/23498.aspx

    Chances are that you're confused about the control lifecycle, if after the
    suggested reading you still can't make your control work, please post some
    more details along with code here,

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx

    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    and not by private mail.
    "Dan" <> wrote in message
    news:0b5d01c399a8$dba142c0$...
    > I have built a user control that displays the header on a
    > web forms. It has a public property ("Msg") that I can set
    > to display error messages from the main web form in a
    > consistant way.
    >
    > In a very simple example I have a web form that has the
    > header user control, a button, and a text box. I can set
    > the public property "Msg" of the user control on the
    > Page_Load event of the web form and it works just fine,
    > but......
    >
    > ..here is the problem. I have a Button_Click event that
    > tries to set the property of the user control ("Msg") to
    > the value of the text box, but the user control never
    > seems to pick this up.
    >
    > I think this has to do with the order of events firing. It
    > seems that when you click the button, the form is posted
    > to the server... then the page_load event fires... then
    > the button_click method is called. It also appears that
    > the user control is rendered before the button_click
    > event, so I am not able to set the property at that point.
    >
    > Is my theory correct? Is the user control in fact rendered
    > during the page_load event of the calling webform? Is
    > there any way to set public properties of user controls
    > from a button click event?
    >
    > Any help is greatly appreciated.
    > -Dan
     
    Victor Garcia Aprea [MVP], Oct 23, 2003
    #2
    1. Advertising

  3. Dan

    Dan Guest

    OK. I read those articles and I'm still not able to get it
    to work, so I will post some code.


    //User Control Code Behind Snippit
    //=========================================================
    ====
    public string Msg = "";
    private void Page_Load(object sender, System.EventArgs e){
    lblMsg.Text= Msg;
    }

    //Web Form Code Behind Code Snippit
    //=========================================================
    ====
    protected System.Web.UI.WebControls.Button Button1;
    protected wrap_top Wrap_top1;
    private void Page_Load(object sender, System.EventArgs e){
    if(! Page.IsPostBack ){
    Wrap_top1.Msg = "Here 1";
    }
    else{
    //Wrap_top1.Msg = "Here 2";
    }
    }
    private void Button1_Click(object sender, System.EventArgs
    e){
    Wrap_top1.Msg = "Here 3";
    }


    //Web Form Code Snippit
    /*================================================
    <%@ Register TagPrefix="uc1" TagName="wrap_top"
    Src="UserControls/wrap_top.ascx" %>
    <form id="Form1" method="post" runat="server">
    <uc1:wrap_top id="Wrap_top1" runat="server"></uc1:wrap_top>
    */







    >-----Original Message-----
    >>>>It also appears that the user control is rendered

    before the button_click
    >event,
    >Nope, its not. Please read this[1] to get a well grasp of

    the control
    >execution lifecycle and this[2] if you really want to get

    your hands dirty.
    >
    >[1]
    >http://msdn.microsoft.com/library/default.asp?

    url=/library/en-
    us/cpguide/html/cpconcontrolexecutionlifecycle.asp
    >[2] http://weblogs.asp.net/vga/posts/23498.aspx
    >
    >Chances are that you're confused about the control

    lifecycle, if after the
    >suggested reading you still can't make your control work,

    please post some
    >more details along with code here,
    >
    >--
    >Victor Garcia Aprea
    >Microsoft MVP | ASP.NET
    >Looking for insights on ASP.NET? Read my blog:
    >http://obies.com/vga/blog.aspx
    >
    >To contact me remove 'NOSPAM'. Please post all questions

    to the newsgroup
    >
    >and not by private mail.
    >"Dan" <> wrote in message
    >news:0b5d01c399a8$dba142c0$...
    >> I have built a user control that displays the header on

    a
    >> web forms. It has a public property ("Msg") that I can

    set
    >> to display error messages from the main web form in a
    >> consistant way.
    >>
    >> In a very simple example I have a web form that has the
    >> header user control, a button, and a text box. I can

    set
    >> the public property "Msg" of the user control on the
    >> Page_Load event of the web form and it works just fine,
    >> but......
    >>
    >> ..here is the problem. I have a Button_Click event that
    >> tries to set the property of the user control ("Msg") to
    >> the value of the text box, but the user control never
    >> seems to pick this up.
    >>
    >> I think this has to do with the order of events firing.

    It
    >> seems that when you click the button, the form is posted
    >> to the server... then the page_load event fires... then
    >> the button_click method is called. It also appears that
    >> the user control is rendered before the button_click
    >> event, so I am not able to set the property at that

    point.
    >>
    >> Is my theory correct? Is the user control in fact

    rendered
    >> during the page_load event of the calling webform? Is
    >> there any way to set public properties of user controls
    >> from a button click event?
    >>
    >> Any help is greatly appreciated.
    >> -Dan

    >
    >
    >.
    >
     
    Dan, Oct 24, 2003
    #3
  4. Dan

    Dan Guest

    ....oops... wasn't quite ready to post that last thread....
    so here it is again. In the following code... When this is
    run the user control will always display "Here 1"
    initialy, but then I am never able to get the User Control
    to display "Here 3", but it will display "Here 2" if I
    uncomment out that line. IF anyone can point out what I am
    doing wrong it would be a huge help. Thanks!

    //User Control Code Behind Snippit
    //============================================
    public string Msg = "";
    private void Page_Load(object sender, System.EventArgs e){
    lblMsg.Text= Msg;
    }

    //Web Form Code Behind Code Snippit
    //============================================
    protected System.Web.UI.WebControls.Button Button1;
    protected wrap_top Wrap_top1;
    private void Page_Load(object sender, System.EventArgs e){
    if(! Page.IsPostBack ){
    Wrap_top1.Msg = "Here 1";
    }
    else{
    //Wrap_top1.Msg = "Here 2";
    }
    }
    private void Button1_Click(object sender, System.EventArgs
    e){
    Wrap_top1.Msg = "Here 3";
    }


    //Web Form Code Snippit
    /*============================================
    <%@ Register TagPrefix="uc1" TagName="wrap_top"
    Src="UserControls/wrap_top.ascx" %>
    <form id="Form1" method="post" runat="server">
    <uc1:wrap_top id="Wrap_top1" runat="server"></uc1:wrap_top>
    */


    >-----Original Message-----
    >>>>It also appears that the user control is rendered

    before the button_click
    >event,
    >Nope, its not. Please read this[1] to get a well grasp of

    the control
    >execution lifecycle and this[2] if you really want to get

    your hands dirty.
    >
    >[1]
    >http://msdn.microsoft.com/library/default.asp?

    url=/library/en-
    us/cpguide/html/cpconcontrolexecutionlifecycle.asp
    >[2] http://weblogs.asp.net/vga/posts/23498.aspx
    >
    >Chances are that you're confused about the control

    lifecycle, if after the
    >suggested reading you still can't make your control work,

    please post some
    >more details along with code here,
    >
    >--
    >Victor Garcia Aprea
    >Microsoft MVP | ASP.NET
    >Looking for insights on ASP.NET? Read my blog:
    >http://obies.com/vga/blog.aspx
    >
    >To contact me remove 'NOSPAM'. Please post all questions

    to the newsgroup
    >
    >and not by private mail.
    >"Dan" <> wrote in message
    >news:0b5d01c399a8$dba142c0$...
    >> I have built a user control that displays the header on

    a
    >> web forms. It has a public property ("Msg") that I can

    set
    >> to display error messages from the main web form in a
    >> consistant way.
    >>
    >> In a very simple example I have a web form that has the
    >> header user control, a button, and a text box. I can

    set
    >> the public property "Msg" of the user control on the
    >> Page_Load event of the web form and it works just fine,
    >> but......
    >>
    >> ..here is the problem. I have a Button_Click event that
    >> tries to set the property of the user control ("Msg") to
    >> the value of the text box, but the user control never
    >> seems to pick this up.
    >>
    >> I think this has to do with the order of events firing.

    It
    >> seems that when you click the button, the form is posted
    >> to the server... then the page_load event fires... then
    >> the button_click method is called. It also appears that
    >> the user control is rendered before the button_click
    >> event, so I am not able to set the property at that

    point.
    >>
    >> Is my theory correct? Is the user control in fact

    rendered
    >> during the page_load event of the calling webform? Is
    >> there any way to set public properties of user controls
    >> from a button click event?
    >>
    >> Any help is greatly appreciated.
    >> -Dan

    >
    >
    >.
    >
     
    Dan, Oct 24, 2003
    #4
  5. "Dan" <> wrote in message
    news:08bc01c39a28$7bb86fc0$...
    > ...oops... wasn't quite ready to post that last thread....
    > so here it is again. In the following code... When this is
    > run the user control will always display "Here 1"
    > initialy, but then I am never able to get the User Control
    > to display "Here 3", but it will display "Here 2" if I
    > uncomment out that line. IF anyone can point out what I am
    > doing wrong it would be a huge help. Thanks!
    >
    > //User Control Code Behind Snippit
    > //============================================
    > public string Msg = "";


    This is not a public property. It's a public field. You should almost NEVER
    use public fields. Instead:

    public string Msg
    {
    get {return ViewState["Msg"] == null ? string.Empty : (string)
    ViewState["Msg"];}
    set {ViewState["Msg"] = value; lblMsg.Text = value;}
    }

    --
    John
     
    John Saunders, Oct 24, 2003
    #5
  6. Hi Dan,

    Have you actually checked that the click event is being fired at all? (I'm
    not seeing any event-wireup code in your posted code). You could do this
    easily check this by using the integrated debugger and setting a breakpoint
    at Button1_Click.

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    and not by private mail.

    "Dan" <> wrote in message
    news:08bc01c39a28$7bb86fc0$...
    > ...oops... wasn't quite ready to post that last thread....
    > so here it is again. In the following code... When this is
    > run the user control will always display "Here 1"
    > initialy, but then I am never able to get the User Control
    > to display "Here 3", but it will display "Here 2" if I
    > uncomment out that line. IF anyone can point out what I am
    > doing wrong it would be a huge help. Thanks!
    >
    > //User Control Code Behind Snippit
    > //============================================
    > public string Msg = "";
    > private void Page_Load(object sender, System.EventArgs e){
    > lblMsg.Text= Msg;
    > }
    >
    > //Web Form Code Behind Code Snippit
    > //============================================
    > protected System.Web.UI.WebControls.Button Button1;
    > protected wrap_top Wrap_top1;
    > private void Page_Load(object sender, System.EventArgs e){
    > if(! Page.IsPostBack ){
    > Wrap_top1.Msg = "Here 1";
    > }
    > else{
    > //Wrap_top1.Msg = "Here 2";
    > }
    > }
    > private void Button1_Click(object sender, System.EventArgs
    > e){
    > Wrap_top1.Msg = "Here 3";
    > }
    >
    >
    > //Web Form Code Snippit
    > /*============================================
    > <%@ Register TagPrefix="uc1" TagName="wrap_top"
    > Src="UserControls/wrap_top.ascx" %>
    > <form id="Form1" method="post" runat="server">
    > <uc1:wrap_top id="Wrap_top1" runat="server"></uc1:wrap_top>
    > */
    >
    >
    > >-----Original Message-----
    > >>>>It also appears that the user control is rendered

    > before the button_click
    > >event,
    > >Nope, its not. Please read this[1] to get a well grasp of

    > the control
    > >execution lifecycle and this[2] if you really want to get

    > your hands dirty.
    > >
    > >[1]
    > >http://msdn.microsoft.com/library/default.asp?

    > url=/library/en-
    > us/cpguide/html/cpconcontrolexecutionlifecycle.asp
    > >[2] http://weblogs.asp.net/vga/posts/23498.aspx
    > >
    > >Chances are that you're confused about the control

    > lifecycle, if after the
    > >suggested reading you still can't make your control work,

    > please post some
    > >more details along with code here,
    > >
    > >--
    > >Victor Garcia Aprea
    > >Microsoft MVP | ASP.NET
    > >Looking for insights on ASP.NET? Read my blog:
    > >http://obies.com/vga/blog.aspx
    > >
    > >To contact me remove 'NOSPAM'. Please post all questions

    > to the newsgroup
    > >
    > >and not by private mail.
    > >"Dan" <> wrote in message
    > >news:0b5d01c399a8$dba142c0$...
    > >> I have built a user control that displays the header on

    > a
    > >> web forms. It has a public property ("Msg") that I can

    > set
    > >> to display error messages from the main web form in a
    > >> consistant way.
    > >>
    > >> In a very simple example I have a web form that has the
    > >> header user control, a button, and a text box. I can

    > set
    > >> the public property "Msg" of the user control on the
    > >> Page_Load event of the web form and it works just fine,
    > >> but......
    > >>
    > >> ..here is the problem. I have a Button_Click event that
    > >> tries to set the property of the user control ("Msg") to
    > >> the value of the text box, but the user control never
    > >> seems to pick this up.
    > >>
    > >> I think this has to do with the order of events firing.

    > It
    > >> seems that when you click the button, the form is posted
    > >> to the server... then the page_load event fires... then
    > >> the button_click method is called. It also appears that
    > >> the user control is rendered before the button_click
    > >> event, so I am not able to set the property at that

    > point.
    > >>
    > >> Is my theory correct? Is the user control in fact

    > rendered
    > >> during the page_load event of the calling webform? Is
    > >> there any way to set public properties of user controls
    > >> from a button click event?
    > >>
    > >> Any help is greatly appreciated.
    > >> -Dan

    > >
    > >
    > >.
    > >
     
    Victor Garcia Aprea [MVP], Oct 24, 2003
    #6
    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. Charles A. Lackman
    Replies:
    1
    Views:
    1,391
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    593
    SpamProof
    Oct 21, 2003
  3. Kevin Spencer
    Replies:
    2
    Views:
    3,318
    Kevin Spencer
    Sep 15, 2004
  4. DC Gringo
    Replies:
    1
    Views:
    5,079
    Karl Seguin
    Apr 26, 2005
  5. sharpfun23
    Replies:
    0
    Views:
    830
    sharpfun23
    Aug 29, 2006
Loading...

Share This Page