473,406 Members | 2,467 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,406 software developers and data experts.

Referencing subcontrols within UserControl

Does anyone know how to reference a subcontrol of a UserControl within
client-side script. For example, if I have a TextBox as one of the elements
of my UserControl with an id of "MyTextBox", how do I reference it within
some jscript function. If I try to just reference it as MyTextBox I get an
error saying that MyTextBox is undefined.

From looking at the generated source, I believe that there are two issues
involved. One is that the subcontrol's ID is getting renamed to something
like Foo1_MyTextBox, where Foo is the name of the UserControl. The second
issue is that the reference seems to need to be qualified by it's parent
form's ID, e.g. Form1.Foo1_MyTextBox. If I reference the TextBox that way
it works, but there are a couple of problems, First, I can't be sure what
the renaming be. I assume that if I have more than one instance of the
UserControl on a form that the prefix will change. The second problem is
that I don't know what the ID if the containing form is going to be so I
can't know how to qualify the TextBox's name.

Thanks in advance.

-Mark
Nov 17 '05 #1
5 2177
Use the ClientID property to get the name of the control in the resulting
HTML.

--
James J. Foster, DotNetCoders
http://www.dotnetcoders.com
"Mark Friedman" <bi******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Does anyone know how to reference a subcontrol of a UserControl within
client-side script. For example, if I have a TextBox as one of the elements of my UserControl with an id of "MyTextBox", how do I reference it within
some jscript function. If I try to just reference it as MyTextBox I get an error saying that MyTextBox is undefined.

From looking at the generated source, I believe that there are two issues
involved. One is that the subcontrol's ID is getting renamed to something
like Foo1_MyTextBox, where Foo is the name of the UserControl. The second
issue is that the reference seems to need to be qualified by it's parent
form's ID, e.g. Form1.Foo1_MyTextBox. If I reference the TextBox that way
it works, but there are a couple of problems, First, I can't be sure what
the renaming be. I assume that if I have more than one instance of the
UserControl on a form that the prefix will change. The second problem is
that I don't know what the ID if the containing form is going to be so I
can't know how to qualify the TextBox's name.

Thanks in advance.

-Mark

Nov 17 '05 #2
I take your response to refer to client-side script that would be generated
by my UserControl, but what I'd really like to do is just write the script
code directly in the page. Is there a way to do what I want without
generating the script in the control?

-Mark

"James J. Foster" <ja***@dotnetcoders.com> wrote in message
news:uc**************@tk2msftngp13.phx.gbl...
Use the ClientID property to get the name of the control in the resulting
HTML.

--
James J. Foster, DotNetCoders
http://www.dotnetcoders.com
"Mark Friedman" <bi******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Does anyone know how to reference a subcontrol of a UserControl within
client-side script. For example, if I have a TextBox as one of the

elements
of my UserControl with an id of "MyTextBox", how do I reference it within some jscript function. If I try to just reference it as MyTextBox I get

an
error saying that MyTextBox is undefined.

From looking at the generated source, I believe that there are two issues involved. One is that the subcontrol's ID is getting renamed to something like Foo1_MyTextBox, where Foo is the name of the UserControl. The second issue is that the reference seems to need to be qualified by it's parent
form's ID, e.g. Form1.Foo1_MyTextBox. If I reference the TextBox that way it works, but there are a couple of problems, First, I can't be sure what the renaming be. I assume that if I have more than one instance of the
UserControl on a form that the prefix will change. The second problem is that I don't know what the ID if the containing form is going to be so I
can't know how to qualify the TextBox's name.

Thanks in advance.

-Mark


Nov 17 '05 #3

<script>
var ctl = document.getElementById('<% =
FindControl("myusercontrol").FindControl("mysubcon trol").ClientId %>');
</script>

"Mark Friedman" <bi******@yahoo.com> wrote in message
news:uy*************@TK2MSFTNGP12.phx.gbl...
I take your response to refer to client-side script that would be generated by my UserControl, but what I'd really like to do is just write the script
code directly in the page. Is there a way to do what I want without
generating the script in the control?

-Mark

"James J. Foster" <ja***@dotnetcoders.com> wrote in message
news:uc**************@tk2msftngp13.phx.gbl...
Use the ClientID property to get the name of the control in the resulting
HTML.

--
James J. Foster, DotNetCoders
http://www.dotnetcoders.com
"Mark Friedman" <bi******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Does anyone know how to reference a subcontrol of a UserControl within
client-side script. For example, if I have a TextBox as one of the

elements
of my UserControl with an id of "MyTextBox", how do I reference it within some jscript function. If I try to just reference it as MyTextBox I get an
error saying that MyTextBox is undefined.

From looking at the generated source, I believe that there are two issues involved. One is that the subcontrol's ID is getting renamed to something like Foo1_MyTextBox, where Foo is the name of the UserControl. The second issue is that the reference seems to need to be qualified by it's
parent form's ID, e.g. Form1.Foo1_MyTextBox. If I reference the TextBox that

way it works, but there are a couple of problems, First, I can't be sure what the renaming be. I assume that if I have more than one instance of the
UserControl on a form that the prefix will change. The second problem is that I don't know what the ID if the containing form is going to be so I can't know how to qualify the TextBox's name.

Thanks in advance.

-Mark



Nov 17 '05 #4
Thank you very much.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #5
Thank you very much.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #6

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

Similar topics

7
by: Martin Schulze | last post by:
Hello, i tried to compose myself a custom usercontrol which is derieved from System.Windows.Forms.UserControl. It contains 2 comboboxes and one textbox (which are also custom controls, but...
3
by: Beeeeeves | last post by:
If I have a project with a component that is derived from a control, say RichTextBox, I insert this control into a form by placing a standard RichTextBox on the form, and then doing a find and...
3
by: gary | last post by:
Hi, I am trying to reference an anchor in a user control with a url. This worked in 1.1 but no longer works in 2.0. The ascx control is located in a "/include" folder If you have a...
2
by: =?Utf-8?B?SmFtZXMgUGFnZQ==?= | last post by:
Hi all I have a user control - mycontrol.ascx with various controls within it. mycontrol.ascx is utilised in myPage.aspx I have a class (class1) to carry out various functions, subs etc. How...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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
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...

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.