Using the ASP.NET ClientServerManager method or property to get name of control - asp.net

Does the ASP.NET ClientServerManager provide some method or property to return the name of an ASP.NET control in the generated html page that could be used to write the javascript (using RegisterClientScriptBlock) in the code-behind? The actual generated control names can be quite long and unknown (I am also using master pages). I would like a generic way to write the javascript text and have the actual names of the controls be added to the javascript string. I would expect some method that I pass in the name of the control and it returns the actual html control name. I have searched in the documentation of the ClientServerManager and could not find anything.

Control.ClientID is rendered as html tag ID, so you can use that property.

If you are worried about control names, check out ClientIdMode where you can specify this and make it much simpler.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx
Would this help your issue?

Related

Custom control names (not ID) in ASP.NET to use with JQ.serialize

I am developing a website using mainly AJAX for saving and retrieving data in order to avoid postbacks (or at least full postbacks).
I "prepare" the page in codebehind, and on client I use JQ and Javascript.
When I want to save data I serialize the elements in a container (using JQ) and then AJAX post to send data to the WebMethod.
This is working well but I have to deal with long name elements ("ctl00$MainContent$grdEmployees$ctl03$ddlRole").
Sometimes is difficult to retrive data from the NameValueColletion as the same WebMethod can be called from different pages, so the "name" of the element is not the same as the control may be nested in other container.
Is there a way to set custom names for ASP.NET controls to avoid this issue?
Or other way to achieve this behavior?
Best regards.
Have you tried to use ClientIDMode="Static" which is found in .NET 4.5.
When a page is render the ids all change as you have noted. However, you set the ClientIDMode to static it will render with the ID that you have put for the control itself. That way you can work with the ID of the control instead of the generated id. If you would like to know more here is a like to the MSDNA about ClientIDMode
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode(v=vs.110).aspx

ASP.NET long Name attribute - Why is there no ClientNameMode

In ASP.NET much has been made about the ClientIdMode property that gives developers greater control over the a control's ID attribute as it appears in the HTML.
However little attention appears to have be paid to the way the controls render their Name attribute. It appears to be a simple concatenation of the control's ID and its hierarchy of naming containers.
Given a sufficiently complex web page these names get very long. They not only make the HTML payload big (and ugly) but are also posted back to the server on every postback. (Also, they make their way into the Control State of some third party control suites. )
Why isn't there a ClientNameMode property - or similar? Surely it is as important as the Id attribute? Is it possible to override some method that generates / rehydrates the Name attribute so that we can man handle it to maybe match the Id? (made shorter by the ClientIdMode)
An example of a name of a control on a page that I am working on is
USoWAR1_tabContainer_UDetailsTabContainer_tabContainer_UDetailsTab_UDetails1_UDueDateAndNotifications1_decDetail_DataEntryRow1_datDueDate_DateTimePicker_calendar_AD
As far as I know the only way to alter this functionality is to extend controls into your own and override UniqueID property (e.g. by returning Server-side ID).
I had this same issue, and had to use JS to set the attr after loading.
$('#idofdomobjiwanttoname').attr("name", "whatIWantToNameIt");

How to set a custom attribute to asp.net literal and read it in code behind?

How to set a custom attribute to asp.net literal and read it in code behind? There's no Attributes collection.
A Literal isn't a HTML element, it literally is a literal (sorry for that). Its content becomes verbatim output in the response so there are no attributes to pull.
Attributes on ASP.NET controls are properties of the control class so you could try descending from Literal and defining your own property.
Just because some may map to HTML attributes is purely chance as they're only relevant to the server when rendering the page usually, the control dictates what output actually happens and how the properties map to the attributes.
If you want a generic HTML element you can try HtmlGenericControl which should offer the basic HTML properties for you to play with.

How does ASP.NET Webforms decide HTML name of a control?

In ASP.NET web forms when a control is rendered on the page, the ID of each field is modified with the ctrl01 as needed to ensure collisions don't happen (aka myField becomes ctrl01$myField or whatnot).
I was wondering where one might find details on HOW the algorithm itself works, or where it might be I can find it. I am assuming the INamingContainer has something to do with this-- but alas I cannot find the part which actually decides the rendered field name.
Any help would be awesome!
You are probably looking for this msdn article.
It's based on the hierarchical layout of the webpage. But you can control this with the ClientId property.
So a textbox in a usercontrol will be named ctrl01#textboxname (Like you said in your post)
It concatenates it's own name with your original id.
In ASP.NET 4 you can suppress this concatenation and keep your own id in three different ways:
Each server control has an attribute called clientIdMode which you can set to Static
You can also set clientIdMode in the page directive which will affect all controls on the page.
Or you can set it in the web.config to affect all controls in all pages. (Unless the page or control is set to a different clientIdMode
Note: If you are using AJAX Control Toolkit you will need to set those controls that are part of the toolkit to a clientIdMode of Predictive
Apart from the other answers, if you are using ASP .NET 4, you have much more control over it.
Take a look # these web pages
http://www.west-wind.com/weblog/posts/54760.aspx
http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx

Access hiddenfield using Jquery

I have a page that's derived from a master page. On this page, I have a hiddenfield ("hfUser"). How can I access this "hfUser" control and get/set its value using JQuery?
I've tried variants of this:
$(document).ready(function() {
var test = $("#hfUser").val();
alert(test);
});
but test = undefined. I'm guessing I've got the selector wrong, but I don't know how to get an asp hiddenfield. Any ideas?
Thanks
If you are using Asp.net controls, the server will mangle the control ids. It adds a bunch of extraneous control tree hierarchy information into the id. You need to reference what that acutal id is that's getting rendered, which is availble with the ClientID property on the control (hfUser.ClientID) or access your control in a different, more roundabout way, like find the controls parent, then search through its children to find your control.
If you don't have to use the asp.net HiddenField control, try just using a regular old html input.
ASP does like to mangle ID's. The further down the rabbit hole (or nesting controls) you go, the more ASP adds to your control ID. Throw in Master Pages, and it's yet another level or two.
Another way to access server-side controls (with the runat property set), is to use the square brackets in your jQuery selector.
Like this:
$("[id$='hidImgSource']").val()
That selects any elements whose ID has 'hidImgSource' as ending part of the name. So it will find mangled ID's.
Here is a link to the jQuery Selectors page that explains some more options.
If the hidden field is an ASP.NET control, check out this blog post to help you with jQuery selectors for ASP.NET controls
http://www.foliotek.com/devblog/extending-jquery-to-select-asp-controls/
Do it like this:
$(document).ready(function()
{
var test = $("**#<%= hfUser.ClientID %>**").val();
alert(test);
});

Resources