all groups > dotnet jscript > february 2007 >
You're in the

dotnet jscript

group:

Finding elements in forms where a Master is being used


Finding elements in forms where a Master is being used MarcG
2/14/2007 12:01:05 PM
dotnet jscript:
Without Master pages, it was easy to find an element in a form with
JavaScript by using its name or ID.

With master pages, these get mangled, so when you create an input element
with runat=server and ID="myElement" it winds up being called something like
ctl00$ContentPlaceHolder1$myElement.

What is the proper way for my JavaScript code to find this thing? Is there a
way that I can prevent the name being mangled? I need to access the content
of the control in both JavaScript on the client and C# on the server.

RE: Finding elements in forms where a Master is being used stcheng NO[at]SPAM online.microsoft.com
2/15/2007 12:00:00 AM
Hello Joe,

Regarding on the Master page, since it is acting like a
template(usercontrol), it will need to divide the page into different
sections and each sections will be a Namingcontainer so that the controls
inside that section will has its own Naming convention. This naming
convention is very important for ASP.NET control structure and event
handling system to work well(correctly identitfy each control in diffferent
control hierarchy).

For your scenario, if you have javascript which want to reference some
nested controls in master page, you can consider the following approach:

In server-side code, use Control.ClientID property to get the ID it will be
rendered to client-side(represent its client-side id that can be used by
javascript). You actually use Page.ClientScript to register some javascript
functions to return these ClientIDs and use them by other scripts. e.g.

#register those functions that return the correct client id of those
controls
=========================
public partial class ContentPage1 : System.Web.UI.Page
{
.............................

protected void Page_PreRender(object sender, EventArgs e)
{
string script = "\r\nfunction get_TextBox1_id(){return '" +
TextBox1.ClientID + "';}";

script += "\r\nfunction get_TextBox2_id(){return '" +
TextBox2.ClientID + "';}";

script += "\r\nfunction get_Button1_id(){return '" +
Button1.ClientID + "';}";


Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"id_script", script, true);

}
}



#you can use these functions in page's other scripts(either defined in
master page or in content page)
=======================================

function test_client_id()
{
alert(document.getElementById(get_TextBox1_id()));
alert(document.getElementById(get_TextBox2_id()));
alert(document.getElementById(get_Button1_id()));

}
==================

Hope this helps you.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.

RE: Finding elements in forms where a Master is being used MarcG
2/15/2007 7:56:13 AM
Steven,

Ah! Now I remember. I looked at ClientID before.

The only problem is that the name segments are separated by underscores and
the actual ID's in the client are separated by "$"

There is a note somewhere that talks about how the nested names are
constructed (I do understand the naming container problem/solution) and it
specifically said that there was a separater and you SHOULD NOT ASSUME what
it was or that it would remain the same from version to version.

Like object hash codes, which are also not guaranteed to remain the same
from version to version, it is tempting to say "What the Hell" and make the
assumption any way.

What do you say? Assume "$" ? Is there a GetCUrrentVersionNameSeparater()
function in there somewhere??

Marc

[quoted text, click to view]
RE: Finding elements in forms where a Master is being used stcheng NO[at]SPAM online.microsoft.com
2/16/2007 2:13:27 AM
Hi Marc,

Thanks for your reply.

Yes, for the separator of the ID and ClientID, it is not recommend to
directly use a hardcode characte since it may change from version from
version(event different from different control adapters in asp.net). If
you do need to get this separator, there are two properties on the Page
class(also derived and override from Control class), they're "IdSeparator"
and "ClientIDSeparator", one of them is "protected", you may define a
custom utility class(dervied from page class) and expose these properties.
e.g.

public class PageUtil : Page
{


public string GetIdSeparator()
{
return this.IdSeparator.ToString();
}

public string GetClientIDSeparator()
{
return this.ClientIDSeparator.ToString();
}
}

Then, you can use it in your page code. e.g.

protected void Page_Load(object sender, EventArgs e)
{
PageUtil util = new PageUtil();

Response.Write("<br/>IdSeparator: " + util.GetIdSeparator());
Response.Write("<br/>ClientIdSeparator: " +
util.GetClientIDSeparator());
}

BTW, if you're usnig the "ClientID" Property, this value will help
automatically do the name mangling so that you can use it direclty in
client script.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.







RE: Finding elements in forms where a Master is being used stcheng NO[at]SPAM online.microsoft.com
2/19/2007 2:23:15 PM
Hello Marc,

Have you got any further ideas or progress on this issue? if there is
anything else we can help, please feel free to let me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
RE: Finding elements in forms where a Master is being used MarcG
2/21/2007 11:52:18 AM
Sorry Steven, I got lost for a bit and neglected to set the "Notify Me of
Replies" box.

That response was perfect. Thanks. A case of RTFM, I'm afraid, but so much
to read, so little time...

Thx
RE: Finding elements in forms where a Master is being used stcheng NO[at]SPAM online.microsoft.com
2/22/2007 1:49:36 AM
Thanks for your followup.

I'm glad that those suggestions help you.

Have a good day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
AddThis Social Bookmark Button