INamingContainer separator - sometimes _ sometimes :

  • Thread starter Thread starter Craig
  • Start date Start date
C

Craig

Hello

I've created a custom web control using the INamingContainer interface.
The custom web control has four textboxes and one button. When the button
is clicked, a new window is opened and a list is displayed. When an item
on the list is selected, the textboxes are populated and the new window
is closed.

This all works fine (using window.opener in javascript to reference the
textboxes from the new window), except that the client ID of the
textboxes changes on different pages from being mycontrol_mytextbox to
mycontrol:mytextbox (which breaks my javascript code, which is hardcoded
to put an underscore between the control name and the textbox name).

I figure I have a few approaches to solve this;
1. Find out if there is some way of knowning what separator has been
used.
or
2. Find some way to force the separator to always be one or the other.
or
3. Find a more reliable way to reference the textboxes from hte new
window.

I have to use INamingContainer because there could be more than one
control on the page and the new window has to know which control to
populate.

Thanks for your help.

Craig
 
Craig said:
Hello

I've created a custom web control using the INamingContainer interface.
The custom web control has four textboxes and one button. When the button
is clicked, a new window is opened and a list is displayed. When an item
on the list is selected, the textboxes are populated and the new window
is closed.

This all works fine (using window.opener in javascript to reference the
textboxes from the new window), except that the client ID of the
textboxes changes on different pages from being mycontrol_mytextbox to
mycontrol:mytextbox (which breaks my javascript code, which is hardcoded
to put an underscore between the control name and the textbox name).

I figure I have a few approaches to solve this;
1. Find out if there is some way of knowning what separator has been
used.
or
2. Find some way to force the separator to always be one or the other.
or
3. Find a more reliable way to reference the textboxes from hte new
window.

I have to use INamingContainer because there could be more than one
control on the page and the new window has to know which control to
populate.

Thanks for your help.

Craig

If you go through a Control in the debugger (remember to unfold until
Control), you will see that a Control have an ID_seperator and an
ID_render_seperator, which is for ID and ClientID only, not UniqueID (if I
recall correct). The following function (taken from my own code, but I
cannot remember exactly then I use it), can force an ID to render with Colon
as ID seperator (the function have some limitations I can see now, maybe
that is why I have declared it private). Also be aware that
getElementById("[id]") in IE will go through DOM a second round if not
finding an element substituting any underscore in [id] with colon, this is
not true in NN. BTW I will advise you never to hardcode ID's if not
necessary - if you have hardcoded ID's in javascript because you are not
emitting the javascript from your Control, you should consider changing the
model, so that javascript connected to the Control is also emitted from the
Control, that way your Control will be much easier reuseable.

private string IdSeperatorUnderscoreToColon(string IdSource)

{

//The first character is not an Id-seperator, only eventually following
underscores

string IdResult = IdSource;

int loopCounter = 0;

while (IdResult.IndexOf('_', 2) != -1 && loopCounter < 100) //setting a
loopCount just to be sure

{

int indexOf_ = IdResult.IndexOf('_', 2);

IdResult = IdResult.Remove(indexOf_, 1);

IdResult = IdResult.Insert(indexOf_, ":");

loopCounter++;

}

return IdResult;

}

Regards
Rasmus Rummel
www.menulab.com
 
Look at the following ASP.NET server side code:


Dim ExposeScript As String
ExposeScript = "<script language=JavaScript> function ExposeTextBox1() {" +
vbCrLf
ExposeScript += "//return document.getElementById('" + Textbox1.UniqueID +
"')}<" + vbCrLf
ExposeScript += "alert(document.getElementById('" + Textbox1.UniqueID +
"').value);}" + vbCrLf
ExposeScript += "</"
ExposeScript += "script>"
Page.RegisterClientScriptBlock("ExposeClientSideTextBox", ExposeScript)


This exposes the text box through clientside javascript, have the popup call
the function instead of the textbox directly.

- J
 
Back
Top