Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > User Control Public Property Problem

Reply
Thread Tools

User Control Public Property Problem

 
 
Dan
Guest
Posts: n/a
 
      10-23-2003
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
 
Reply With Quote
 
 
 
 
Victor Garcia Aprea [MVP]
Guest
Posts: n/a
 
      10-23-2003
>>>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/de...nlifecycle.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" <(E-Mail Removed)> wrote in message
news:0b5d01c399a8$dba142c0$(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
Dan
Guest
Posts: n/a
 
      10-24-2003
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" <(E-Mail Removed)> wrote in message
>news:0b5d01c399a8$dba142c0$(E-Mail Removed)...
>> 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

>
>
>.
>

 
Reply With Quote
 
Dan
Guest
Posts: n/a
 
      10-24-2003
....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" <(E-Mail Removed)> wrote in message
>news:0b5d01c399a8$dba142c0$(E-Mail Removed)...
>> 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

>
>
>.
>

 
Reply With Quote
 
John Saunders
Guest
Posts: n/a
 
      10-24-2003
"Dan" <(E-Mail Removed)> wrote in message
news:08bc01c39a28$7bb86fc0$(E-Mail Removed)...
> ...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


 
Reply With Quote
 
Victor Garcia Aprea [MVP]
Guest
Posts: n/a
 
      10-24-2003
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" <(E-Mail Removed)> wrote in message
news:08bc01c39a28$7bb86fc0$(E-Mail Removed)...
> ...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" <(E-Mail Removed)> wrote in message
> >news:0b5d01c399a8$dba142c0$(E-Mail Removed)...
> >> 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

> >
> >
> >.
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSE 4 11-15-2006 02:40 AM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola Microsoft Certification 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSD 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd realexxams@yahoo.com Microsoft Certification 0 05-10-2006 02:35 PM
microsoft.public.dotnet.faqs,microsoft.public.dotnet.framework,microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.general,microsoft.public.dotnet.languages.vb Charles A. Lackman ASP .Net 1 12-08-2004 07:08 PM



Advertisments