I want dropdown auto select value from DB while loading. For example i have values like this:
<option value="1">Mango</option>
<option value="2">Orange</option>
but I want when dropdown load orange selected because in previous form I already select orange.
<select class="form-control" data-plugin="select2" id="ddl_fruit" name="ddl_fruit" disabled="false" data-select2-id="2" tabindex="-1">
<option value="">Select fruit</option>
#if (ViewBag.fruitname!= null)
{
foreach (var item in ViewBag.fruitname)
{
<option value="#item.ID">#item.fruitname</option>
}
}
</select>
I am using ADO.NET and using not html.dropdown.
How do I solve the problem?
foreach (var item in ViewBag.fruitname)
{
if(#item.fruitname == "Orange")
<option value="#item.ID" selected = "selected">#item.fruitname</option>
else
<option value="#item.ID">#item.fruitname</option>
}
if you do not want to use if condition or html helpers then you use Jquery to select specific item.
Note: if you are using sections please put this code in it.
$(function(){ $('#ddl_fruit').val('2') // for orange })
Pass selected value in ViewBag.SelectedFruit from Controller to View.
In View:
foreach (var item in ViewBag.FruitName)
{
<option value="#item.Id" #(ViewBag.SelectedFruit == item.FruitName ? "selected" :"")>#item.FruitName</option>
}
Related
This is the code in View Section:
<select name="country">
<option value="0">Select</option>
#foreach (var country in countries)
{
<option value="#country.CountryId"> #country.CountryName</option>
}
</select>
Code to get value from Controller:
public ActionResult IndexPost()
{
ViewBag.Countries = CountryRepository.GetCountries();
if (Request["btnSubmitCountry"] != null)
{
string country = Request["country"];
ViewBag.Msg = "You have selected " + country;
}
return View();
I wanted to Request to get value the name of the Country too, but right now it only gets the id. Can you please help me to get the id and name both using this only this method.
Because select returns the value of the value,But you can use the hidden tag to store the text value.
E.g:
<form onsubmit="var sel=document.getElementById('country');document.getElementById('country_text').value=sel.options[sel.selectedIndex].text;">
<select name="country" id="country">
<option value="0">Select</option>
#foreach (var country in countries)
{
<option value="#country.CountryId"> #country.CountryName</option>
}
</select>
<input type="hidden" name="country_text" id="country_text" />
</form>
get text:
string countryText = Request["country_text"];
I want to get a dynamic dropdown with different backgroundcolors depending on the value. value: red -> backgroundcolor: red and so on...
Dropdown is filled by table in SQL-database.
Ampel
This snippet will do what you need.
$('#mySelect > option').each(function() {
$(this).css('background-color', $(this).val());
});
$('#mySelect').on('change', function() {
$('#mySelect').css('background-color', $(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="mySelect">
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
<option value="orange">orange</option>
</select>
I want select item from dropdown list in winforms C# app.
I have:
<select name ="petbet">
<option value="1">Speedy</option>
<option value="2">Snuffles</option>
<option value="3">Sneak</option>
<option value="4">Snow</option>
<option value="5">Slowww</option>
I try:
var test = webBrowser2.Document.GetElementsByTagName("select").Count;
webBrowser2.Document.Forms[0].SetAttribute("value", "3");
I want select sneak, but don't work.
I am trying to change the value of a drop down list using the following code on the click event handler. Nothing actually changes when the button is pressed. What am I missing? Is this the correct way to do this?
HtmlDocument document = webBrowser1.Document;
HtmlElement salutation = document.GetElementById("status");
salutation.SetAttribute("value", "Mr");
Here is the html
<select id="status">
<option selected="selected" value="">Select</option>
<option value="Mr">Mr.</option>
<option value="Mrs">Mrs.</option>
</select>
You could try this (based on your code)
HtmlDocument document = webBrowser1.Document;
HtmlElement salutation = document.GetElementById("status");
var option = salutation.Children.Cast<HtmlElement>().First(x => x.GetAttribute("value").Equals("Mr"));
option.SetAttribute("selected", "selected");
Are you selecting the <select> tag? If so, don't use that. Select the <option>, just like this:
<select>
<option value="volvo">Volvo</option>
<option value="saab" selected="selected">Saab</option>
<option value="vw">VW</option>
<option value="audi">Audi</option>
</select>
To see it in action: See this link
Using some codes from scartag, how about this:
HtmlElement salutation = document.GetElementById("status");
var option = salutation.Children.Cast<HtmlElement>().
Where(x => x.GetAttribute("selected").Equals("selected")).First();
if (option != null) option.SetAttribute("value","Mr,");
I want to be able to set the first value in my dropdown list as "Select a poll", and then load all my select items from my viewdata respectively. How would I go about doing that? My block of code right now looks something like this:
<select name="ddlPolls" id="ddlPolls">
<% foreach (var item in (string[][])ViewData["returnPolls"])
{ %> <option value="<%= item[0] %>"><%= item[1] %></option><% } %>
</select>
This returns a dropdown list of my polls, but I want the first value in that dropdown list to be "Select a poll" so that I can use jquery to perform .change ajax functions to load another dropdown list. Any help is much appreciated!! :D
Just write your code as follow:
<select name="ddlPolls" id="ddlPolls">
<option value="0">Select a poll</option>
<% foreach (var item in (string[][])ViewData["returnPolls"])
{ %> <option value="<%= item[0] %>"><%= item[1] %></option><% } %>
</select>