Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / JScript / February 2007

Tip: Looking for answers? Try searching our database.

Finding elements in forms where a Master is being used

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MarcG - 14 Feb 2007 20:01 GMT
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.

Thanks
Steven Cheng[MSFT] - 15 Feb 2007 07:31 GMT
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.
MarcG - 15 Feb 2007 15:56 GMT
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

> Hello Joe,
>
[quoted text clipped - 86 lines]
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
Steven Cheng[MSFT] - 16 Feb 2007 02:13 GMT
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.
Steven Cheng[MSFT] - 19 Feb 2007 14:23 GMT
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.
MarcG - 21 Feb 2007 19:52 GMT
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
Marc
Steven Cheng[MSFT] - 22 Feb 2007 01:49 GMT
Thanks for your followup.

I'm glad that those suggestions help you.

Have a good day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.