How to force...

Last post 10-29-2006 7:53 PM by Moskie. 12 replies.

Sort Posts:

  • How to force...

    04-27-2006, 11:17 AM
    • Member
      495 point Member
    • monolithx
    • Member since 11-28-2005, 1:38 PM
    • Earth
    • Posts 108
    ... 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?

  • Re: How to force...

    04-29-2006, 6:20 AM
    • All-Star
      46,022 point All-Star
    • joteke
    • Member since 06-16-2002, 3:24 PM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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

    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: How to force...

    04-29-2006, 7:17 AM
    • Member
      495 point Member
    • monolithx
    • Member since 11-28-2005, 1:38 PM
    • Earth
    • Posts 108
    OK, ty, I will try to explain a bit more, sorry.

    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... :-)

  • Re: How to force...

    04-29-2006, 7:23 AM
    • All-Star
      46,022 point All-Star
    • joteke
    • Member since 06-16-2002, 3:24 PM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Ah,

    you can always get the ASP.NET generated clientid by taking it from control's ClientID property. Does that help?

    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: How to force...

    04-29-2006, 7:24 AM
    • Member
      495 point Member
    • monolithx
    • Member since 11-28-2005, 1:38 PM
    • Earth
    • Posts 108
    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.

  • Re: How to force...

    04-29-2006, 7:25 AM
    • All-Star
      46,022 point All-Star
    • joteke
    • Member since 06-16-2002, 3:24 PM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
    See my previous reply. :-)
    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: How to force...

    04-29-2006, 7:33 AM
    • Member
      495 point Member
    • monolithx
    • Member since 11-28-2005, 1:38 PM
    • Earth
    • Posts 108
    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...

  • Re: How to force...

    04-29-2006, 7:44 AM
    • All-Star
      46,022 point All-Star
    • joteke
    • Member since 06-16-2002, 3:24 PM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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)

    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: How to force...

    05-05-2006, 3:15 PM
    • Member
      64 point Member
    • Ran Davidovitz
    • Member since 04-14-2006, 2:20 PM
    • Israel
    • Posts 12
    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.
    -Davidovitz
    http://davidovitz.blogspot.com
  • Re: How to force...

    05-06-2006, 2:56 AM
    • All-Star
      46,022 point All-Star
    • joteke
    • Member since 06-16-2002, 3:24 PM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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)

    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: How to force...

    05-06-2006, 4:00 AM
    • Member
      495 point Member
    • monolithx
    • Member since 11-28-2005, 1:38 PM
    • Earth
    • Posts 108
    Thank you both for your advice...
  • Re: How to force...

    10-29-2006, 6:53 PM
    • Member
      30 point Member
    • Moskie
    • Member since 05-17-2005, 9:11 AM
    • San Francisco
    • Posts 9
    I'm using the method described in that blog to force the IDs to be what I want, but I've encountered another problem that I think might a be a result of using this method.

    I've created a class that inherits from HtmlGenericControl. It overrides UniqueID and ClientID so that they both return this.ID. It also has contructors in the class that call the base class' constructors... and that's all that I have in the class.



    Can someone confirm (or refute) this for me? I'm thinking there's something else I have to have in my class, but I'm not sure what that is. Any help is appreciated!

    This is with .Net 2.0.

  • Re: How to force...

    10-29-2006, 7:53 PM
    • Member
      30 point Member
    • Moskie
    • Member since 05-17-2005, 9:11 AM
    • San Francisco
    • Posts 9

    Whoops, sorry, part of the text I supplied didn't show up in my post. Here's what I meant to say: 

    I'm using the method from the blog to force the IDs to be what I want, but I've encountered another problem that I think might a be a result of using this method.

    I've created a class that inherits from HtmlGenericControl. It overrides UniqueID and ClientID so that they both return this.ID. It also has contructors in the class that call the base class' constructors... and that's all that I have in the class.

    But now I'm getting an error when I try to view the Trace page of a page that uses objects of this type. The error is a Null Reference Exception that occurs in System.Web.Handlers.TraceHandler.CreateControlTable(DataTable datatable) +2442. I'm not 100% sure that using my custom class is causing the problem, but that's my suspicion.

    Can someone confirm (or refute) this for me? I'm thinking there's something else I have to have in my class, but I'm not sure what that is. Any help is appreciated!

Page 1 of 1 (13 items)