Angularjs includes an empty option in select - asp.net

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:

Related

Trying to use a select value to save information

I used a select dropdown that is filled dynamically, based on conditions on country>state. But I can not get it out on context for the backend to properly save it to the database (selected value).
I can show the dropdowns correctly and save differently and correctly if I use a asp dropdownlist. Thing is, this code is imported and apparently all I can use if the select value.
<label for="country" class="col-form-label">Country: </label>
<select name="txtAddress_Country" class="countries order-alpha form-control" id="txtAddress_Country" runat="server">
<option value="">Select Country</option>
</select>
<label for="txtPersonal_FirstName" class="col-form-label">State: </label>
In the backend... this function works wonders with dropdownlist and text.
dbCampos.Save(u, new ASF.HC.JobApplication.Entity.Campo("txtAddress_Country", txtAddress_Country.Text));
So I am guessing the place to modify is the frontend.
I am guessing that I am missing some attribute but can not seem to find it. Says that it does not contain a definition for "Text" or accepting a first argument.
Maybe an initial value?
It is better to use instead of html with runat server.
secondly you can try as below:
dbCampos.Save(u, new ASF.HC.JobApplication.Entity.Campo("txtAddress_Country", txtAddress_Country.SelectedValue));
instead of:
dbCampos.Save(u, new ASF.HC.JobApplication.Entity.Campo("txtAddress_Country", txtAddress_Country.Text));

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).

asp.net dropdown always no selected value on server side

I have an asp.net dropdown like this
<asp:DropDownList width="95%" ID="RessourceComposantes" runat="server"
DataSourceID="Composantes"
DataTextField="Description" DataValueField="ComposanteID">
</asp:DropDownList>
<asp:SqlDataSource ID="RessourceComposantes" runat="server"
ConnectionString="<%$ ConnectionStrings:OraEntities %>"
SelectCommand="SELECT [Blabla], [blablabla] FROM [blablablaa]
ORDER BY [blablablabla]">
</asp:SqlDataSource>
Is it normal that when I do dd_ressource_composante.selectedvalue on the server side I have no value. It's always "".
the source looks like this :
<select name="ctl00$Tab$dd_ressource_composante" id="ctl00_Tab_dd_ressource_composante" style="width:95%;">
<option value="1">Composante</option>
<option value="3">DGAG</option>
<option value="2">DSF</option>
<option value="5">Test</option>
<option value="6">Tous</option>
<option value="4">VMD</option>
</select>
I just tried to fill my dropdown in vb.net and I have the same result. The dropdown is full but when I do a postback I have no selected value
Actualy on the server side when I put a breakpoint on the dropdown, the item count is 0. I don't understand why... It's like the 8th dropdown list in this project and everything is the same but this one doesn't work.
I don't do any binding on the page load. it's all in the aspx file
well it works ONLY when I add autopostback="true" SelectedIndexChanged="dd_ressource_composante_SelectedIndexChanged" for the dropdown.
And theres no code in dd_ressource_composante_SelectedIndexChanged
It's vb.net for the server side
Thank you
Where are you calling your dd_ressource_composantes.DataBind() method in the code-behind? Is it in the Page_Load event? If so, have you wrapped that statement in an IsPostback check so that you're not binding it everytime the page loads? Not doing so would reset your DataSource, and any selected value, every time the page posts back.
If (Not Page.IsPostBack) Then
dd_ressource_composantes.DataBind()
End If
NOT
' No IsPostBack check
dd_ressource_composantes.DataBind()
Also, doesn't your DataSourceID have to be "RessourceComposantes" and not "Composantes" since that's the ID of your datasource?
When setting SelectedValue, the value must match the value of the Value attribute. If that's not the case, the results are, for all intents and purposes, undefined.
If you are calling DataBind() method somewhere in your page_load event please comment that otherwise wrap it in
IF NOT IsPostBack Then
DataBind()
EndIF
This problem is most likely caused by rebinding the control before the event you are using is fired. If you are getting the selectedItem.Text the SelectedValue should also be there.
Well that's weird. That control was in a table and I added a row like this
tblRessourcesProjet.Rows.Insert(1, tr)
and I changed it for
tblRessourcesProjet.Rows.Add(tr)
I don't understand quite well why it was a problem but it works now.
Thank you everyone!

how the exclude a asp.net web control from the tabbing order

I've been trying to exclude an asp.net web control from the tabbing order.
The control that i'm using is the RadioButtonList control. I've try setting the TabIndex to either 0 or -1.
The problem that i'm running into is ... initially the control is skipped (which is good), but it seemed like the engine just shifted the control to the end of the tabbing order.
Is this an expected behavior? or is there a work around for this?
After checking the HTML source, I have some interesting findings ...
<td><input id="rbSpiffType_0" type="radio" name="rbSpiffType" value="R" checked="checked" tabindex="-1" /><label for="rbSpiffType_0">Regular Spiff</label></td><td>
I think i might be tabbing into the label ... any ideas how to fix that in ASP.NET?
The issue may be the individual radio buttons don't have a tab index of -1, try looping through the RBL's Items collection, and do:
item.Attributes.Add("tabindex", "-1");
And see if that clears it up. It might actually be item.Attributes["tabindex"], can't remember the exact syntax at the moment.

Don't display HTML code and entities in dropdownlists

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>

Resources