473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 1678
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Christian | last post by:
Hi, I try to change the position of a WebUserControl on my WEbform (layout is set to GridLayout) by changing the absolute position in the html-code but it does not work. When I change the...
0
by: Mike Levin | last post by:
Hello Group, I'm using VisualStudio.NET and put a DataGrid into a WebUserControl (an ascx file). For the DataGrid element, I have set AllowSorting="True" and OnSortCommand="Grid_Sort".
1
by: DichkoSoft | last post by:
Hi I create WebUserControl and very confused when use control For Example: ***************************** *wuc.ascx ***************************** <script id=clientEventHandlersJS...
0
by: Christian H | last post by:
Hello, I've just created a WebUserControl that consists of a few other controls such as DropDownList, TextBox and Button. Based on the information entered in the DropDownlist, and TextBox, the...
4
by: Oren | last post by:
hi, How can I call to a Function/Property and send data dynamically to the UserControl ? I mean Instead (look at CurrentPage): <Result:Header id="rh" CurrentPage="1" RecordsPerPage="2"...
2
by: Oren | last post by:
Hi everyone, I have WebUserControl on a Webform. How can I sent dynamically to the WebUserControl from a Function/Property on the Webform ? <uc:myuc id="myid1" CurrentPage=<%#GetText()%>...
0
by: Giovanni | last post by:
Dear Friends, I have a decision to make and hope to get some insight: I have built several WebUserControls one of which is a WebUserControl that queries an SQL server database, retrieves a...
0
by: Klaus Jensen | last post by:
Hi In a repeater-control, in the <SeparatorTemplatei have placed a webusercontrol, I have made. That works great- However, I want to know be able to only display this WebUserControl a certain...
1
by: deeparengan | last post by:
hi in asp.net i have a webusercontrol that has three dropdownlist.In the first dropdownlist i loaded the data at design time and in second dropdownlist i load the some data at run time according to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.