client id mode on 2 identical id's - asp.net

I have an control1.ascx page
and control2.ascx page
both of them have this element:
<asp:TextBox runat="server" ID="txt_name" ClientIDMode="Static"></asp:TextBox>
page.aspx contains both of the controls.
so now the page contains input type text with the id "txt_name" X2.
I am wondering how it is working? can someone explain?

Ideally, you want to use ClientIDMode="Static" only if you are sure that no other control has same name in the page.
For example, you really want to access the ServerControl from external javascript file (althought it is not a good design).
If you are not sure, you want to use Predictable.

As your are using ClientIDMode="Static" thus control's id will be rendered exactly as it is.
ID will be rendered directly. See MSDN docs, this Blog is a good read.

ASP.Net 4+ supports various modes to generate ClientIDs for controls.
Here is a reference for MSDN on ClientID and its' generation modes: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid(v=vs.100).aspx
To see how ClientID generation works, you may want to refer to this link :
http://www.codeproject.com/Articles/108887/Client-Ids-Generation-with-ASP-NET-4-0
(It has visual explanations on how the ClientID generation works in different cases)
Hope this helps.

Related

ASP.Net - Naming Containers and ClientIDMode="Static"

MSDN documentation states with regard to the ClientIdMode:
Static The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as
the top of the hierarchy of naming containers for any controls that it
contains.
source: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode%28v=vs.100%29.aspx
However, i am not finding the "top of the hierarchy" business to be the case. For example, I have a usercontrol:
<uc1:WidgetsListControl runat="server" id="WidgetsListControl" ClientIDMode="Static" />
For good measure, I set the clientidmode in the control source as well although I'm not sure which one is needed:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WidgetsListControl.ascx.cs" Inherits="WebApplication1.WidgetsListControl" EnableViewState="false" ClientIDMode="Static" %>
Within the user control, I have a textbox:
<asp:TextBox ID="testTextBox" runat="server" />
My expectation is that the text box would be named something like WidgetsListControl$testTextBox
However what I find upon view source is:
<input name="ctl00$MainContent$WidgetsListControl$testTextBox" id="testTextBox" type="text"/>
What am I missing? Is there a way to achieve what I'm looking for (shorter ids) without setting 'static' on every control within the user control?
EDIT:
Actually after looking closer, I am finding that the ID attribute is working as described in the MSDN - however the name attribute is still the full concatenation of the naming-container hierarchy.
Given a site of high complexity, the names and IDs of these controls start to take up the majority of the bandwidth (markup size). I can't seem to find any good workaround to slim down the markup in this regard.
You must have this control within a Panel or something with runat="server" and the custom control itself is a naming container for its children. If you want identifiers to be how you name them with no change, you'll want to use the Predictable (IIRC) client id mode.
Note: though looking at the documentation for Predictable this doesn't seem to be the case, I'm sure it's what I've always used to get 'clean' .NET identifiers in ASP.NET.
Further, if you want to apply the ClientIDMode globally, then specify it in the web.config file in the attribute of the <pages> element.
ClientIdMode="Static" does work as described by Microsoft.
Make sure your looking at the id in the generated source, not the name.
That's the mistake I made.

Globalization difference in inline tags in ASP.NET

Is there any advantage/downfalls between using the inline write tag instead of the resource tag? Example:
<%=Resources.Site.SampleString %>
The resources tag (expression tag) as seen in any MSDN example:
<asp:Literal id="Literal1" runat="server" text="<%$ Resources:Site, SampleString %>" />
I find the first option far easier to use, and it has IntelliSense, but maybe it won't function the same?
These methods will function exactly the same. The latter simply calls first one; there is a reason why strongly-typed resources access code is being generated in the background. Therefore you can use whatever method you please.
By the way, there is also another way - by using meta:resourcekey attribute. So you should be able to write:
<asp:Literal id="Literal1" runat="server"
meta:resourcekey="SampleString" text="Default one" />
and it all should work exactly the same.
EDIT on implicit Localization.
What I forgot to mention, is that with meta:resourcekey certain conditions have to be met. The conditions are:
Values are taken from App_LocalResources, therefore related resource file need to exist
Related resource file name must be pagename.resx, for example: Default.aspx.resx, Default.aspx.es.resx
The resource file must contain keys in form of resourcekey.propertyname, for example SampleString.Text, SampleString.ID (although I wouldn't localize control ID's)
Because of how resources are generated, the key mentioned above must exist in invariant resource file (Default.aspx.resx), or it won't be localized.
I realised after some time that the <%=Resources.Site.SampleString %> method does not have designer support (which is understandable). This doesn't bother me, but it is a difference (for future readers).
So if you need or want designer support, the second option would be necessary.

ASP.net over ride client ID for form elements

Given a textbox:
<asp:Textbox runat="server" id="txtAddress1" />
This renders as something similar to:
<input name="ctl00$mainContent$txtAddress1" type="text" id="ctl00_mainContent_txtAddress1" />
I don't think browsers autocomplete features recognise this name/ID as a field they can autofill, they are not standard recognised names.
Is there any way to overide the client ID's so that autocomplete has a better chance of recognising them?
2 Points with this.
1) The "Override the Name" feature was introduced in ASP.Net 4.0, where for any property you can choose a hardcoded name instead of the dynamic name. You need to be careful on this as you don't want 2 objects sharing a name.
2) ASP.Net 2.0 and above (may have been in v1.0) has a property on the control called "AutoCompleteType" which provides a hint to the browser on what sort of information is required in the box.
Assuming you're using Asp.net 4.0, and you're aware of the points mentioned by DJIDave, you can use the ClientIDMode property on a control, and set it to 'Static'. Then, what ever you specify in the Id field in Asp.Net will be brought through to your final markup, and will not be 'mangled' (for want of a better word) by Asp.Net.

How to set specific ID for server controls in an ASP.NET Web Form that is using a MasterPage?

Is it possible to set a specific ID on an ASP.NET server control? Everytime I assign an ID and run the web form the ID changes.
For Example:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
Gets translated into this:
<input id="ct100_ContentPlaceHolder1_txtName" type="text" />
I think this is do to me using master pages, but if so how can I be sure a control will have a certain ID(for javascript purposes). I placed the auto-generated id in my javascript and it is working, but I would prefer to have used the id's that I originally assigned them. Is this possible?
(This is for version:ASP.NET 3.5)
Starting with .NET 4 you have greater control about how the client-side IDs look like (see this post for details).
To force a specific client-side ID, you have to set the ClientIDMode to static. The following will render an <input> element with id="txtName":
<asp:TextBox ID="txtName" ClientIDMode="static" runat="server"></asp:TextBox>
Although if you do this, you have to ensure that you don't have two controls with identical client-side IDs. Check the article linked above for other options.
This is the way web controls ID's are in .NET prior to version 4.0. Version 4.0 introduces client IDs, which you can read about here.
You can use somthing like this in your JS:
var something = '<%= txtName.ClientID %>';
You can use the Control.ClientID property in your codebehind to get the actual id after it's been added to the control tree.
Super annoying choice made by the asp.net webforms people.

Explicit localization problem

when trying to translate the confirmation message to Norwegian i get the following error:
Cannot have more than one binding on property 'OnClientClick' on 'System.Web.UI.WebControls.LinkButton'. Ensure that this property is not bound through an implicit expression, for example, using meta:resourcekey.
i use Explicit localization in the following manner:
<asp:LinkButton ID="lnkMarkInvoiced" runat="server" OnClick="lnkMarkInvoiced_OnClick"
OnClientClick="<%# Resources: lnkMarkInvoicedResource.OnClientClick%>"
Visible="False" CssClass="stdtext" meta:resourcekey="lnkMarkInvoicedResource" ></asp:LinkButton>
here's the local resource file entry:
<data name="lnkMarkInvoicedResource.OnClientClick" xml:space="preserve">
<value>return confirm('Er du sikker?');</value>
if i remove the meta attribute i get the English text(default).
how do i get the Norwegian text appearing without resorting to using the code behind?
Update:
removing the meta attribute prevents the exception from occurring but the original problem still exists. I can't get the Norwegian text to show.
only the default English text shows.
Another Update:
I know this question is getting old but i still can't get the Norwegian text to display.
If anyone has some tips please post a response.
Looks like you're making the problem harder by inlining the onclick. Why not split it out to a separate line?
<script>
function markInvoiced()
{
return confirm('<%= Resources.SomehowGet.lnkMarkInvoicedResource.OnClientClick%>');
}
</script>
<asp:LinkButton ID="lnkMarkInvoiced" runat="server" OnClick="lnkMarkInvoiced_OnClick"
OnClientClick="return markInvoiced();"
Visible="False" CssClass="stdtext" meta:resourcekey="lnkMarkInvoicedResource" ></asp:LinkButton>
And while we're looking at your code, you realize that you're essentially building an <a> tag, right? As such, why not just build an <a> and save yourself some grief?
And finally, next project why not ditch the built-in ASP.NET localization nighmare in favor of something sane like FairlyLocal, in which case you'd write this:
<a href="#" onclick="return confirm(<%=_"really?"%>) onserverclick="lnkMarkInvoiced_OnClick" runat="server">
<%=_("Mark Invoice")%>
</a>
Are you using the .NET resource manager and satellite assemblies to store your localized resources? It looks like you have hard-coded the alternative language in your markup, rather than storing it in a language-specific resources assembly...
.NET has some extremely rich localization and globalization capabilities. If you use them properly, localization should be a pretty automatic thing (assuming your client is providing their language code as part of the HTTP headers). Even if your client has not configured their browser with the appropriate language, it is still easy enough to manually change the UI culture via a user request (clicking a flag icon, configuring a setting, etc.)
This article might be helpful: ASP.NET Web Page Resources Overview
That meta tag is using implicit localization when you're using explicit localization in the OnClientClick. You will need to choose one method or the other. If you are to use explicit localization, you'll need to do the necessary work to set the proper culture info in your application in the code-behind.

Resources