NamingContainer and UserControl

  • Thread starter msmail.microsoft.com
  • Start date
M

msmail.microsoft.com

Hi,

UserControl inherits from TemplateControl which implements the
NamingContainer interface.

This causes all the controls inside a UserControl to have unique names. If
my project, this is Not A Good thing, as I need the names of the form
controls to be what I set them to be and not be changed.

Does anyone have any idea how to eliminate this behavior?

Thanks,
s
 
S

Sahil Malik

The UserControls and Controls within the UserControl follow a standard
naming algorithm. Can you get around this impasse by judging the name of the
child control within the usercontrol at runtime? In 99.999999% of the cases
you should be able to unless your problem is totally unique.

- Sahil Malik
You can reach me thru my blog http://www.dotnetjunkies.com/weblog/sahilmalik
 
S

Scott Allen

One technique is to generate your JavaScript from the code behind and
use a control's ClientID property to find the name the NamingContainer
will give to the control in the browser (I'm assuming JavaScript is
the problem?). You can inject JavaScript into a page from the code
behind using RegisterClientScriptBlock.

HTH,
 
S

Shahar Prish

Thanks for the advice guys, but..

The child-control my user-control has is a check box, and it's part of
a form. I can potentially have hundreds of these little buggers there.
Using javascript to scrape and clean the control seems like.. The
Wrong Thing To Do.

Maybe someone can think of a "right" way to do that?

Thanks!
s
 
S

Scott Allen

Hi Shahar:

I'm not sure what you want to do by "scrape and clean" the controls.

Using the ClientID property in server side code is the safest approach
and a well accepted practice in creating client side script that works
with INamingContainers (but then I'm still assuming it is JavaScript
that is causing you difficulties as you have not said otherwise). If
you can tell us what you want your application to do, we might be able
to offer better suggestions. There are usually two areas where the
unique names cause difficulties: 1) in writing client side script to
interact with the controls, and 2) using FindControl on the server
side.
 
S

Shahar Prish

Sorry for not being clear about this..

I am not levereging the full capabilities of ASP.NET - mainly not
using the same-page-postback mechanism, not using the view-state and
not using the ASP Session objects.

What I am doing, is that I dynamically generate a bunch of combo-boxes
(think hotmail email list, the little combos that allow you to delete
emails). I then use a method="post" and action="some_other_page.aspx"
to post the check-boxes that the user has selected. The way I am
currently doing it, is that each such check-box name is some kind of
ID that my software recognizes. These then get posted back (say, on a
submit) to that other page that knows how to process them gets them
and, for sake of argument, delete whatever objects they stand for.

If I use a user-control, and if there is no clean way of making it
preserve the names of the check-boxes, I need to do one of two things,
as far as I can see it:

1. Use java-script when, say, the page is done loading, and change all
the checkboxes IDs to something that does not have the prefix via the
DOM.

... or ..

2. On the some_other_page.aspx, I will need to do this translation of
the strings residing inside my Request.Form (actually, inside
Request.Form.Keys) object. Since ASP.NET does not have the same
control tree structure (and, since I am not using ViewState anyway
etc), it has no way of doing the translation for me.

If I could get ASP.NET to not touch these guys, I am scott-free - I
can do whatever I want in there.

s
 
S

Shahar Prish

Also, I am *not* using ASP.NET Controls for this, but rather the less
sophisticated HtmlControls namespace.
 
S

Scott Allen

Ah, ok. I think you may want to write a class derived from WebControl
and override the Render method. This would give you complete control
over the HTML.

As far as I know, there is no clean way to disable the unique
generation of client side IDs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top