471,073 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

a simple webusercontrol ??

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
Nov 18 '05 #1
4 1571
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" <ch********@pandora.be> wrote in message news:Wz**********************@phobos.telenet-ops.be...
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

Nov 18 '05 #2
Indeed !

Thanks a lot !

Chris
"Andrea Williams" <an*******@hotmail.IHATESpam.com> wrote in message news:eF*************@TK2MSFTNGP11.phx.gbl...
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" <ch********@pandora.be> wrote in message news:Wz**********************@phobos.telenet-ops.be...
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

Nov 18 '05 #3
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" <an*******@hotmail.IHATESpam.com> wrote in message news:eF*************@TK2MSFTNGP11.phx.gbl...
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" <ch********@pandora.be> wrote in message news:Wz**********************@phobos.telenet-ops.be...
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

Nov 18 '05 #4
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" <ch********@pandora.be> wrote in message news:EP**********************@phobos.telenet-ops.be...
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" <an*******@hotmail.IHATESpam.com> wrote in message news:eF*************@TK2MSFTNGP11.phx.gbl...
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" <ch********@pandora.be> wrote in message news:Wz**********************@phobos.telenet-ops.be...
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

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mike Levin | last post: by
1 post views Thread by DichkoSoft | last post: by
reply views Thread by Christian H | last post: by
4 posts views Thread by Oren | last post: by
2 posts views Thread by Oren | last post: by
reply views Thread by Giovanni | last post: by
reply views Thread by Klaus Jensen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.