Results 1 to 5 of 5

 

Thread: Help, going nuts - setting text property of a textbox in a user control

  1. #1
    accelerator's Avatar
    Online shopping rocks!

    Status
    Offline
    Join Date
    Nov 2004
    Location
    England
    Posts
    3,034
    Thanks
    54
    Thanked 183 Times in 164 Posts


    Oh dear, it's getting late and I am having problems setting the text property of a TextBox contained within a user control in vb.net:

    I have written a .ascx user control which includes the following TextBox:

    <asp:TextBox ID="ProductId" runat="server" Visible="false" />

    This user control is included on another page, let's say nokia-n95.aspx. The problem is I want to set the Text property of the above TextBox from that page. So far, I've tried in nokia-n95.aspx:

    Sub PageLoad()
    ProductId.Text = "23"
    End Sub

    but the value isn't getting carried through to the user control and is appearing as "".

    I guess this is something to do with the scope of variables and them getting carried into user controls?

    Can anyone help?

    Many thanks

    Accelerator

  2. #2
    Registered User

    Status
    Offline
    Join Date
    Aug 2005
    Posts
    128
    Thanks
    0
    Thanked 1 Time in 1 Post
    First off are you setting the Visible property back to on at any stage?

    Secondly - best way to pass into a user control is to create properties in the user control and then you can refer to them in the code of the main page such as :

    Me.UserControl.ProductID = "1234"

    Then in the Page_Load event of the user control have some code that picks up the value of the variable behind the property and stuffs it in the text box.
    haggul

  3. #3
    accelerator's Avatar
    Online shopping rocks!

    Status
    Offline
    Join Date
    Nov 2004
    Location
    England
    Posts
    3,034
    Thanks
    54
    Thanked 183 Times in 164 Posts
    Thanks for your help haggul, I have this working in the way I want it to now, although whether this is technically best practice I'm not sure ....

    In the .aspx page, the user control instance has a PageProductId property:

    <navito:reviewbuttons id="reviewbuttons1" runat="server" PageProductId="23" />

    within the .ascx user control I have the following Property accessor:

    <script runat="server">

    Public _PageProductId As String

    Public Property PageProductId() As String


    Get
    Return _PageProductId
    End Get

    Set(ByVal value As String)
    ProductId.Text = value
    End Set

    End Property

    </script>

    ....

    which sets the Text property of the following TextBox in the user control:

    <asp:TextBox ID="ProductId" runat="server" Visible="false" />


    So, now I can define the ProductId in the original .aspx page.

    Thanks again.


    Rgds

    Accelerator

  4. #4
    Registered User

    Status
    Offline
    Join Date
    Jun 2006
    Posts
    628
    Thanks
    7
    Thanked 70 Times in 67 Posts
    for what it's worth you can also address the textbox in the user control from your .aspx page like this
    Dim t As TextBox = YourUserControl.FindControl("YourTextBox")
    then set it's text property
    t.text="something"

    Jon

  5. #5
    Registered User

    Status
    Offline
    Join Date
    Aug 2005
    Posts
    128
    Thanks
    0
    Thanked 1 Time in 1 Post
    Yeah that also works I just find it easier to expsose things as properties - each to their own I guess!!

    You can actually make the variable _PageProductId Private in the ascx as it's only used internally to the user control.

    And you can obviously programatically control what is passed from your code behind your main page (I see you hard coded in the user control declaration which might cover what you want in this instance) - if you are using VWD or VS then intellisense will even pop up with the properties and methods you have defined yourself.

    Have you done much .Net? I'm actually working on my first full blown project which is a redevelopment of a pretty in depth e-Comm site - baptism of fire or what!! Luckily I have done other OOP stuff in the past. Biggest problem is there are almost too many ways to solve problems.
    haggul



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. New Vodafone deals on Mobile Rainbow!
    By MobileRainbow in forum Affiliate Future
    Replies: 0
    Last Post: 27-04-07, 02:52 PM
  2. New Vodafone deals on Mobile Rainbow!
    By MobileRainbow in forum Affiliate Marketing Lounge
    Replies: 0
    Last Post: 27-04-07, 02:43 PM
  3. PHP/mysql formating problem with paragraphs
    By Kandevil in forum Programming
    Replies: 2
    Last Post: 25-01-07, 09:48 PM
  4. Help! Setting property value in vb.net user control
    By accelerator in forum Programming
    Replies: 5
    Last Post: 22-04-06, 03:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2