473,651 Members | 3,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="Compa nies.ascx.cs" Inherits="Compa nies" %>
<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="#f fffff"
bgcolor="button face">
<asp:Label ID="lblCompanyN ame" runat="server" Text="Company
Name:" Width="150px" Font-Names="Arial"></asp:Label></td>
<td style="width: 318px; height: 6px;" bordercolor="#f fffff"
bgcolor="button face">
<asp:TextBox ID="txtCompanyN ame" runat="server"
CausesValidatio n="True" Width="220px"></asp:TextBox></td>
<asp:RequiredFi eldValidator ID="
RequiredFieldVa lidator1"
runat="server" ErrorMessage="M ust Enter Company Name"
ControlToValida te="txtCompanyN ame"></asp:RequiredFie ldValidator><td
style="height: 6px; width: 3px;" bordercolor="#f fffff">

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

</table>

May 23 '06 #1
10 1768
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.CompanyName 2 = uc1.CompanyName 1

-------------------------------
Public Class WebUserControl2
Inherits System.Web.UI.U serControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()

End Sub
Protected WithEvents uc1 As WebUserControl1
Protected WithEvents uc2 As WebUserControl2
Protected WithEvents uc1TextBox1 As New
System.Web.UI.W ebControls.Text Box
Protected WithEvents uc2TextBox1 As New
System.Web.UI.W ebControls.Text Box

Protected WithEvents Button1 As System.Web.UI.W ebControls.Butt on
'Protected WithEvents uc1TextBox1 As New

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

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

#End Region

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

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

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

uc2.CompanyName 2 = uc1.CompanyName 1
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.U serControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()

End Sub

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

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

#End Region

Protected WithEvents uc1TextBox1 As
System.Web.UI.W ebControls.Text Box

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

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

End Class
--------------------------------------------
<%@ Page Language="vb" AutoEventWireup ="false"
Codebehind="Web Form1.aspx.vb" Inherits="testu c.WebForm1"%>
<%@ Register TagPrefix="uc1" TagName="WebUse rControl1"
Src="WebUserCon trol1.ascx" %>
<%@ Register TagPrefix="uc1" TagName="WebUse rControl2"
Src="WebUserCon trol2.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1 </title>
<meta name="GENERATOR " content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.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:WebUserCon trol1 id="WebUserCont rol11"
runat="server"> </uc1:WebUserCont rol1></asp:Panel>
<asp:Panel id="Panel2" style="Z-INDEX: 102; LEFT: 277px; POSITION:
absolute; TOP: 177px" runat="server"
Height="35px" Width="363px">
<uc1:WebUserCon trol2 id="WebUserCont rol21"
runat="server"> </uc1:WebUserCont rol2></asp:Panel>
</form>
</body>
</HTML>
---------------------------------------------
<%@ Control Language="vb" AutoEventWireup ="false"
Codebehind="Web UserControl1.as cx.vb" Inherits="testu c.WebUserContro l1"
TargetSchema="h ttp://schemas.microso ft.com/intellisense/ie5" %>
<asp:TextBox id="uc1TextBox1 " runat="server"> </asp:TextBox>
------------------------------------------------
<%@ Control Language="vb" AutoEventWireup ="false"
Codebehind="Web UserControl2.as cx.vb" Inherits="testu c.WebUserContro l2"
TargetSchema="h ttp://schemas.microso ft.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

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

Similar topics

6
16860
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 store in an array then a file. Perhaps this is the wrong control to use as there seems no way of referencing each line of the text box.
0
1153
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 form/user control does NOT change the focus from that TextBox. Any subsequent Tab or mouse clicks will move the focus as expected. But not the first time any single TextBox is edited on the displayed form/user control. It's not that the carrot moves...
2
8387
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 submitting the form to update the database. The server doesn't have the client side value any more. It seems to me that as I begin to write the client side javacript code for form validation and client side editing capabilities in order to save...
0
1228
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 must do this exactly!)
3
1698
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 page with and button, and 5 <asp:TextBox>'s and when an user makes changes to each of the textbox content, javascript client side code is triggered to change the textbox background color property to some other color. My problem is when I click...
11
4539
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 opinion of how I want to handle the 'user experience' in the application I'm creating. While I know I could allow the user to enter in number and alpha text - in a text box - and then tell them when the execuate a command "This is not numeric data", I...
6
1726
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 account number. When a user enters an account number and then tabs or points to another field, I want the subroutine called so that it can access the database and get the data fill out six other fields. Nothing happens when I enter the account...
7
2890
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 enough memory' error whenever I scroll past N number of bytes in a textbox that has been filled with a lot of data. I am not sure what N is, but for a large chunk of data it occurs at about the halfway scroll, and smaller chunks might not throw...
6
2894
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") %>' Width="30px" ToolTip='<%# Eval("M1") %>'></asp:TextBox>
0
8277
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8803
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8581
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7298
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.