By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

a simple webusercontrol ??

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.