Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > a simple webusercontrol ??

Reply
Thread Tools

a simple webusercontrol ??

 
 
Chris
Guest
Posts: n/a
 
      05-17-2004
Hi,

I have 2 textboxes (txtVal1, txtVal2) in a WebUserControl
(WebCalcUserControl) for which I write public properties :

public int Value1
{
get { return Convert.ToInt32(txtVal1.Text); }
set { txtVal1.Text = Convert.ToString(value); }
}
public int Value2
{
get { return Convert.ToInt32(txtVal2.Text); }
set { txtVal2.Text = Convert.ToString(value); }
}

Then, I host the user control in a WebForm for which I provide a private
datamember in the webform :


private WebCalcUserControl WebCalcUserControl1 = new WebCalcUserControl();

I add a button (btnAdd) and a Label (lblResult) to the WebForm as well.

Button Event-handler :
private void btnAdd_Click(object sender, System.EventArgs e)
{
lblResult.Text = Convert.ToString(
WebCalcUserControl1.Value1 +
WebCalcUserControl1.Value2);
}

I run the webapp ... enter 2 numbers in the texboxes ... press the
Add-button .. and get a run-time error : Input string was not in a correct
format.
When debugging do i notice that both Text-properties of the textboxes (of
the user control) are empty ??? (Although I entered values at run-time)

How come ?

Thanks

Chris


 
Reply With Quote
 
 
 
 
Andrea Williams
Guest
Posts: n/a
 
      05-17-2004
If you are declaring the Web control in the ASPX form (as It looks like), then you need to change the code in the class.

instead of using:
private WebCalcUserControl WebCalcUserControl1 = new WebCalcUserControl();

Code-Behind inside the Class declaration:
protected WebCalcUserControl WebCalcUserControl1; //The name you declare here, must match the ID on the ASPX page

I hope that helps...

Andrea Williams

"Chris" <> wrote in message news:Wz%pc.112515$...
> Hi,
>
> I have 2 textboxes (txtVal1, txtVal2) in a WebUserControl
> (WebCalcUserControl) for which I write public properties :
>
> public int Value1
> {
> get { return Convert.ToInt32(txtVal1.Text); }
> set { txtVal1.Text = Convert.ToString(value); }
> }
> public int Value2
> {
> get { return Convert.ToInt32(txtVal2.Text); }
> set { txtVal2.Text = Convert.ToString(value); }
> }
>
> Then, I host the user control in a WebForm for which I provide a private
> datamember in the webform :
>
>
> private WebCalcUserControl WebCalcUserControl1 = new WebCalcUserControl();
>
> I add a button (btnAdd) and a Label (lblResult) to the WebForm as well.
>
> Button Event-handler :
> private void btnAdd_Click(object sender, System.EventArgs e)
> {
> lblResult.Text = Convert.ToString(
> WebCalcUserControl1.Value1 +
> WebCalcUserControl1.Value2);
> }
>
> I run the webapp ... enter 2 numbers in the texboxes ... press the
> Add-button .. and get a run-time error : Input string was not in a correct
> format.
> When debugging do i notice that both Text-properties of the textboxes (of
> the user control) are empty ??? (Although I entered values at run-time)
>
> How come ?
>
> Thanks
>
> Chris
>
>

 
Reply With Quote
 
 
 
 
Chris
Guest
Posts: n/a
 
      05-18-2004
Indeed !

Thanks a lot !

Chris
"Andrea Williams" <> wrote in message news:...
If you are declaring the Web control in the ASPX form (as It looks like), then you need to change the code in the class.

instead of using:
private WebCalcUserControl WebCalcUserControl1 = new WebCalcUserControl();

Code-Behind inside the Class declaration:
protected WebCalcUserControl WebCalcUserControl1; //The name you declare here, must match the ID on the ASPX page

I hope that helps...

Andrea Williams

"Chris" <> wrote in message news:Wz%pc.112515$...
> Hi,
>
> I have 2 textboxes (txtVal1, txtVal2) in a WebUserControl
> (WebCalcUserControl) for which I write public properties :
>
> public int Value1
> {
> get { return Convert.ToInt32(txtVal1.Text); }
> set { txtVal1.Text = Convert.ToString(value); }
> }
> public int Value2
> {
> get { return Convert.ToInt32(txtVal2.Text); }
> set { txtVal2.Text = Convert.ToString(value); }
> }
>
> Then, I host the user control in a WebForm for which I provide a private
> datamember in the webform :
>
>
> private WebCalcUserControl WebCalcUserControl1 = new WebCalcUserControl();
>
> I add a button (btnAdd) and a Label (lblResult) to the WebForm as well.
>
> Button Event-handler :
> private void btnAdd_Click(object sender, System.EventArgs e)
> {
> lblResult.Text = Convert.ToString(
> WebCalcUserControl1.Value1 +
> WebCalcUserControl1.Value2);
> }
>
> I run the webapp ... enter 2 numbers in the texboxes ... press the
> Add-button .. and get a run-time error : Input string was not in a correct
> format.
> When debugging do i notice that both Text-properties of the textboxes (of
> the user control) are empty ??? (Although I entered values at run-time)
>
> How come ?
>
> Thanks
>
> Chris
>
>

 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      05-18-2004
but why does it have to be protected ?

using it as 'private' creates a runtime error ? ==> Object reference not set to an instance of an object.

Chris

"Andrea Williams" <> wrote in message news:...
If you are declaring the Web control in the ASPX form (as It looks like), then you need to change the code in the class.

instead of using:
private WebCalcUserControl WebCalcUserControl1 = new WebCalcUserControl();

Code-Behind inside the Class declaration:
protected WebCalcUserControl WebCalcUserControl1; //The name you declare here, must match the ID on the ASPX page

I hope that helps...

Andrea Williams

"Chris" <> wrote in message news:Wz%pc.112515$...
> Hi,
>
> I have 2 textboxes (txtVal1, txtVal2) in a WebUserControl
> (WebCalcUserControl) for which I write public properties :
>
> public int Value1
> {
> get { return Convert.ToInt32(txtVal1.Text); }
> set { txtVal1.Text = Convert.ToString(value); }
> }
> public int Value2
> {
> get { return Convert.ToInt32(txtVal2.Text); }
> set { txtVal2.Text = Convert.ToString(value); }
> }
>
> Then, I host the user control in a WebForm for which I provide a private
> datamember in the webform :
>
>
> private WebCalcUserControl WebCalcUserControl1 = new WebCalcUserControl();
>
> I add a button (btnAdd) and a Label (lblResult) to the WebForm as well.
>
> Button Event-handler :
> private void btnAdd_Click(object sender, System.EventArgs e)
> {
> lblResult.Text = Convert.ToString(
> WebCalcUserControl1.Value1 +
> WebCalcUserControl1.Value2);
> }
>
> I run the webapp ... enter 2 numbers in the texboxes ... press the
> Add-button .. and get a run-time error : Input string was not in a correct
> format.
> When debugging do i notice that both Text-properties of the textboxes (of
> the user control) are empty ??? (Although I entered values at run-time)
>
> How come ?
>
> Thanks
>
> Chris
>
>

 
Reply With Quote
 
Andrea Williams
Guest
Posts: n/a
 
      05-24-2004
When you use the protected keyword, then it allows the code-behind to connect to the controls and/or code that is declares in the ASPX page. Any variable that you would want to display in the ASPX page would also need to be protected.

For example:
in Code-behind:
protected string mstrPageTitle;

public void Page_Load()
{
mstrPageTitle = "This is my Title";
}

in ASPX page:
<%=mstrPageTitle%>

In order for the line above to work, it must be a protected variable. I guess you could say that the protected key word allows the code-behind to interact with the variables and objects. If they are private, they are private to the class only and are not shared with the ASPX.

I don't know how clear this is, so let me know if you still have questions. If someone else would like to elaborate more thoroughly, be my guest.
Andrea
"Chris" <> wrote in message news:EPhqc.114250$...
but why does it have to be protected ?

using it as 'private' creates a runtime error ? ==> Object reference not set to an instance of an object.

Chris

"Andrea Williams" <> wrote in message news:...
If you are declaring the Web control in the ASPX form (as It looks like), then you need to change the code in the class.

instead of using:
private WebCalcUserControl WebCalcUserControl1 = new WebCalcUserControl();

Code-Behind inside the Class declaration:
protected WebCalcUserControl WebCalcUserControl1; //The name you declare here, must match the ID on the ASPX page

I hope that helps...

Andrea Williams

"Chris" <> wrote in message news:Wz%pc.112515$...
> Hi,
>
> I have 2 textboxes (txtVal1, txtVal2) in a WebUserControl
> (WebCalcUserControl) for which I write public properties :
>
> public int Value1
> {
> get { return Convert.ToInt32(txtVal1.Text); }
> set { txtVal1.Text = Convert.ToString(value); }
> }
> public int Value2
> {
> get { return Convert.ToInt32(txtVal2.Text); }
> set { txtVal2.Text = Convert.ToString(value); }
> }
>
> Then, I host the user control in a WebForm for which I provide a private
> datamember in the webform :
>
>
> private WebCalcUserControl WebCalcUserControl1 = new WebCalcUserControl();
>
> I add a button (btnAdd) and a Label (lblResult) to the WebForm as well.
>
> Button Event-handler :
> private void btnAdd_Click(object sender, System.EventArgs e)
> {
> lblResult.Text = Convert.ToString(
> WebCalcUserControl1.Value1 +
> WebCalcUserControl1.Value2);
> }
>
> I run the webapp ... enter 2 numbers in the texboxes ... press the
> Add-button .. and get a run-time error : Input string was not in a correct
> format.
> When debugging do i notice that both Text-properties of the textboxes (of
> the user control) are empty ??? (Although I entered values at run-time)
>
> How come ?
>
> Thanks
>
> Chris
>
>

 
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
Re: a simple webusercontrol ?? Phil Winstanley [Microsoft MVP ASP.NET] ASP .Net 0 05-17-2004 11:27 AM
Re: can NOT change the position of a WebUserControl on page Rick Spiewak ASP .Net 1 07-24-2003 10:16 PM
can NOT change the position of a WebUserControl on page (GridLayout is set) Christian ASP .Net 0 07-21-2003 08:51 AM
Viewstate of WebUserControl does NOT work Christian ASP .Net 2 07-15-2003 03:48 PM
Re: click event does not occur in webusercontrol Simon Storr ASP .Net 0 07-14-2003 09:52 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57