Referencing Values in a User Control

Last post 05-12-2008 3:06 AM by rajeshthangarasu. 7 replies.

Sort Posts:

  • Referencing Values in a User Control

    05-08-2008, 3:16 PM
    • Loading...
    • evanburen
    • Joined on 06-17-2002, 7:20 PM
    • Vancouver, BC
    • Posts 119

    Hi

    I'm clueless when it comes to user controls.  I have a control named header which contains 3 textboxes.  How do I grab the text entered in the textbox named txtCompanyName found in the control in my aspx page?

     

    My user control: header.ascx

    <%@ Control Language="VB" ClassName="header" %>

    <script runat="server">
        Protected Sub QuickSearch_Companies_Click(ByVal Sender As Object, ByVal e As EventArgs)
            Dim strCompanyName As String = txtCompanyName.Text
           
            'Take the text entered into the search form and send them to the search form
            Response.Redirect("quick.search.results.company.aspx?CompanyName=" & strCompanyName)
        End Sub
       
        Protected Sub QuickSearch_Candidate_Click(ByVal Sender As Object, ByVal e As EventArgs)
            Dim strFirstName As String = txtFirstName.Text
            Dim strLastName As String = txtLastName.Text
       
            'Take the text entered into the search form and send them to the search form
            Response.Redirect("quick.search.results.candidate.aspx?FirstName=" & strFirstName & "&LastName=" & strLastName)
        End Sub  
       

    </script>
            

    <table width="100%" border="0" cellpadding="3" cellspacing="0">

    <tr>
        <td> Company: <asp:Textbox ID="txtCompanyName" runat="server" CssClass="textbox" /></td>
        <td>Candidate Name: <asp:TextBox ID="txtFirstName" runat="server" CssClass="textbox" Columns="15" /> <asp:TextBox ID="txtLastName" runat="server" CssClass="textbox" Columns="15" Text="Last Name" /><asp:Button ID="btnCandidateSearch" runat="server" Text="go" CssClass="buttons" OnClick="QuickSearch_Candidate_Click" /></td>  
    </tr>   
    </table>


    .aspx page


    <%@ Page Language="VB" %>
    <%@ Register Src="header.ascx" TagName="header" TagPrefix="uc1" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>

    <script runat="server">

    'This fails
    Dim CompanyName As String = txtCompanyName.Text

    </script>


    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Candidate Search</title>   
    </head>
    <body>

    <form id="form1" runat="server">
      
    <!--This is my user control page header-->
    <uc1:header id="Header1" runat="server"></uc1:header>

    </form>

     

     

     

  • Re: Referencing Values in a User Control

    05-08-2008, 3:29 PM
    Answer

    Create public properties in the control that expose the textbox values you want to grab.
    If this answered your question, be sure to mark it as the answer. That way, everybody after you will know it's the answer also!
  • Re: Referencing Values in a User Control

    05-08-2008, 3:30 PM

     Check this post -

    http://www.dotnetjunkies.ddj.com/Article/E1F97CE9-7834-46FA-BED9-866F720AB013.dcik

    hope it helps. 

    Hope it helps.

    -Manas
  • Re: Referencing Values in a User Control

    05-08-2008, 3:52 PM
    • Loading...
    • evanburen
    • Joined on 06-17-2002, 7:20 PM
    • Vancouver, BC
    • Posts 119

    Thanks

    Is this also an accepted way of doing it?

    Dim CompanyName As Textbox=CType(Page.FindControl("Header1$txtCompanyName"),Textbox)

    Dim CompanyNameText As String = CompanyName.Text

     

     

  • Re: Referencing Values in a User Control

    05-09-2008, 6:50 AM

    I don't do VB very much, but it looks plausible.

    But I think that Header1.CompanyName (where CompanyName is a string) is a lot easier to code than your way. :) 

    If this answered your question, be sure to mark it as the answer. That way, everybody after you will know it's the answer also!
  • Re: Referencing Values in a User Control

    05-09-2008, 7:27 AM

     

    <script runat="server">

    'This fails
    Dim CompanyName As String = txtCompanyName.Text

    </script>

     

    Should not the statement be in a function or property ???

    Regards,

    Digvijay
    http://www.digvijay.eu
    http://blog.digvijay.eu

    --------
    Please remember to Mark As Answer if this post answered your question!
  • Re: Referencing Values in a User Control

    05-12-2008, 2:04 AM
    Answer

    Hi,

    In page, to access the control which is in the UserControl:

    Dim CompanyName As Textbox=CType(usercontrol.FindControl("txtCompanyName"),Textbox)

    In UserControl, you can use the below codes to access the control which is in parent page.

    Dim TextBox1 As Textbox =CType(Me.Parent.FindControl("TextBox1"),TextBox)

    Hope it helps.

     

    ================================================
    Sincerely,
    Vince Xu
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
    Answer” if a marked post does not actually answer your question. This can be beneficial to other
    community members reading the thread.
  • Re: Referencing Values in a User Control

    05-12-2008, 3:06 AM

    You have to use properties

    Property ? it is a special method that can return a current object?s state or set it. Simple syntax of properties can see in the following example:

    public int Old 
    {
       get {return m_old;}
       set {m_old = value;}
     } 
    public string Name 
    {
       get {return m_name;} 
    }

    Here are two types of properties. A first one can set or get field of class named m_old, and the second is read only. That?s mean it can only get current object?s state.

    The significance of these properties is its usability. These properties need not be called with any function names like objectname.get or objectname.set etc., But they can be directly assigned the values or retrieve the values.

     

    Mark as Answer if the post was useful to you

    Rajesh Thangarasu
    Microsoft Certified Professional
Page 1 of 1 (8 items)