User Control question - REPOSTED

Discussion in 'ASP .Net Web Controls' started by Brian Cesafsky, May 2, 2006.

  1. I am using framework 1.1

    I have a user control and a web page.

    I want to set up properties on the user control, so I can access the text
    boxes on the user control when I am in the code behind page of the 'main'
    web page.

    I have done this so far...


    1) On the user control... in the #Region " Web Form Designer Generated Code
    " I have 2 text boxes (for simplicity of this question). If I am on the
    main web page and I want to access the text boxes on the user control, I
    have to change the defenition of the text box from Protected WithEvents to
    Protected Shared WithEvents. Then I create the property like this in the
    user control...

    Public Class AccountCenterGeneral
    Inherits System.Web.UI.UserControl

    #Region " Web Form Designer Generated Code "
    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    End Sub

    Protected Shared WithEvents txtSalesTaxPercent As
    System.Web.UI.WebControls.TextBox
    Protected WithEvents txtNotes As System.Web.UI.WebControls.TextBox
    #End Region

    Public Property Notes() As String
    Get
    Return txtNotes.Text
    End Get

    Set(ByVal Value As String)
    txtNotes.Text = Value
    End Set
    End Property



    2) In the main web page, I can then code this to access the text box

    dim tempString as string
    tempString = AccountCenterGeneral.Notes (AccountCenterGeneral is the
    name of my class in the user control)



    THE PROBLEM... often times, if I make a change to the user control, ALL of
    my definitions for the controls get changed back to Protected WithEvents
    automatically and I have to reset them to Protected Shared WithEvents. So
    I'm wondering if I am doing this all wrong, or if I should go back to using
    this type of code... (can anyone explain which way is better)?



    dim tempString as string
    Dim currentTextBox As TextBox
    currentTextBox =
    Page.FindControl("AccountCenterGeneral1").FindControl("txtInterestRate")
    tempString = currentTextBox.Text



    Thanks in advance! Brian
     
    Brian Cesafsky, May 2, 2006
    #1
    1. Advertising

  2. Brian Cesafsky

    Kelly Leahy Guest

    Brian,

    You should add a property to your code behind file and have the property act
    as a proxy for the information you want to expose.

    For instance,

    Public Property Get TheFirstTextBox( ) As TextBox
    Return Me.TextBox1
    End Property

    Then, you should be able to access the property through your code on the page.

    Cheers,
    Kelly

    "Brian Cesafsky" wrote:

    > I am using framework 1.1
    >
    > I have a user control and a web page.
    >
    > I want to set up properties on the user control, so I can access the text
    > boxes on the user control when I am in the code behind page of the 'main'
    > web page.
    >
    > I have done this so far...
    >
    >
    > 1) On the user control... in the #Region " Web Form Designer Generated Code
    > " I have 2 text boxes (for simplicity of this question). If I am on the
    > main web page and I want to access the text boxes on the user control, I
    > have to change the defenition of the text box from Protected WithEvents to
    > Protected Shared WithEvents. Then I create the property like this in the
    > user control...
    >
    > Public Class AccountCenterGeneral
    > Inherits System.Web.UI.UserControl
    >
    > #Region " Web Form Designer Generated Code "
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    > End Sub
    >
    > Protected Shared WithEvents txtSalesTaxPercent As
    > System.Web.UI.WebControls.TextBox
    > Protected WithEvents txtNotes As System.Web.UI.WebControls.TextBox
    > #End Region
    >
    > Public Property Notes() As String
    > Get
    > Return txtNotes.Text
    > End Get
    >
    > Set(ByVal Value As String)
    > txtNotes.Text = Value
    > End Set
    > End Property
    >
    >
    >
    > 2) In the main web page, I can then code this to access the text box
    >
    > dim tempString as string
    > tempString = AccountCenterGeneral.Notes (AccountCenterGeneral is the
    > name of my class in the user control)
    >
    >
    >
    > THE PROBLEM... often times, if I make a change to the user control, ALL of
    > my definitions for the controls get changed back to Protected WithEvents
    > automatically and I have to reset them to Protected Shared WithEvents. So
    > I'm wondering if I am doing this all wrong, or if I should go back to using
    > this type of code... (can anyone explain which way is better)?
    >
    >
    >
    > dim tempString as string
    > Dim currentTextBox As TextBox
    > currentTextBox =
    > Page.FindControl("AccountCenterGeneral1").FindControl("txtInterestRate")
    > tempString = currentTextBox.Text
    >
    >
    >
    > Thanks in advance! Brian
    >
    >
    >
     
    Kelly Leahy, May 2, 2006
    #2
    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. news.amnet.net.au
    Replies:
    1
    Views:
    595
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Sharp Tool

    JoSQL reposted

    Sharp Tool, Oct 23, 2005, in forum: Java
    Replies:
    4
    Views:
    941
    Christian
    Oct 23, 2005
  3. Eric Giguere

    ANSI C Summary reposted

    Eric Giguere, Sep 30, 2003, in forum: C Programming
    Replies:
    0
    Views:
    416
    Eric Giguere
    Sep 30, 2003
  4. Tayna Taylor
    Replies:
    1
    Views:
    111
  5. CastAlone
    Replies:
    0
    Views:
    168
    CastAlone
    Aug 3, 2012
Loading...

Share This Page