a simple webusercontrol ??

Discussion in 'ASP .Net' started by Chris, May 17, 2004.

  1. Chris

    Chris Guest

    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
     
    Chris, May 17, 2004
    #1
    1. Advertising

  2. 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$-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
    >
    >
     
    Andrea Williams, May 17, 2004
    #2
    1. Advertising

  3. Chris

    Chris Guest

    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$-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
    >
    >
     
    Chris, May 18, 2004
    #3
  4. Chris

    Chris Guest

    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$-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
    >
    >
     
    Chris, May 18, 2004
    #4
  5. 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$-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" <> 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$-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
    >
    >
     
    Andrea Williams, May 24, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Storr
    Replies:
    0
    Views:
    495
    Simon Storr
    Jul 14, 2003
  2. Christian
    Replies:
    2
    Views:
    2,051
    Christian Cambier
    Jul 15, 2003
  3. Rick Spiewak
    Replies:
    1
    Views:
    353
    Rick Spiewak
    Jul 24, 2003
  4. Christian
    Replies:
    0
    Views:
    372
    Christian
    Jul 21, 2003
  5. Andreas Klemt
    Replies:
    3
    Views:
    839
    Natty Gur
    Aug 28, 2003
Loading...

Share This Page