3

I have a page where it is getting overwhelmed with code like:

var textBox = $get("<%=textState.ClientID%>");

This requires me to have my JavaScript inside the page instead of nicely tucked in a js file. Is there a better approach to this?

document.getElementById doesn't work because then I end up with code like this:

var textBox = document.getElementById("originDestinationControl_textState");

or

var textBox = document.getElementById("ctl00_ContentPlaceHolder_originDestinationControl_textState");

depending on where I am referencing these controls (inside master pages and/or usercontrols)

4 Answers 4

3

I normally stick something like this into pages where I want to use a separate js file.

<script type="text/javascript">
    var pageNameElements = {
        textbox : '<%= textbox.ClientId %>',
        button : '<%= button.ClientId %>'
    };
</script>

This way you get a nice javascript object with all the control ids that you can use in your js file like this.

$('#' + pageNameElements.textbox)

or

document.getElementById(pageNameElements.textbox)

if you're not using jquery.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually this isn't working for me. I'm getting null JS File: var myTextBox = document.getElementById(pageOriginElements.textOriginMultiState); alert(myTextBox); ASCX: <script type="text/javascript"> var pageOriginElements = { textOriginMultiState : '<%=textOriginMultiState.ClientID%>', textOriginMultiStateCountry : '<%=hiddenOriginMultiStateCountry.ClientID%>' };
Make sure the function in the JS file is getting executed after the object containing the element-ids has been created, i.e. add the js file just before the </body> tag, or use the jQuery $(document).ready() function to execute it when the dom is ready.
2

Might I suggest learning jQuery? Since I started using it I have never once had to deal with messy asp tags to get at controls on the page.

var textBox = $get("<%=textState.ClientID%>");

would look something like

var textBox = $("input[id$='_textState']");

in jQuery. And even better, you can place it into it's own js file!

Comments

1

With .NET 4.0 you actually have total control over this:

http://www.codeproject.com/KB/aspnet/ASP_NET4_0ClientIDFeature.aspx

It has hit Release Candidate along with Visual Studio 2010. I know this isn't an ideal solution, but it is one.

http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx

Comments

0

I think you're stuck.

Your textState.ClientId code needs to stay on the aspx page itself. This is because it's rendered client side. JavaScript include files are downloaded separately from the rest of the page, so it won't know how to handle it.

If you need to clean this up, you could try using classic asp style include files. However, in asp.net they end up adding more of a mess than they fix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.