... clientid?
I have problems with javascript because asp.net messes with my clientids...
Also, is it possible to include whole script at once and not build it line by line?
What do you mean with including whole script at once? If you have a script you can throw it out with
Page.ClientScript.RegisterClientScriptBlock.
If you have it as external file then using RegisterClientScriptInclude (or RegisterClientScriptResource if its available as a web resource)
I have a text editor made with javascript (BBcode able etc).
My js is held in editor.js and consists of functions that operate on editor elements which are textarea, buttons, images, editboxes, divs...
So, to be able to use js for that I'm using unique names (ids) for me editor elements and/or divs; like "editarea" for textarea, "boldButt" for bold button control, etc.
In PHP, which I used to use, there would be no problem as I would simply include my .js up in the head and my editor control (actually a heap of controls editor is made of) and all would work fine.
In ASP.NET however, I'm facinga problem that being ASP.NET plays a wiseguy ^^ and renames my clientids to something of a nested naming style to ensure unique names.
Well, as I can't know how deep into a page my editor will be nested (as control should be usable everyhwere), I can't setup my js script with correct client ids so my scripts don't work as usually instead of just "boldButt" my button is, in final html, named
"$ctrl00$kfdjdvk3gbjdsg$hdgoudboldButt"...
I've seen javascript can be built line by line in codebehind, but for a 500-1000 lines js script, that can become a little bit tedious and messy least to say...
And yes, rewritting control aka overriding crossed my mind, but as on the link you gave author found out, this would create other problems.
I was wondering if there is a more elegant, official way to pass somehow real ids to javascript functions without dynamic script generation in cb.
Crossposted :-)
Ok, I've seen, but using CLIENTID property would imply creating javascript in C# code line by line which is what I want to avoid. I wan't resourced or at least external .js file...
Why cannot you create only those function calls(s) which just take the ID in. That way, you wouldn't need to output all but only but those function calls from the control.
E.g put static part of the script into separate js file (or resource as was in the link I gave), and only make the calls into those functions which require the ID (generally speaking set the ID).
If you expect your control to exist only once on Page, you certainly could output just a js variable where the client ID would be given (script library would use that then)
Hi,
Putting hardcoded IDs in the code is BAD and should be avoided.
As a intermediate code you can change your javascript code from using a hardcoded ID to a predefined variable.
So a function like this:
function foo()
{
textboxa.value = "bar";
}
Will become:
function foo()
{
m_Defined_Textbox.value = "bar";
}
And now the only thing you need to do is to render a script that will initialize m_Defined_Textbox to the client id of the textbox.
Hope this helps.
Like said earlier, there's no hardcoding, if one takes ClientID of server-side control and uses that to create the ID into generated script.
Taking previous example say you have TextBox on form
<asp:TextBox ID="TextBox1" runat="server" />
You could make a script as follows
function foo()
{
document.getElementById('<%=TextBox1.ClientID%>').value = "bar";
}
which would render the correct ID at client-side (client ID would change based on if control is placed inside a naming container such as a user control). Therefore creating js call is possible also in server-side code (this example was just inline script)
as TextBox is programmatically 100% available there (especially ClientID in this scenario)
monolithx
Member
495 Points
108 Posts
How to force...
Apr 27, 2006 03:17 PM|LINK
I have problems with javascript because asp.net messes with my clientids...
Also, is it possible to include whole script at once and not build it line by line?
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: How to force...
Apr 29, 2006 10:20 AM|LINK
Hi,
http://west-wind.com/weblog/posts/4605.aspx
What do you mean with including whole script at once? If you have a script you can throw it out with Page.ClientScript.RegisterClientScriptBlock. If you have it as external file then using RegisterClientScriptInclude (or RegisterClientScriptResource if its available as a web resource)
About the resource way: http://west-wind.com/weblog/posts/4310.aspx
Teemu Keiski
Finland, EU
monolithx
Member
495 Points
108 Posts
Re: How to force...
Apr 29, 2006 11:17 AM|LINK
I have a text editor made with javascript (BBcode able etc).
My js is held in editor.js and consists of functions that operate on editor elements which are textarea, buttons, images, editboxes, divs...
So, to be able to use js for that I'm using unique names (ids) for me editor elements and/or divs; like "editarea" for textarea, "boldButt" for bold button control, etc.
In PHP, which I used to use, there would be no problem as I would simply include my .js up in the head and my editor control (actually a heap of controls editor is made of) and all would work fine.
In ASP.NET however, I'm facinga problem that being ASP.NET plays a wiseguy ^^ and renames my clientids to something of a nested naming style to ensure unique names.
Well, as I can't know how deep into a page my editor will be nested (as control should be usable everyhwere), I can't setup my js script with correct client ids so my scripts don't work as usually instead of just "boldButt" my button is, in final html, named "$ctrl00$kfdjdvk3gbjdsg$hdgoudboldButt"...
I've seen javascript can be built line by line in codebehind, but for a 500-1000 lines js script, that can become a little bit tedious and messy least to say...
So, I need a way to make my scripts work... :-)
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: How to force...
Apr 29, 2006 11:23 AM|LINK
Ah,
you can always get the ASP.NET generated clientid by taking it from control's ClientID property. Does that help?
Teemu Keiski
Finland, EU
monolithx
Member
495 Points
108 Posts
Re: How to force...
Apr 29, 2006 11:24 AM|LINK
I was wondering if there is a more elegant, official way to pass somehow real ids to javascript functions without dynamic script generation in cb.
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: How to force...
Apr 29, 2006 11:25 AM|LINK
Teemu Keiski
Finland, EU
monolithx
Member
495 Points
108 Posts
Re: How to force...
Apr 29, 2006 11:33 AM|LINK
Ok, I've seen, but using CLIENTID property would imply creating javascript in C# code line by line which is what I want to avoid. I wan't resourced or at least external .js file...
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: How to force...
Apr 29, 2006 11:44 AM|LINK
That is the "official" way getting the ID.
Why cannot you create only those function calls(s) which just take the ID in. That way, you wouldn't need to output all but only but those function calls from the control.
E.g put static part of the script into separate js file (or resource as was in the link I gave), and only make the calls into those functions which require the ID (generally speaking set the ID).
If you expect your control to exist only once on Page, you certainly could output just a js variable where the client ID would be given (script library would use that then)
Teemu Keiski
Finland, EU
Ran Davidovi...
Member
64 Points
12 Posts
Re: How to force...
May 05, 2006 07:15 PM|LINK
Putting hardcoded IDs in the code is BAD and should be avoided.
As a intermediate code you can change your javascript code from using a hardcoded ID to a predefined variable.
So a function like this:
function foo()
{
textboxa.value = "bar";
}
Will become:
function foo()
{
m_Defined_Textbox.value = "bar";
}
And now the only thing you need to do is to render a script that will initialize m_Defined_Textbox to the client id of the textbox.
Hope this helps.
http://davidovitz.blogspot.com
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: How to force...
May 06, 2006 06:56 AM|LINK
Like said earlier, there's no hardcoding, if one takes ClientID of server-side control and uses that to create the ID into generated script.
Taking previous example say you have TextBox on form
<asp:TextBox ID="TextBox1" runat="server" />
You could make a script as follows
function foo()
{
document.getElementById('<%=TextBox1.ClientID%>').value = "bar";
}
which would render the correct ID at client-side (client ID would change based on if control is placed inside a naming container such as a user control). Therefore creating js call is possible also in server-side code (this example was just inline script) as TextBox is programmatically 100% available there (especially ClientID in this scenario)
Teemu Keiski
Finland, EU