How do you get at the Textbox that is in another User Control?

Discussion in 'ASP .Net' started by aarepasky@yahoo.com, May 23, 2006.

  1. Guest

    I am using ASP Web 2005 express.

    I created the user control below and I am listing the html code. I
    have 2 textboxes. One is txtCompanyName.

    The main page has 2 panels on it and a button. Panel1 contains the
    user control1 which is a form that has the txtCompanyName. Panel2
    contains another user control2 which is another form.

    When I click the button, I want to do something with the textbox
    txtCompanyName in the code behind file using C#. But the code behind
    does not see the textbox, because it is in another aspx file, not a
    code behind file. So how do I set this up so that it sees the textbox?

    I am new to asp.net and .net. I am getting frustrated that I can not
    come up with a way to see this textbox. There must be a way to see
    everything in every file.

    Thanks,
    Al

    <%@ Control Language="C#" AutoEventWireup="true"
    CodeFile="Companies.ascx.cs" Inherits="Companies" %>
    <table style="width: 348px">

    <caption style="color: #ffffff; background-color: #6666cc;
    font-weight: bold; font-family: Arial;" align="center">
    New Client Information</caption>

    <tr>
    <td style="width: 544px; font-family: Arial; height: 6px;
    background-color: buttonface;" align="right" bordercolor="#ffffff"
    bgcolor="buttonface">
    <asp:Label ID="lblCompanyName" runat="server" Text="Company
    Name:" Width="150px" Font-Names="Arial"></asp:Label></td>
    <td style="width: 318px; height: 6px;" bordercolor="#ffffff"
    bgcolor="buttonface">
    <asp:TextBox ID="txtCompanyName" runat="server"
    CausesValidation="True" Width="220px"></asp:TextBox></td>
    <asp:RequiredFieldValidator ID="
    RequiredFieldValidator1"
    runat="server" ErrorMessage="Must Enter Company Name"
    ControlToValidate="txtCompanyName"></asp:RequiredFieldValidator><td
    style="height: 6px; width: 3px;" bordercolor="#ffffff">

    <tr>
    <td style="width: 544px; font-family: Arial; background-color:
    buttonface; height: 6px;" align="right" bgcolor="buttonface">
    <asp:Label ID="lblUniqueCode" runat="server" Text="Unique
    Client Code:" Width="150px" Font-Names="Arial"></asp:Label></td>
    <td style="width: 318px" bgcolor="buttonface">
    <asp:TextBox ID="txtUniqueCode" runat="server" Wrap="False"
    Width="220px"></asp:TextBox></td>
    </tr>

    </table>
     
    , May 23, 2006
    #1
    1. Advertisements

  2. You have to put an accessor in your user control code behind to have
    access to the textControl object.

    Regards,
    Tasos
     
    Tasos Vogiatzoglou, May 23, 2006
    #2
    1. Advertisements

  3. Guest

    Tasos,

    What is an accessor? I told you I am new.

    Thanks,
    Al
     
    , May 24, 2006
    #3
  4. Guest

    Tasos,

    Ok, I know what an accessor is now. I set up the code below.

    Now I get the error:
    Object reference not set to an instance of an object.

    I thought I instanciated it. Code is below.

    Error is on this line: uc2.CompanyName2 = uc1.CompanyName1

    -------------------------------
    Public Class WebUserControl2
    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 WithEvents uc1 As WebUserControl1
    Protected WithEvents uc2 As WebUserControl2
    Protected WithEvents uc1TextBox1 As New
    System.Web.UI.WebControls.TextBox
    Protected WithEvents uc2TextBox1 As New
    System.Web.UI.WebControls.TextBox

    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    'Protected WithEvents uc1TextBox1 As New

    'NOTE: The following placeholder declaration is required by the Web
    Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Public Property CompanyName2() As String
    Get
    Return uc2TextBox1.Text
    End Get
    Set(ByVal Value As String)
    uc2TextBox1.Text = Value
    End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button1.Click

    uc2.CompanyName2 = uc1.CompanyName1
    End Sub
    End Class

    So what is wrong?

    It seems simple enough. Please help!

    Thanks,
    Al
     
    , May 24, 2006
    #4
  5. Al goodmorning,

    can you put a break point in that line and check which of two
    components are null ?

    Regards,
    Tasos
     
    Tasos Vogiatzoglou, May 25, 2006
    #5
  6. Guest

    Hi Tasos,

    It is CompanyName1 that is not declared.

    Al
     
    , May 25, 2006
    #6
  7. Al, can you also post the markup that you use to create the components
    ?

    Regards,
    Tasos
     
    Tasos Vogiatzoglou, May 26, 2006
    #7
  8. Guest

    Hi Tasos,

    I believe what you want is the code for the other user control, where I
    create the component. So the code follows. I hope this is what you
    want.

    Thanks,
    Al


    Public Class WebUserControl1
    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

    'NOTE: The following placeholder declaration is required by the Web
    Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Protected WithEvents uc1TextBox1 As
    System.Web.UI.WebControls.TextBox

    Public Property CompanyName1() As String
    Get
    Return uc1TextBox1.Text
    End Get
    Set(ByVal Value As String)
    uc1TextBox1.Text = Value
    End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub

    End Class
    --------------------------------------------
    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="WebForm1.aspx.vb" Inherits="testuc.WebForm1"%>
    <%@ Register TagPrefix="uc1" TagName="WebUserControl1"
    Src="WebUserControl1.ascx" %>
    <%@ Register TagPrefix="uc1" TagName="WebUserControl2"
    Src="WebUserControl2.ascx" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:panel id="Panel1" style="Z-INDEX: 101; LEFT: 275px; POSITION:
    absolute; TOP: 72px" runat="server"
    Height="50px" Width="259px">
    <uc1:WebUserControl1 id="WebUserControl11"
    runat="server"></uc1:WebUserControl1></asp:panel>
    <asp:panel id="Panel2" style="Z-INDEX: 102; LEFT: 277px; POSITION:
    absolute; TOP: 177px" runat="server"
    Height="35px" Width="363px">
    <uc1:WebUserControl2 id="WebUserControl21"
    runat="server"></uc1:WebUserControl2></asp:panel>
    </form>
    </body>
    </HTML>
    ---------------------------------------------
    <%@ Control Language="vb" AutoEventWireup="false"
    Codebehind="WebUserControl1.ascx.vb" Inherits="testuc.WebUserControl1"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <asp:TextBox id="uc1TextBox1" runat="server"></asp:TextBox>
    ------------------------------------------------
    <%@ Control Language="vb" AutoEventWireup="false"
    Codebehind="WebUserControl2.ascx.vb" Inherits="testuc.WebUserControl2"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <asp:TextBox id="uc2TextBox1" runat="server"></asp:TextBox>
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
     
    , May 26, 2006
    #8
  9. Al,

    from what I see (correct me if I am wrong) you are trying to access the
    UC1 component from inside UC2 component without referencing in the user
    control markup.

    This is why you get a null reference as the component is not
    initialized inside the UC2 component.

    To achieve what you want to do, you have to expose the onClick event
    from the UC2 and then wire them in the common parent (the page where
    both user controls reside).

    I hope this clarifies the issue.

    Regards,
    Tasos
     
    Tasos Vogiatzoglou, May 27, 2006
    #9
  10. Guest

    Tasos,

    I do not know what you mean in this paragraph:

    To achieve what you want to do, you have to expose the onClick event
    from the UC2 and then wire them in the common parent (the page where
    both user controls reside).

    How do I do that? How do I expose the onClick event?

    This program is just a test for a larger program that I am working on.
    I will have several user control where there will be textboxes for
    input. I put each user control in a panel on the main page. I want to
    make some visible while other are not visible at the same time. But I
    have to have some way to get the input from one user control to the
    other or to the main page. I took a class and the instructor said this
    would be the way to do it. I did try putting the button (onclick
    event) on the main page and it still got a instance error. I made this
    little test program to test this out.

    In class the instructor had a program very similar to this and it
    worked. I have been trying to get him to look at it also, but he has
    been busy. He teaches only as a sideline and has a full time job.
    Maybe he will eventually get back to me, but no promises. So I have
    been trying to solve this with you and you have been a big help so far.
    So just bear with me a little longer.

    I can sent a zip of the test program if you give me your email address.

    Thanks,
    Al
     
    , May 27, 2006
    #10
  11. charlisa outram, Jun 2, 2006
    #11
    1. Advertisements

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. Tom Rowton
    Replies:
    2
    Views:
    734
    Tom Rowton
    Aug 1, 2003
  2. qwerty
    Replies:
    3
    Views:
    9,371
    Scott Allen
    Sep 30, 2004
  3. Replies:
    0
    Views:
    487
  4. Leeor Chernov
    Replies:
    2
    Views:
    833
    Leeor Chernov
    Oct 16, 2005
  5. =?Utf-8?B?REo=?=
    Replies:
    2
    Views:
    508
    =?Utf-8?B?REo=?=
    Dec 1, 2005
Loading...

Share This Page