server control id's

N

Neil Stevens

Hi,

Merry Christmas to you all.

I have a problem with server controls, i am writing a C# ASP.NET web project
and i am creating custom server controls for some of the more common
element. One example is a NavigationMenu control or a Login control.

The problem is that when the page renders, say, the Login control, the ID of
the username field is changed from

id="username"

to

id="Login_ctl0__username"

or somthing like that...lol

Now, i have a generic javascript function

document.getElementById(username.ID).focus();

which sets focus to the specified control id and while the control is
rendered this javascript is added to the page using
Page.RenderStartupScript("focusScript", script).

This causes a javascript error because username.ID="username" when the page
is being rendered but by the time the script executes the username controls
id has changed to Login_ctl0__username.

Is there a way that i can prevent this from happening or a way that i can
get the correct id at the time my control is rendered.

Sorry about the length of this post, and hope someone can help.

Merry Christmas
Neil
 
P

Peter Rilling

On the server, there is a property called Control.ClientID. This allows the
server to know what ID will be given to the controls. You can use this to
dynamically generate the code that will be sent to the browser.
 
N

Neil Stevens

Thats still not right, I tried the ClientID, UniqueID and ID properties and
they all report a value of "username" for the username text box, but when
the page is rendered into the browser the id property is changed to
"__ctl0_username".

I write the javascript using the ClientID property but this fails because
the id that is rendered is different to the id that i set in the javascript.

Where does the __ctl0_ part come from?

Thanks in advance
Neil

Have a prosperous new year
 
W

William F. Robertson, Jr.

When are you trying to determine the ClientID? The Login__ctl0_ is being
appended to your control because it appears you are placing your control
inside another control that implements the INamingContainer. This is a
marker interface that will guarantee unique names as they are rendered to
the page.

You need to ensure you have added your control "username" to your containing
control (Placeholder?) and the page before trying to access the .ClientID
property.

bill
 
V

Vikesh Arora

Like in a datagrid , if you are defining column and placing a
textbox(runat=server) with name txt within it in a <TemplateColumn
HeaderText="Column1">
<asp:TextBox id='txt' runat=server ></TextBox>
</TemplateColumn>
then while rendering the control name is changed to grid$_ctl1.. something
like that.

All this is done to make every server control id unique, as ASP.NET every
control is distinguished by the ID only....
 

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,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top