views:

612

answers:

2

This is for my Sharepoint edit form: I need to be able use the asp.net TextBox in my javascript:

<asp:TextBox runat="server" id="ff17{''}" text="{@Container_x0020_Qty}" __designer:bind="{ddwrt:DataBind('u',concat('ff17',''),'Text','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Container_x0020_Qty')}" />

I'm trying to read it in my javascript as such:

var CCID = document.getElementById("ff17");

This doesn't work...

However if I copy what the page renders as the form's Id:

var CCID = document.getElementById("ctl00_m_g_64200ded_b593_468b_bca5_0ea023581b8a_ff17");

This works!

Is there a way I can get this to work without having to always figure out the ct portion?

A: 

I'm not sure how sharepoint and bindings works. But in ASP.net you would do something like this.

var CCID = document.getElementById('<%= ff17.ClientID %>');

I'm not sure this will work, but it might point you in the right direction.

Oscar Kilhed
+2  A: 

I'm not sure how this will translate to sharepoint, but for normal asp.net pages I typically put a script like this in the head to get client ids:

<head runat="server">
    <script type="text/javascript" language="javascript">     
     //ClientIDs for controls in naming containers that we'll want to use from javascript
     var Controls = {
      'ff17':<%="'" + ff17.ClientID%>',
                'othercontrol':<%="'" + othercontrol.ClientID%>'};
    </script>
</head>

The reason I factor it out like this is to avoid needing to re-render the id everywhere I might want to use it, and because I can reference this from included script files without having to pass them through the asp.net processor. The Controls object is to avoid naming collisions (only one name to worry about).

The downside is the ugly string concatenation for the initial quote, because the simpler '<%=o.clientid%>' syntax doesn't work here. Also, this doesn't work as well with master pages, where the head is in the master page but the controls you care about are in a detail page. But you could certainly do this elsewhere in your page instead.

Joel Coehoorn
+1 This is better than peppering `<%= %>` throughout the rest of normal javascript code. Leaves the main body of Javascript purely static.
AnthonyWJones
I could never get this to work in sharepoint unfortunately :(
Alarius