473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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 '06 #1
10 1755
You have to put an accessor in your user control code behind to have
access to the textControl object.

Regards,
Tasos

May 23 '06 #2
Tasos,

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

Thanks,
Al

May 24 '06 #3
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 '06 #4
Al goodmorning,

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

Regards,
Tasos

May 25 '06 #5
Hi Tasos,

It is CompanyName1 that is not declared.

Al

May 25 '06 #6
Al, can you also post the markup that you use to create the components
?

Regards,
Tasos

May 26 '06 #7
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 '06 #8
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

May 27 '06 #9
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 '06 #10


if it is already one user and you want two how do you get it

*** Sent via Developersdex http://www.developersdex.com ***
Jun 2 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Gray | last post by:
Greetings all, I'm working on a program that allows a user to enter notes in a multiline textbox. I would like to be able to read the contents of the textbox (as records - one per line) and...
0
by: SpotsPlay | last post by:
If I bind a TextBox to a property on a "BusinessObject", the first time that TextBox is edited (each time the form/user control is opened) hitting Tab or clicking on another control on the...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
0
by: Dot net work | last post by:
Hi, Make up a very simple project as follows: 1 aspx form 3 web user controls (referred to as A, B, and C) "A" web user control: Put 1 textbox and 1 button on this web user control. (You...
3
by: Henry | last post by:
Hi. I've also posted this at another discussion board and here is the original question. ------------------------- "I have this problem and I don't know what I can do. First of all, I have a...
11
by: Keith | last post by:
I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely reading them all - but I think I have a differing...
6
by: Shelly | last post by:
In looking at the asp.net TextBox, I noticed that there is a property called "OnTextChanged". I tried to use this control to call a subroutine called "GetCompanyProperties". The field is the...
7
by: robert.waters | last post by:
I have an Access database frontend linked via ODBC to a large (gigabytes) mysql database. I need to view a large amount of data in a a textbox (variable up to 300K), but I receive a 'there isnt...
6
by: john | last post by:
I have the following textbox setup with Text & ToolTip Bindings as follows; I'm using Visual Studio 2008 VB: <asp:TextBox ID="txtDay1" runat="server" Text='<%# Eval("Day1") %>'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.