How can I get chosen values from select tag after click button? Here I get html select with multiple choose.
selTest.Value returns only first selected item.
<select runat="server" id="selTest" name="selTest" data-placeholder="Choose a Country..." class="chzn-select" style="width:350px;" tabindex="4">
<option value=""></option>
<option value="United States" selected="selected">United States</option>
<option value="Western Sahara">Western Sahara</option>
<option value="Yemen">Yemen</option>
<option value="Zambia" selected="selected">Zambia</option>
<option value="Zimbabwe" selected="selected">Zimbabwe</option>
</select>
<asp:Button ID="testBt" runat="server" OnClick="testBt_Click" Text="ds" />
Code behind:
protected void testBt_Click(object sender, EventArgs e)
{
}
If you use an ASP.NET ListBox proper you can access the Items, strongly-typed, and query:
<asp:ListBox runat="server" id="MyList" SelectionMode="Multiple">
<asp:ListItem Value="-1"></asp:ListItem>
<asp:ListItem Valuealue="United States" Selected="True">United States</asp:ListItem>
<asp:ListItem Valuealue="Western Sahara">Western Sahara</asp:ListItem>
<asp:ListItem Valuealue="Yemen">Yemen</asp:ListItem>
<asp:ListItem Valuealue="Zambia" Selected="True">Zambia</asp:ListItem>
<asp:ListItem Valuealue="Zimbabwe" Selected="True">Zimbabwe</asp:ListItem>
</asp:ListBox>
And then to get the selected items (using Linq):
var selected = MyList.Items.Where(i => i.Selected);
Html:
<asp:ListBox ID="Options" SelectionMode="Multiple" runat="server">
<asp:ListItem Value="option1" >Option #1</asp:ListItem>
<asp:ListItem Value="option2" >Option #2</asp:ListItem>
<asp:ListItem Value="option3" >Option #3</asp:ListItem>
</asp:ListBox>
Code behind:
var options = Options.Items.Cast<ListItem>()
.Where(item => item.Selected)
.Select(item => item.Value)
.ToList();
'options' will be a list of string contains all selected values.
Related
I want to make a dropdown list in asp:DropDownList in vb.Net webform.
I am calling a method that is returning a list. Now I want to bind that list to the dropdown
<asp:DropDownList ID="Branches" CssClass=" dropdown dropdown-toggle" runat="server" DataTextField="Name" DataValueField="Code">
<asp:ListItem Value="0000">---Select a Branch---</asp:ListItem>
</asp:DropDownList>
This is the code behind
Dim List = GetBranches("123")
Branches.Enabled = True
For Each i In List
Branches.DataValueField = i.Code
Branches.DataTextField = i.Name
Branches.DataBind()
Next
<asp:DropDownList runat="server" ID="Branches" CssClass=" dropdown dropdown-toggle" DataTextField="Name" DataValueField="Code" />
Dim List = GetBranches("123")
Branches.Enabled = True
Branches.DataSource = List
Branches.DataBind()
I am creating a site that contains 3 surveys stored in one SQL table, so based on the selected SurveyID the page will then be populated by the Questions and an answer line, now I have achieved this but now I need to be able to retrieve the information.
Using a DataList, each question generates a line, and within the itemtemplate of my DataList (QuestionList) I have put the following:
<asp:DataList ID="QuestionList" runat="server" DataKeyField="QuestionID" DataSourceID="QuestionData">
<ItemTemplate>
<div class="col-md-12" id="hr">
<h3>
<asp:Label ID="Higher_ReadingLabel" runat="server" Text='<%# Eval("Higher_Reading") %>' /></h3>
</div>
<div class="col-md-12" id="lr">
<h3>
<asp:Label ID="Lower_ReadingLabel" runat="server" Text='<%# Eval("Lower_Reading") %>' /></h3>
</div>
<div class="col-md-12">
<asp:RadioButtonList ID="AnswerList" runat="server" RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="Excellent" Value="Excellent"></asp:ListItem>
<asp:ListItem Text="Very Good" Value="Very Good"></asp:ListItem>
<asp:ListItem Text="Good" Value="Good"></asp:ListItem>
<asp:ListItem Text="Fair" Value="Fair"></asp:ListItem>
<asp:ListItem Text="Bad" Value="Bad"></asp:ListItem>
</asp:RadioButtonList>
</div>
</ItemTemplate>
</asp:DataList>
When the submit button is clicked, I want to retrieve the following:
DataKeyField of the QuestionList
Value of Checked Radio Button within AnswerList
I admit I am pretty stumped atm, everything I have tried hasnt worked at all, as the SQL isnt wrote yet I just wanted to see if the output could just output using Response.Write(value,datakeyfield).
Hope you guys can help,
Neil
This may be helpfull for you:
foreach (DataListItem datalistItem in QuestionList.Items)
{
if (datalistItem.ItemType == ListItemType.Item || datalistItem.ItemType == ListItemType.AlternatingItem)
{
var radioButtonList = datalistItem.FindControl("AnswerList") as RadioButtonList;
if (radioButtonList != null)
{
var selectedRadioButtonValue = radioButtonList.SelectedValue;
var itemDataKeyValue = QuestionList.DataKeys[datalistItem.ItemIndex];
Response.Write(string.Format("QuestionID :{0}, selected option: {1} <br/>", itemDataKeyValue, selectedRadioButtonValue));
}
}
}
Hi I have dropdown list code I want to add placeholder there how I can add?
<asp:DropDownList id="ddlyear" runat="server" >
<asp:ListItem >Experience</asp:ListItem>
<asp:ListItem>Fresher</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
I want to show like this
You can just do this:
<asp:DropDownList id="ddlyear" runat="server" >
<asp:ListItem selected hidden>Experience</asp:ListItem>
<asp:ListItem>Fresher</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
The selected makes it the default, while hidden prevents it to be visible on the expanded list.
It's the simplest way possible.
You can't do it on HTML alone, you'd need Html + jQuery to achieve this.
<asp:DropDownList ID="ddlyear" runat="server">
<asp:ListItem>Experience</asp:ListItem>
<asp:ListItem>Fresher</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
After this, you need your jQuery to do the magic by removing and re attaching your placeholder.
<script>
var isChanged = false;
$(function () {
$('#ddlyear').focusin(function () {
if (!isChanged) {
// this removes the first item which is your placeholder if it is never changed
$(this).find('option:first').remove();
}
});
$('#ddlyear').change(function () {
// this marks the selection to have changed
isChanged = true;
});
$('#ddlyear').focusout(function () {
if (!isChanged) {
// if the control loses focus and there is no change in selection, return the first item
$(this).prepend('<option selected="selected" value="0">Experience</option>');
}
});
});
</script>
Take note that you need jQuery to use this, just install it as a nuget package or download it manually and add a declaration in your aspx.
<head runat="server">
<title></title>
// Sample only, you can place it in any location or use any version
<script src="../scripts/jquery-2.2.2.min.js"></script>
</head>
#amit
Brother try this....
<select placeholder="select your beverage">
<option value="" default="" selected="">select your beverage</option>
<option value="tea">Tea</option>
<option value="coffee">Coffee</option>
<option value="soda">Soda</option>
</select>
In many cases it would be acceptable to have an empty or dummy item as the first item. 'Empty' means that the value is empty, the text can be anything you want.
So like this:
<asp:DropDownList id="ddlyear" runat="server">
<asp:ListItem Value="">Select</asp:ListItem>
<asp:ListItem Value="Experience">Experience</asp:ListItem>
<asp:ListItem Value="Fresher">Fresher</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
</asp:DropDownList>
Weird behaviour happening in my DropDownList Asp.net
My Code:
<asp:DropDownList ID="DDLFromMinutes" runat="server" Width="20%">
<asp:ListItem Text="00" Value="00"></asp:ListItem>
<asp:ListItem Text="15" Value="15"></asp:ListItem>
<asp:ListItem Text="30" Value="30"></asp:ListItem>
<asp:ListItem Text="45" Value="45"></asp:ListItem>
</asp:DropDownList>
However when i run it for example if 15 is the Selected Value i get it Twice in my DropDownList
While Debugging in FireBug get the following:
<select id="ContentPlaceHolder1_DDLFromMinutes" style="width:20%;" name="ctl00$ContentPlaceHolder1$DDLFromMinutes">
<option value="00" selected="selected">30</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
Code behind:
string Frominput = seprateFromTime[1];
string Frommin = Frominput.Substring(0, 2);
DDLFromMinutes.SelectedItem.Text = Frommin;
if (DDLFromMinutes.Items.Count > 0)
{
DDLFromMinutes.SelectedIndex = DDLFromMinutes.Items.IndexOf(DDLFromMinutes.Items.FindByText(Frommin));
}
While saving the Data is use DDLFromMinutes.SelectedItem.Text Could this be an issue?
You need to use MyDropDown.SelectedValue instead of selectedItem.Text.
Details here: MSDN
do you want to get the value of drop down list? If so, I think you need to change SelectedItem to SeletedValue.
I am having 2 dropdown lists on my page if i select an item the same selected item should be displayed in another drop down list. Can any one give me a javascript for this i need Javascript not Jquery
here is a very simple implementation of what you are describing:
given the html:
<select id="select1">
<option value="foo">foo</option>
<option value="bar">bar</option>
</select>
<select id="select2">
<option value="foo">foo</option>
<option value="bar">bar</option>
</select>
and this javascript:
document.getElementById('select1').onchange = function(e) {
var index = this.selectedIndex;
document.getElementById('select2').options[index].selected = true;
}
you can achieve what you want. note the indexes should be exactly the same in both select boxes (as in the options should be in the same order)
You can attach an onchange event on your dropdown.
Then whenever your selected Index changes, it will fire and call the supplied update method.
For example:
HTML
<asp:DropDownList id="FirstDropdown" onChange="javascript:update();" ...>
JavaScript
<script type="text/javascript">
function update ( ) {
document.getElementById('<%= SecondDropdown.ClientID %>').value =
document.getElementById('<%= FirstDropdown.ClientID %>' ).value;
}
Try this
<asp:DropDownList ID="ddl1" runat="server">
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem Value="4"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<script type="text/javascript">
function MyApp(sender){
var lbMatch = false;
var loDDL2 = document.getElementById('DropDownList1');
for(var i=0; i< loDDL2.length-1; i++){
lbMatch = sender.value==loDDL2.options[i].value;
lsSelected = lbMatch ? "<=SELECTED" : "";
if(lbMatch)
loDDL2.selectedIndex = sender.selectedIndex;
}
}
</script>
In page load event add this
ddl1.Attributes.Add("OnChange", "MyApp(this)");