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

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.

Related

Dynamically change ASP.Net WebForms Control IDs While Maintaining State

We're working with a third party system and we need to modify some ASP.Net controls so that they have a specific token added to their HTML id attribute (so the third party system can identify them from the request), as well as HTML comments before and after the controls' output.
Basically, we need to be able to take say a TextBox (say for "Employee First Name") and dynamically change its output to something like this:
<!-- FIZZBOT_START -->
<input id="EmployeeFirstName_FIZZBOT_" ... />
<!-- FIZZBOT_END -->
We would ideally like to implement this so that the developers only have to do something like this:
<xxx:FizzbotWrapper ID="MyWrapper" runat="server">
<asp:TextBox ID="EmployeeFirstName" runat="server" />
</xxx:FizzbotWrapper>
Is there any way to get such an interface to produce such output, while still being able to:
maintain control state across requests
work properly with Validators
work properly with UpdatePanels
allow developers to refer to the original ID in the codebehind for readability (instead of having to refer to "EmployeeFirstName_FIZZBOT_"
work with out-of-the box ASP.Net controls like TextBox and not just controls we've derived.
I've tried a few ways of doing this (the technique that was the most promising was this, but I ran into some trouble getting it to work with validators, and it wouldn't work at all with controls we don't have the source code to).

client id mode on 2 identical id's

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.

Asp.Net page rendering and UI controls modularity

the website I'm currently developing can display dynamically-built forms.
A form is composed of fields, which are created directly by the users and can be displayed as one of multiple types that we support (text box, list box, tickbox, radiobuttonlist etc.). The rendering logic uses a repeater that iterates over a collection of all the fields defined by the user.
Inside the repeater (directly in the aspx page), one instance of each of the types we support is defined.
<asp:Repeater ID="fieldRepeater" runat="server">
<ItemTemplate>
<asp:TextBox ID="textBox" runat="server" />
<asp:DropDownList ID="dropDownList" runat="server" />
<asp:CheckBox ID="checkBox" runat="server" />
<asp:RadioButtonList ID="radioButtonList" runat="server" />
[...]
</ItemTemplate>
</asp:Repeater>
During the loading, we figure out which control is required and actively hide all the other ones.
Being still new to the web based development world, this approach seems very odd to me. My guts would prefer keeping the UI clean and instanciate exactly the controls that are required in CodeBehind and not start "playing" with visibility... but the current approach has some obvious benefits as well.
Is it really how one would do it in a web app?
Are there some best practices here?
Thanks!
I have no idea what the best practice is here, but I have done something similar before in a previous project and tried both approaches. Both will work.
Creating the controls in code-behind can be fiddly, especially if you are having to deal with post-backs. The controls have to be created in OnInit, as otherwise they won't get the posted form values and viewstate populated. This will cause complications if any of the control creation is based on the values of other controls, as you won't known the values without manually delving into the posted form values.
The only practical disadvantage with your current approach that I can think of is that all four controls (TextBox, DropDownList etc.) have to be instantiated and processed server-side by ASP.Net, which is a bit of a waste of resources. But it's probably not too significant; maybe do some profiling to see. I do agree that it seems a bit odd though, it doesn't feel very "clean".
As you said you're new to web development, then I would recommend continuing with your current approach of including all the controls and hiding the irrelevant ones. I just found it simpler when I did, even though it may not seem as nice.
Good luck!

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.

Simple HTML construction in ASP.NET?

A simple question, I think:
I want to put a tag into an ASP.NET app I've been asked to maintain, so I'm coming at this from a newbie point of view just tinkering around the edges without knowing a lot.
I wrote an old ASP application back in 1998, so I am just running on memory...
How do I write some output to the webpage?
I know I can use an
<asp:label id="blah">
but then I need to define a Label blah; in my code behind and then assign it.
I believe that I can put in-place:
<% Response.Write("sometext"); %>
and that will write sometext in the location within the page. (Am I correct?)
Lastly, I remember there was a syntax to the effect of
<%= "some string" %>
but I can't find the documentation on it, to say either it is deprecated, unadvised, or the rationale for such a decision.
I have tried googling for "ASP.NET grammar" but I can't even find a good description that "<%=" even exists, though it is mentioned in a few blogs.
For something simple, like inject the global version number, or the current date, then I can't see anything particularly wrong with in-place composition - it would save me defining 15 labels and having to initialise them all - though perhaps the asp:label approach could reference one global instance of a label?
Just asking for opinions on good practices :)
<%= string %> is perfectly valid ASP.NET syntax. The reason you will often find references to problems with using that is people use <%= (equivalent to Response.Write) when they should use <%# (for databinding) or vice-versa.
For example, we use it very extensively in our content managed site, where we pull in values from a global settings repository:
<%= SiteContext.Current.GetSetting("SiteTitle") %>
MSDN:
MSDN entry on <%= (this is under the JScript.NET section but still applies)
MSDN entry on <%#
Some others suggest <%= is not a "best practice" or a very good approach, but I strongly disagree with that sentiment. For an MVC-ish type site (especially a site that is template- or view-driven in some way), the most direct approach is frequently more effective than using server controls.
Just be mindful that when you use an <asp:Label /> it renders the .Text inside the <span> tag whereas an <asp:Literal /> adds no extraneous HTML to the string passed to it.
For example, if you were building a content management system and wanted to display user-driven HTML, a Label control would not correctly display the output from a WYSIWYG type rich textbox whereas a Literal control is the appropriate choice.
The <%= %> is the late-bound equivalent of the Literal's .Text property. The only difference here is when the value is placed in the page (aside from obvious syntax and separation of concerns paradigm) during the course of the page lifecycle.
Since the .Text property is on a control inherited from WebControl, it can be set/read/manipulated during any of the events following the control's Load event (wherever/whenever you load the control inside the page), but the <%= %> text cannot be directly read/used/manipulated by the code-behind without referencing some other control to get to it (like a containing div's InnerHtml property).
There are lots of options. You could use a single label, and string concatenate all the data you want displayed in that location.
You could create a user control with the layout you want and assign values that way.
You could inject it directly with response.write or the <%= %> syntax
You could create an HtmlGenericControl in your code behind (it's a div), add some text to it, and inject it into the pages controls collection.
Whatever you pick, try and go with the existing style of the coded page.
Look up the term "render blocks" for the <% %> syntax.
How about using
<asp:Literal id="z" text="goofy" runat="server" />?
Labels are usually used with forms.
You can also take full control of the rendering of your pages and controls and compose whatever you need to. You control the HTML, the order of rendering your controls, etc...
Go with the <asp:label /> (or a literal control if you want to customize some html in the content). Seriously. I'ts not that hard: when you put label in your markup visual studio will create it in the code-behind for you, so there's no extra work involved.
You could use the <%= "some string" %> syntax in web forms, but there can be issues when mixing that with the asp controls and there's a good reason new frameworks moved away from mixing logic like that in with your markup.

Resources