Results 1 to 9 of 9

Thread: UserControl and setting text

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question UserControl and setting text

    I have a simple user control:
    Code:
    <%@ Control Language="vb" AutoEventWireup="false" Codebehind="Length.ascx.vb" Inherits="GF.Length" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <asp:TextBox ID=txtLength Width=30px CssClass=tastFelt Runat=server></asp:TextBox>
    I've added 10 instaces of this in my application. How do I set the text-property for each of these user controls? Let's say the content should be 5,10,15... in the text boxes.

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    You need to go to your code behind and add protected members for each usercontrol with the exact same name as the control on your form.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Great, thanks.
    The next problem I have is how I can read the value from this user control. The user control name is Dimension and the first textbox is named txtLength1. How can I read this value, when I submit?
    I have tried this:
    Code:
    sub test_calc()
       dim test as single
       test = CSng(dimension1_txtLength1.text) 
    end sub
    but it doesn't work. Whats wrong?

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    The usercontrol will need to expose properties. For example:

    Code:
    public string TextBoxContents {
        get {
            return txtName.Text;
        }
        set {
            txtName.Text = value;
        }
    }
    Remember, a usercontrol is like a black box, it's internal data is hidden from the outside (concept of encapsulation). Only expose what needs to be exposed.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Great, just what i needed. Thanks a lot.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Sorry for disturbing again, but I can't make it work. Let' say my simple user control is:
    Code:
    <%@ Control Language="vb" AutoEventWireup="false" Codebehind="WebUserControl1.ascx.vb" Inherits="GF.WebUserControl1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <asp:TextBox ID=txtLength1 Runat=server>0</asp:TextBox>
    How would the code be for storing the content of the textbox to a new variable?

  7. #7
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    You would need to add this to your code-behind:

    Code:
    public string Length {
        get {
            return txtLength1.Text;
        }
        set {
            txtLength1.Text = value;
        }
    }

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    I am using VB.Net, I guess the code you wrote is C#. I also think there is another way: To declare the class where the user control is placed, like this, also added to the code behind:
    Code:
    protected testUserControl as ClassName.testUserControl.testUc.
    But I can't find out how get and set the property, do you know the code in VB that I need to insert in the codebehind?

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Now I've found out that I don't need to specify the classname, but what should the code be for VB instead of C#? Do you know where I can find some tutorials concerning User Controls?

Posting Permissions

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



Click Here to Expand Forum to Full Width