User Control Public Property Problem

D

Dan

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
 
V

Victor Garcia Aprea [MVP]

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.
 
D

Dan

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----- 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 said:
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


.
 
D

Dan

....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----- 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 said:
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


.
 
J

John Saunders

Dan said:
...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;}
}
 
V

Victor Garcia Aprea [MVP]

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 said:
...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 said:
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


.
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top