a simple webusercontrol ??

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

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
 
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
 
Indeed !

Thanks a lot !

Chris
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
 
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

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

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
 
Back
Top