When should I use a Localize control instead of a Literal? - c#

I recently became aware of the System.Web.UI.WebControls.Localize control in a lab for the ASP.NET 4.0 MCTS certification course. The purpose of this control is unclear to me.
In the examples, the Literal control and the Localize control appear to be more-or-less interchangeable. Upon inspection, it appears that the Localize control inherits from Literal, but provides no additional functionality. It uses a different designer class, which appears to me to be less capable than the designer class for literals.
So, color me confused. Literals are localizable already. What is the Localize control used for? Should I use it, and under what circumstances?

I appreciate this has already been marked as answered, but here is another way to look at it.
<asp:Localize> is used to specify a Resource defined item, which forces the IDE to display some specified text, and still allows it to resolve at runtime, to the language of the website.
This may be useful for the development of a site where the content of the site is actually in a different language. So you would be able to be an English-speaking programmer, creating a website in Turkish, and still know what a <asp:Label> is supposed to without having to learn Turkish.
So as an example:
<asp:Localize runat="server" Text="<%$Resources : Label, Price%>">
Price
</asp:Localize>
Now, if my default Label.resx was translated into Turkish, the Labels.resx mapping would be:
Key="Price"
Value="fiyat"
At design time, the IDE would display Price (as the inner text of the <asp:Localize> element is Price) but the actual live view of the page in a web browser, would resolve to fiyat.
Therefore:
<div>
<asp:Localize runat="server"
Text="<%$Resources : Label, Price%>">
Price
</asp:Localize>
</div>
Becomes rendered as:
<div>fiyat</div>
But in the IDE Designer, this would be displayed as "Price".
The difference with labels, is that <asp:Label> will resolve to fiyat in both the IDE Designer and at run-time.

This described it best:
The Localize Control lets you localize any element on an .aspx page. It provides a design time feature not offered by its base class, the Literal control; in particular, the Localize control provides design time editing of static content so you can see a default value while working in page design mode.
Found at: http://wiki.asp.net/page.aspx/357/localize/
MSDN (bottom section has a good description)

Related

What is the usage of AccessibleRole property in a user control?

there are two properties with these names in user control :
1- AccessibleName
2- AccessibleRole
What are these properties and what's their usages in an win form application?
I have already take a look at MSDN but unfortunately I couldn't understand their descriptions?
any help will be appreciated
Those are special attributes that can be used by screen readers or text-to-speech programs to make your program accessible to people with disabilities or who use assistive technology.
AccessibleName can be used to tell the user the element the cursor is sitting in. E.g. the assistive technology program can't determin which label around the text box belongs to it, but it's able to read that attribute (i.e. "what's the meaning of this box?"). A more detailed description can be added to AccessibleDescription.
In a similar way AccessibleRole describes the user (or tells the tool) what kind of control this is. This isn't as important for the standard controls, but imagine some custom button or hyperlink control: With this attribute it's able to tell "Hey, I'm clickable and I'm a button/link!".

Change every control like TextBox's max length in big Asp.net Application

I have a situation where i have a very big project and i want to change max length property of every textbox that exist in application. How can i do this with small effort and without overhead on server.
Create a new class where you inherit from the standard TextBox.
Set/override the MaxLength property in the new class.
Use ASP.NET tag mapping to replace all instances of the standard TextBox with your new one. In web.config:
<pages>
. . .
<tagMapping>
<add tagType="System.Web.UI.WebControls.TextBox"
mappedTagType="YouNamespace.YourTextBox" />
</tagMapping>
</pages>
TagMapping is done by the compiler, so there's no added runtime overhead.
You could also use a control adapter, but in this case I think tag mapping may be cleaner and easier.
You can use a control adapter to customize the rendering of all the controls of a specified type, so this way you have control over the max length accepted by the text box.
From the ControlAdapter documentation (emphasis is mine):
An adapter modifies a control for a specific browser or class of
browsers or acts as an arbitrary filter on some capability.
In this case the arbitrary filter would be a common MaxLength for all TextBox controls.
Refactor. If all your TextBox controls on the project derives from System.Web.TextBox then make your own MyTextBox that derives from System.Web.TextBox and make all your textboxes derives from this instead.
So, in, the MyTextBox just set the preferred MaxLength
Use search-replace option of text editors.
alternatively if you do not want to hard code it, use a common Java script file on page load. In this javascript set the MaxLenght of each TextBox of the page.
Something like this in Jquery should work for you
$(document).ready(function()
{
// replace 6 with new length you want to set
$('input[type=text], textarea').attr('maxlength','6');
});

ASP.NET web control only allow specific contents?

I am new to ASP .NET web controls, but not ASP .NET in general or C#.
I am wondering how I can limit the allowed content types to a specific class.
I have made a custom web control called TabPanel, and I want it to only be able to contain TabPages.
As an example, the following markup should be illegal, since it contains a checkbox.
<cc1:TabPanel ID="TabPanel1" runat="server">
<cc1:TabPage runat="server">
this is a simple test
</cc1:TabPage>
<cc1:TabPage runat="server">
this is another simple test
</cc1:TabPage>
<asp:CheckBox runat="server" />
</cc1:TabPanel>
In this case, I wouldn't want the checkbox to be there. How can I block this from happening?
I have not tried exactly what you are after but based on other things I have done I would try this:
Create a property in TabPannel that is a collection of TabPages (call it Tabs for demonstration purposes). This can be an array, a list, or a custom collection class, the key is to have typed to only accept TabPages as members.
Give the property the [PersistenceMode(PersistenceMode.InnerProperty)] atribute.
Override CreateChildControls to add the contents of the collection to the control.
If you do it this way then your mark up should end up looking something like this:
<cc1:TabPanel ID="TabPanel1" runat="server">
<Tabs>
<cc1:TabPage runat="server">this is a simple test</cc1:TabPage>
<cc1:TabPage runat="server">this is another simple test</cc1:TabPage>
</Tabs>
</cc1:TabPanel>
and it should not allow anything that is not a TabPage to be nested inside of the Tabs property.
http://msdn.microsoft.com/en-us/library/9txe1d4x(v=VS.90).aspx is a walk through demonstrating this technique in detail.
I figured it out.
Had to throw an exception under AddedControl procedure that I overrided from the WebControl if the type of the control being added was not of the type I wanted.
Now the designer shows a beautiful red error-message on the control itself, preventing me from doing such a foolish thing.
Awesome!
I'm going to take a guess here, but based on some quick googling I think you're looking for the ControlBuilder. The demo limits their control to an object called "mycell", but I don't see any reason why this couldn't be limited to your own objects, or build-in ASP.NET controls (i.e. Panels but not TextBoxes, etc.)
As a last resort, I'm sure you could hijack the rendering method and only render controls within the pre-determined class set, but this seems hack-ish at best.

How is ID work in asp: TextBox?

I'm very new in .net c#, just a question here to about the following code. I have wrote a code to display a text box on the screen as below:
<asp:TextBox ID="sUnit" runat="server" MaxLength="12" Width="3em" />
But somehow I failed to using jqueryto assign a value into this textbox:
$('#sUnit').val('test');
Surprisingly when I view the source code, and it show me the ID for the text box is like below:
<input name="ctl00$cplh$sUnit" type="text" maxlength="12" id="ctl00_cplh_sUnit" style="width:3em;" />
Does anyone know how can I get the textbox ID so that I can assigned the value into the textbox?
Hope my question is not sound stupid.
If you are using ASP.NET 4.0, set the ClientIDMode to static.
For older versions, use
$('#<%= sUnit.ClientID %>')
This is due to the fact that UI elements containing these controls are being rendered by ASP.NET, those containers that implement INamingContainer generate the prefix that should be prefixed as per their scope.
From the MSDN line mentioned above:
Any control that implements this
interface creates a new namespace in
which all child control ID attributes
are guaranteed to be unique within an
entire application. The marker
provided by this interface allows
unique naming of the dynamically
generated server control instances
within the Web server controls that
support data binding.
One suggestion in order to find the control itself might be to use the first selector, where you controls are contained within a regular div. An example to find elements of type input might look something like:
$('#MyDiv').first('input[type="text"]')

DateTimePicker-like Input Control

I'm not very experienced with .NET development, so forgive me if I've missed something obvious.
My scenario is quite simple: my program is going to deal with both the Imperial and Metric systems of measurement. I need an input control that will be able to elegantly house {feet, inches} and {meters, centimeters} (only one set at a time).
From what I've seen in VC#'s Control Toolbox (Winforms .NETF 3.5), I'm looking for a mix of MaskedTextBox, NumericUpDown, and DateTimePicker.
Namely:
MaskedTextBox's ability of filtering user input
NumericUpDown's ability of [in/de]crementing the user-selected [sub-]unit of measurement
DateTimePicker's ability of "breaking apart" information in a single control. I'm specifically interested in breaking apart say feet and inches while still displaying them on the same control
I should also point out that I'm most interested in replicating DateTimePicker's ability to keep separate pieces of input in a single control.
I would greatly appreciate your thoughts as to what base control I should sub-class in order to achieve this functionality.
Cheers!
Have you looked at this code project article which might be a starting point.
I think DateTimePicker isnt enough flexible to make that.
You should create a composite user control. The main control should contain:
Three NumericUpDown control (year, month, day)
Delegate events to every Validate event (instead of masked textbox)
Show a Calendar control when ie. user click into the NumbericUpDown control (or you can dedicate an '...' labelled button on the right)
Or yoou can search on CodeProject or Codeplex for opensource, and Telerik and DevX for shareware components.

Categories

Resources