Don't display HTML code and entities in dropdownlists - asp.net

When I add or é to a text value of a listitem, it display the code of the HTML entity instead of the result (a space or é).
I can add "physical" non-breaking spaces or special chars, but I would like to avoid that if possible. Sometimes the data stored in database is encoded, and I don't want to always process data before displaying it.
Any solution ?
Thanks
Edit note : previous description noted it was about a dropdownlist simulating a treeview, but it was merely an example ; I can't and don't want to replace the dropdownlist by anything else.

ListItems are automatically HtmlEncoded.
You can HtmlDecode the list items before hand, so when they are HtmlEncoded you get the proper characters:
DropDownList1.DataSource = new List<string> { Server.HtmlDecode("A…"), Server.HtmlDecode("B C") };
DropDownList1.DataBind();

You can use the <optgroup> tag for hierarchical (i.e., tree-like) drop-down menus.
<select>
<optgroup label="Parent 1">
<option>Child 1.1</option>
<option>Child 1.2</option>
</optgroup>
<optgroup label="Parent 2">
<option>Child 2.1</option>
<option>Child 2.2</option>
</optgroup>
</select>

Try with ul(ol) and play with the css. You can use jquery to change style if some requirements are met (li has value attribute)

Standard drop down lists are very poor at representing treeviews .
They can handle going one level deep if the top level is not selectable (see the optgroup element[1]), but beyond that I suggest taking a regular list (ordered or unordered) with as much nesting as you need (remember sublists go inside list items in their parent list) and including a checkbox or radio button with each selectable list item.
Some JavaScript could then be added to manipulate classes to create a drop down effect if desired.
[1] Optgroup should be able to go deeper but, IIRC, browser support is sucky.

The problem described in the question isn't what I see using a current version of ASP.NET -- when I try it, both the following – are rendered correctly:
<asp:RadioButtonList ID="Visibility" runat="server">
<asp:ListItem Value="public" Text="Public – All users"></asp:ListItem>
<asp:ListItem Value="private">Private – Only you</asp:ListItem>
</asp:RadioButtonList>

Related

Angularjs includes an empty option in select

I have a dropdownlist with data being bound dynamically this way at Page_Load:
<asp:DropDownList ID="ddlSet" DataTextField="Title" DataValueField="PKSetId" runat="server"
AppendDataBoundItems="true" ng-model="SetId"></asp:DropDownList>
The rendered html is :
<select id="cpContent_ddlSet" ng-model="SetId">
<option value="? undefined:undefined ?"></option>
<option value="3">Test3</option>
</select>
The option Test3 should be selected by default when the page loads, but this isn't happening. I would have set $scope.SetId=3, but don't know this value before hand.
I see similar questions here but the dropdownlists are data bound the angular way, where you can easily set the selected item from $scope.
How to tackle this.
Either you build a server side ASP application or you build a client side Angular application, but don't mix the 2 technologies.
Let Angular do the whole frontend logic and rendering and the ASP will just provide the REST services.
See Angulars Select for an example how to preselect a dropdown value.
You cannot do this in this way.
I suggest to add option at first place with such as:
<option value="" disabled ng-selected> Make your choice:

Format kendoui textbox as phone number using jquery.inputmask plugin

I'm trying to format a textbox control in kendoui as phone number, using data- attributes
The backend field is string.
I made it as:
<asp:TextBox runat="server" CssClass="k-textbox"
data-role="numerictextbox"
data-spinner="false"
data-format="(###) ###.####"/>
and
<asp:TextBox runat="server" CssClass="k-textbox" data-format="(###) ###.####"/>
but none works.
First one does not display any intermediary formatting characters or spaces: ( ) -
Second does not enforce digits, as well as not display any intermediary formatting characters.
Any suggestions?
EDIT
I just find out jquery.inputmask, and tried to use it. I included the script jquery.inputmask, and added the data-inputmask attribute as below
<script src="/scripts/jquery.inputmask/jquery.inputmask-2.4.20.js"></script>
....
<input type="text" class="k-textbox" data-inputmask="'mask': '(999) 999.9999'"/>
but nothing happened.
How should I trigger mask enforcement?
I don't want to use jquery call for each control like
$("#myctrl").inputmask({"mask": "(999) 999.9999"})
but instead use data-attributes
Thanks
You should use the HTML5 phone number input type.
html5 input types
As far as I am aware KendoUI does not have a widget or control to format input field such as phone numbers. It is planed to be incorporated soon this year, I think it has not been release yet:
http://feedback.kendoui.com/forums/127393-kendo-ui-feedback/suggestions/2404523-just-a-mask-edit
In the meantime I would recommend you using one of the JQuery options available, like these 2 examples:
https://github.com/RobinHerbots/jquery.inputmask
http://digitalbush.com/projects/masked-input-plugin/
Here is a link to a working fiddle using the data-inputmask attributes that you would like to use.
You had assigned the mask in the attribute correctly..
<input type="text" class="k-textbox" data-inputmask="'mask': '(999) 999.9999'"/>
Your problem is that you never selected the elements and then applied the inputmask. The data-inputmask is just an attribute that will store the mask definition, it doesn't run automatically.
You need to add this.
$(document).ready(function()
{
$('[data-inputmask]').inputmask();
});

How to select the item form the dropdownlist in gridview

I have the dropdownlist in the gridview itemtemplate, i need to select the value based on the dataset, i tried to bind it as "SelectedValue='<%# Eval("code") %>', but i cant find any attribute like "SelectedValue" in HTML code.
I tried below link also it was not work out to me
Eval() in a DropDownList within a GridView
Can any one help me in that
You have to understand how dropdown list (select tag) in HTML works.
E.g. if you want to select some item you have to mark it as selected as follows
<select>
<option value="a">a</option>
<option value="b" selected="1">b</option>
<option value="c">c</option>
</select>
So you have to put selected="1" to item you want to select. That means you can not do it easily using Eval method. You have to utilize server side which will do it for you.
The example given on the page you have postetd and which you said you have tried works correctly. Check whether the HTML output contains value attributes at each option. The value can differ from what is enclosed within the option tag. If the value attribute is missing that is the reason why the item is not being selected. (Use firebug or any developer console to examine).

Checking a radio button with jQuery when radio button is runat="server"?

Using jQuery I want to be able to click an element which will also checks it's related radio button. I had this working fine until we had to add runat="server" to the radio buttons.
When I apply this it prevents my jQuery function from working and I cant figure out how to get round it, heres a simplified version of the code:
HTML
<input type="radio" runat="server" id="sector1Radio" name="SectorGroup" title="Sector1" />
jQuery
$('#SomethingElse').click(function() {
$('input[title=Sector1]').attr('checked','checked');
});
I've found out that when its converted to a .net control instead of checked="checked" (as it would be usually) it is just Checked, so I changed that but on inspecting the DOM in multiple browsers, none of my radio buttons are being checked :-(
Are there any other ways I can use jQuery to check a radio button that has runat="server"?
Cheers!
I think that Your problem is that the id of the input is no longer sector1Radio but rather ctl00_sector1Radio or something similar. This happens if Your input control is inside e.g. a ContentPlaceHolder control (when using master pages).
Can You check the generated HTML code (in the browser) to verify if this is the case? What is the id of the input control?
If this is the case, You need to generate Your js jQuery code
$('#SomethingElse').click(function() {
$('input[title=Sector1]').attr('checked','checked');
});
from codebehind so that SomeThingElse is replaced with the ClientID of the control.
.is(':checked') works on ASP.NET radiobuttons and checkboxes
$('#SomethingElse').click(function() {
$('input[title=Sector1]').is(':checked');
});
try using
$('input[title=Sector1]').attr('checked',true);
and
$('input[title=Sector1]').attr('checked',false);
or maybe
$('#SomethingElse').click(function () {
$('input[title=Sector1]').attr('checked',!$('input[title=Sector1]').attr('checked'));
});
As suggested by others, ASP.net will not generate the html with the same ID you specified.
Quick solutions:
You can keep using the id but asks jquery to check the end of the id instead, example:
$("input[id$='sector1Radio']").is(":checked");
Or check against the title and name as Nico suggested
Use the class element which is not effected by ASP.net, example
<input type="radio" runat="server" id="sector1Radio" class="sector1Radio" name="SectorGroup" title="Sector1" />
$("input.sector1Radio").is(":checked");
Best thing is to view the generated html code and see what id is giving you, then you can use the appropriate jquery selector, because the generated id could have different extensions depends whether you use master pages, etc.
If you are using a MasterPage or are creating the controls dynamically then it is probable that the control ID's are being renamed #SomethingElse becomes #MainContent_SomethingElse.
The easiest way to check this is to use the WebDeveloper plugin for Firefox or Chrome.
Go to Information -> Display Element Information and then select the object in question. It will give you it's ID, class, as well as ancestor and children information.
Check to see if the ID is being changed dynamically by the .NET.
If that's the case:
To prevent this, in the server side code you can use the following attribute to create static ID's
SomethingElse.ClientIDMode = ClientIDMode.Static;
You can then reference in you jQuery
$('#SomethingElse').click(function() {
if ($('input[title=Sector1]').attr('checked')) {
//execute event
});
I think what happens is that in ASP NET Checkboxes and Radio Buttons generates an "input" and a "span" after the input. So you need to select the input only.
You can try:
$('.classname input[type=checkbox]').each(function() {
this.checked = true;
});
Two things here: finding the control and executing the check. In ASP.NET, your control's actual ID and name will end up getting changed based on the runat="server" containers in which it appears, even if those containers have no Ids.
Rendered ASP.NET controls always end with the same name as you started with, so a tag like:
<input type="radio" runat="server" id="sector1Radio" title="Sector1" />
might end up being rendered as
<input type="radio" runat="server" id="ctl0$ctl0$sector1Radio" name="ctl0_ctl0_SectorGroup" title="Sector1" />
You can find this element, even after it is rendered if you use the "contains" selection syntax in JQuery. So to find this element, once rendered, you could use:
$("input[type='radio'][id*='$sector1Radio']")
This syntax will find any radio button whose id contains "$sector1Radio"
Once you have the element, you can check or uncheck it using the following code, which you'd call from the click event of your other element.
// check the radio button
$("input[type='radio'][id*='$sector1Radio']").attr('checked', true);
// uncheck the radio button
$("input[type='radio'][id*='$sector1Radio']").attr('checked', false);
One last thing... if you just want a block of text to click the button when pressed (wrap it in an tag and set the AssociatedControlId property to the control name of your radio button, like this...
<input type="radio" runat="server" id="sector1Radio" title="Sector1" />
<asp:label runat="server" id="lblsector1Radio" associatedControlID="sector1Radio">clicking here clicks and unclicks the radio button</asp:label>
I had the same problem. To use the jQuery UI to make your radiobuttons nice one has to write:
<div id="radio">
<input type="radio" id="radio1" runat="server" />
<label for="radio1">The label of the radio button</label>
...
</div>
<script type="text/javascript">
$('#radio').buttonset();
</script>
The id of the input tag must be correctly referenced by the label's for attribute. If the webpage is inside a master page then the id of the input tag will be modified to something like ctl00_Something_radio1, and suddenly the label's for attribute no longer references the input tag. Beware of this in ASP.NET!

What's the Literal control used for and what's the difference to the Label Control in asp.net?

What Literal control is used for in asp.net? and What is the difference between them and Label control?
The major difference is that the Label Control adds the span tag to the text (property) you set, allowing to apply a style to it:
<span>My Label text</span>
The Literal Control allows you to render any kind of content. You can use it to render scripts, hmtl and any other type of document content. It doesn't change the string you provide in the Text property.
Note: the Label control allows you to render straight HTML too, but it puts all your text in span tags as mentioned. So, for rendering large HTML portions a Literal control is the way to go.
P.S.: In HTML there is a <label> tag. If you use the AssociatedControlId property of the Label control, it will render as HTML <label> (thanks to Ray for pointing that out.)
For example:
<asp:Label runat="server" id="FirstNameLabel" AssociatedControlId="FirstNameTextBox">
Input First Name:
</asp:Label>
<asp:Textbox runat="server" id="FirstNameTextBox" />
Will render as:
<label for="FirstNameTextbox" id="FirstNameLabel">Input first name:</label>
<input type="text" id="FirstNameTextbox" name="FirstNameTextBox" />
See also here on W3 Schools.
One thing also to note is if you are justing using it to display something and have no need for formatting the text use a Literal control. The ViewState is not as heavy with a Literal vs a Label control and when you have many of these on a page using ViewState it can really bloat your page size.
I always ask myself, do I need to apply a custom style or formatting? Yes, use a Label. No, use a Literal.
It is used to display text on the page, the text that is displayed can be set at runtime via server side code.
The label control also has the AssociatedControlId property that associates the label with another control. An example of where this is useful is with a textbox control. Once these are associated, screen readers are more able to give better results.
Another example is a radio button with a label allows you to click on the label and the radiobutton will select if the AssociatedControlId property is set.
MSDN on AssoicatedControlId
As splattne mentions, the label encloses its text in a span, whereas the literal is simply a placeholder. However, be careful in making assumptions about how ASP.Net controls are going to render. It can depend on the user agent that you are using. For instance, the panel control renders as a div in IE, but renders as a table with Firefox.
It will place LITERALLY whatever text you place in it on the page. You can use it to write html, JavaScript or just plain text.
We can use literal control in the title tag whereas label cannot be used in the title tag
Label can be used to set focus on other controls like Textbox.
Whereas Literal simply rander the static text on the web page

Resources