473,671 Members | 2,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom Control ClientID weirdness

I'm encountering some weird behavior with the ClientID property. Let's
see if I can give you enough info and if anyone has ideas/suggestions.

I'm working in ASP.NET 2.0. I have a Web Project with a master page,
with a single ContentPlacehol der on it.
I have a page that uses that Master template, and is meant to place 2
controls of type Fred in the content placeholder. The .aspx for this
page goes like this:
[register tag prefix cc to grab custom controls from a separate
project]
....
<asp:Content ID="Content1"
ContentPlaceHol derID="contentP laceholder_Work area" runat="server">
<cc:Fred runat="Server" ID="fred_1" />
<cc:Fred runat="Server" ID="fred_2"></cc:Fred>
</asp:Content>

The Fred control is actually a custom control I defined as inheriting
from CompositeContro l. Fred overrides TagKey to return
HtmlTextWriterT ag.Div. It also overrides CreateChild Control to do a
few things, among which, it adds a System.Web.UI.W ebControls.Pane l
named Wilma to its Controls. When I add a Wilma to the controls, I add
an attribute to it like this:
myWilma.Attribu tes.Add("onclic k", "alert('"+this. ClientID+"');") ;

It all works quite nicely, and the Wilma's have the Freds' mangled Ids
(e.g.: onclick="alert( 'ctl00_contentP laceholder_Work area_fred_1')")
available.

However, if I put anything (and I mean anything, even a space
character, a newline character, ...) between the opening and closing
tags of a Fred control, like this:
<cc:Fred runat="Server" ID="fred_2"</cc:Fred>
Then Wilma's onclick event gets an un-mangled version of Fred's ID
(e.g.: onclick="alert( 'fred_2');" ).

It seems that I'm missing some crucial step/aspect that makes my
controls' Client IDs go out of sync.
I have checked fred_2's ClientID from the surrounding page Page_Load
method, and it is mangled at that time.
So, the difference is between the times when Page_Load and the
control's CreateChildCont rols methods run...

Any suggestion ?

Thanks in advance,
Frank

May 20 '07 #1
2 3136
On May 21, 3:23 am, Olorin <francesco.ri.. .@gmail.comwrot e:
I'm encountering some weird behavior with the ClientID property. Let's
see if I can give you enough info and if anyone has ideas/suggestions.

I'm working in ASP.NET 2.0. I have a Web Project with a master page,
with a single ContentPlacehol der on it.
I have a page that uses that Master template, and is meant to place 2
controls of type Fred in the content placeholder. The .aspx for this
page goes like this:
[register tag prefix cc to grab custom controls from a separate
project]
...
<asp:Content ID="Content1"
ContentPlaceHol derID="contentP laceholder_Work area" runat="server">
<cc:Fred runat="Server" ID="fred_1" />
<cc:Fred runat="Server" ID="fred_2"></cc:Fred>
</asp:Content>

The Fred control is actually a custom control I defined as inheriting
from CompositeContro l. Fred overrides TagKey to return
HtmlTextWriterT ag.Div. It also overrides CreateChild Control to do a
few things, among which, it adds a System.Web.UI.W ebControls.Pane l
named Wilma to its Controls. When I add a Wilma to the controls, I add
an attribute to it like this:
myWilma.Attribu tes.Add("onclic k", "alert('"+this. ClientID+"');") ;

It all works quite nicely, and the Wilma's have the Freds' mangled Ids
(e.g.: onclick="alert( 'ctl00_contentP laceholder_Work area_fred_1')")
available.

However, if I put anything (and I mean anything, even a space
character, a newline character, ...) between the opening and closing
tags of a Fred control, like this:
<cc:Fred runat="Server" ID="fred_2"</cc:Fred>
Then Wilma's onclick event gets an un-mangled version of Fred's ID
(e.g.: onclick="alert( 'fred_2');" ).

It seems that I'm missing some crucial step/aspect that makes my
controls' Client IDs go out of sync.
I have checked fred_2's ClientID from the surrounding page Page_Load
method, and it is mangled at that time.
So, the difference is between the times when Page_Load and the
control's CreateChildCont rols methods run...

Any suggestion ?

Thanks in advance,
Frank
Hi,

A asp,net controls client id is generated according to where the
control reside...
i.e. if i add the control in a panel and then add the panel in
usercontrol and at last in page
client id will be usercontrolclie ntid+panelclien tid
+controlclienti d.....
if i change my control from one panel to another panel it will change
accordingly...
so its better to generate any script in onrender method of control
life cycle...

Thanks
Masudur

May 21 '07 #2
On May 21, 2:36 am, Masudur <munn...@gmail. comwrote:
so its better to generate any script in onrender method of control
life cycle...

Thanks
Masudur
Thanks, Masudur, your hint helped me out and I was able to make the
Freds and Wilmas work out as intended.
I was assuming that the Render method in CompositeContro l would call
the CreateChildCont rols method (which I was overriding), but it turns
out that's not the case.

Thanks again,
~O.

May 21 '07 #3

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

Similar topics

7
7728
by: moondaddy | last post by:
I have a user control being used instead of a frame page. when the user clicks on a menu item I need to send the ID (integer value) of that menu as a parameter in the postback of the user control which will be used to query sql server to repopulate the datagrid in the user control. I also wrapped the user control in a panel element so I could position it on the page. I'm clueless on how to get started with this.
1
1698
by: Sanjay Pais | last post by:
I built a custom control for all the basic web.ui.controls like textbox, label, checkbox etc etc. I added my custom attribute called ApplySecurity to the html in the page. However, when I cycle through the controls on the page using this code, I cant seem to be able to access the Attribute collection. However, if I were to add the tag to a regular TextBox, the Attribute is available. My recursive function looks like this:...
1
1214
by: Jeffrey Frabutt | last post by:
Sorry about the repost... but my original post had funky characters that was causing it to get truncated. I've got a rather simple custom web control that has an object delcared as HtmlControls.HtmlInputHidden. I set the ID = 'PageNo' and the Name = 'PageNo'. However, when I look at the page source in the browser, the object's ID is "WebCtl3_PageNo" and the Name is "WebCtl3:PageNo". I'm reference the object in javascript as...
8
3764
by: david | last post by:
I have developed a web form by using visual Studio. My question is: (1) what is the problem? (2) what is right way to do it? In the form, there are labels with id: lblWear, lblColor, and lblQuality. Now I need to assign values to those label dynamically. I have JavaScript: function regTriples(id){ if (id==1){
1
1129
by: benoit | last post by:
What is wrong with this code? I get no errors on compilation of my assembly But On compilation on my Webpage i keep getting this error "Unable to find control id 'FreeBox1' referenced by the 'ControlToValidate' property of ''" public class FreeBox : TextBox{
1
365
by: Gonza | last post by:
Hi group, i'm trying to add a customvalidator control to a custom web control. The problem is i'm getting a "Unable to find control id..." exception. Here is the code: public class CuitTextBox : WebControl { protected override void OnInit(EventArgs e) {
6
393
by: Mr. SweatyFinger | last post by:
hi. i have a bunch of controls that get validated with the validation controls in vs2005 there are two that i could not find a control for so i wrote some javascript. so i set my submit button to OnClientClick="vRealtor()"
5
1518
by: TS | last post by:
I have a custom textbox that i need to access a label's text property. the label and textbox are 2 separate controls that will be on my page. Inside my custom control i want to have access to this label's text. I was thinking of having a property on my textbox that was something like LabelControlID. the textbox could then do a .Findcontrol on its parent control to find this label and access its text. i thought that this might be an...
3
415
by: shapper | last post by:
Hello, I have a custom control named Parent where I used the following: Protected Overrides Sub CreateChildControls() ... MyBase.Controls.Add(MyTextBox) MyBase.CreateChildControls() Me.ChildControlsCreated = True
0
8473
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
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
8911
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
8597
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
8667
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
7428
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...
0
5692
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
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2808
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.