How to select the item form the dropdownlist in gridview - asp.net

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

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

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:

ASP .NET Reques.Form DropDownlist strange behavior

I have an issue getting the selected value of a dropdownlist.
My view source looks like this:
<div class="editor-field">
<select class="list" id="DivisionesLists" name="DivisionesLists">
<option value=""> -- Seleccione -- </option>
<option selected="selected" value="1">COORPORATIVO</option>
<option value="2">MANUFACTURA</option>
</select>
</div>
And my controller:
string s = Request.Form["DivisionesLists"];
The problem is that Request.Form["DivisionesLists"] returns "1,1" instead of just "1" (which is the actual selected value of the dropdownlist).
What may be happening?
Thank you!!!
It might also be posting some additional data, what happens if you select the first one, what does it post back? Does it post anything back with a comma?
It could be a duplicate control... though like Html.CheckBox action method: it renders the checkbox control and a hidden field that represents the value when the checkbox isn't there; accessing the value when its checked is true,false.
HTH.
If the select isn't a multiple select I think you have another control (input or select) that have the same name attribute on the page.

multiselect from dropdownlist for web app?

I'm building a web app that will come back with a report. For certain parameters where the user has requested a dropdown list, they also want to be able to select more than one option at a time.
e.g. show me all transactions from the states of TX, WV, and ID.
The reason I've decided to go with the dropdownlist they requested and not a listbox is there are over 40 parameters they can pick from and my page is already crammed with that many controls.
You won't be able to do it with a dropdownlist directly but what you could do is fake it.
Have a hidden ListBox on your page. Construct something that looks like a dropdownlist visually (label + image would work). During the onclick event of your fake dropdownlist show your listbox underneath your dropdownlist. Hide the listbox during the onblur event for the ListBox. Also you could throw in some jquery to animate the unveiling of your listbox to more closely match the look of a dropdownlist.
try this out: have a drop down for unselected choices and a list box for selected; similar to anton lavey's suggestions but slightly less elaborate.
<style>
select { width: 200px; }
</style>
<body>
<select id="sel" onchange="list.appendChild(this[this.selectedIndex]);">
<option disabled>Select items from this list</option>
<option value="a">a</option>
<option value="c">c</option>
<option value="d">d</option>
</select><br>
<select multiple="" id="list"><option value="b">b</option></select>
<button onclick="while(list.selectedIndex > -1) { sel.appendChild(list[list.selectedIndex]); }">Remove selected</button>
</body>
ordering can be preserved if you scan through to find the right spot to insert.

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