Obtain selected value from dropdown in code behind - c#

I have the follwing dropdown list and to get the selected value with javascript is easy enough.
<select id="FirstDropDown" runat="server" onchange="ValidatePrimaryDropDown();" >
<option>[Please Select Yes Or No]</option>
<option>Yes</option>
<option>No</option>
</select>
var e = document.getElementById("FirstDropDown");
var dropDownFirst = e.options[e.selectedIndex].value;
I prefer to use this dropdown as apposed to 'asp:DropDownList'.
How can I retrieve the selected value in code behind C#?

There are FindByText and FindByValue function available.
ListItem li = Select1.Items.FindByText("Three");
ListItem li = Select1.Items.FindByValue("3");
li.Selected = true;
Link to source

Is that an aspx page? If so, add a name attribute to your select and use
Requets.Form["elementName"];
in aspx.cs.
Btw: to your javascript code: is there any particular reason why are you using DOM selection instead of jquery? In jquery you wold just use
var selectedItem = $("#FirstDropDown").find(":selected").text()

Related

get aspxcombobox value using javascript

I have a aspxcombobox with ID="comboSubjectCategory", can I get the selected value/text using javascript function? thanks lot...I tried this but it does not works.
function getValue() {
var combo = document.getElementById('comboSubjectCategory').value;
alert(combo);
}
The ASPxComboBox is not a simple HTML element. In the browser, it is represented by the ASPxClientComboBox Class. Use its GetValue, GetText or GetSelectedItem methods to obtain the selected value.
You can obtain the ASPxClientComboBox instance by the name assigned to the ClientInstanceName property of the ASPxComboBox control.
See Also: Client-Side Functionality
var combo = document.getElementById('<%= comboSubjectCategory.ClientID %>');
var val = combo.options[combo.selectedIndex].value;

How to clear TextBox text and AJAX CalendarExtender value using Javascript?

I have an ASP.NET user control that contains a textbox which has an AJAX CalendarExtender control which allows users to select a date which then populates the textbox.
I can clear the textbox value using a submit button and setting the value to null in the code behind, but I need to set the value to "" in the textbox using Javascript.
I have tried the following javascript but it didn't work:
function ClearTextBox()
{
document.getElementsByName('TextBox1').Value = "";
}
What javascript should I use?
Instead of Value => value. JavaScript is case sensitive...
getElementsByName returns an array of elements so select the first element with [0]
document.getElementsByName('TextBox1')[0].value = "";
or select the element by id:
document.getElementById('TextBoxId').value = "";

ASP.Net Dropdownlist ListItem (Enabled=false) not showing in page

I have a dropdownlist in aspx called ddlService.
I want to added the listitems from behind.
When I add, I will create them in order of Title and items underneath like..
Title1
Item1
Item2
Title2
Item1
Item2
Titles should not be able to click. Only Items should be able to click.
ListItem tempServicesItem = new ListItem();
tempServicesItem.Text = tempTitle;
tempServicesItem.Value = tempTitle;
tempServicesItem.Enabled = false;
ddlServices.Items.Add(tempServicesItem);
tempServicesItem = new ListItem();
tempServicesItem.Text = tempItem;
tempServicesItem.Value = tempItem;
ddlServices.Items.Add(tempServicesItem);
The problem I encountered is The ListItems with (Enabled=false) are not appearing in aspx.
When I change it to (Enabled=true), it is appearing.
I must have missed out something. Can anyone point out?
Thanks.
I believe this is what you're looking for. (Not tested)
tempServicesItem.Attributes.Add("disabled", "disabled");
MSDN documentation says
You cannot use this property to disable a ListItem control in a
DropDownList control or ListBox control.
I think you need to set the "disabled" attribute which corresponds to the HTML markup for the option element
tempServicesItem.Attributes["disabled"] = "true";

What should be the return type of property while assigning datasource to HTML select in .net

Please advise how I can assign datasource to HTML select control from code behind using a property in asp.net. I want to know what should be the type of property like will it be OBJECT
HTML
<select name="searchKey" id="selDropdown" runat="server"></select>
thanks
You have to list items just like an asp Dropdown list, something like...
selDropdown.Items.Add(new ListItem("text", "Value", true));
I am not sure, but have you tried this?
selDropdown.DataSourceID = "DatasourceID";
selDropdown.DataTextField = "";
selDropdown.DataValueField = "";

asp.net: How can I remove an item from a dropdownlist?

I have a dropdownlist, and in some cases need to remove an item (in the code-behind). I need to remove the item based on the value of the item.
How can I do this?
You can use this:
myDropDown.Items.Remove(myDropDown.Items.FindByValue("TextToFind"));
Code:
ListItem removeItem= myDropDown.Items.FindByValue("TextToFind");
drpCategory.Items.Remove(removeItem);
Replace "TextToFind" with the item you want to remove.
myDropDown.Items.Remove(myDropDown.Items.FindByText("TextToFind"))
You can use the
myDropDown.Items.Remove(ListItem li);
or
myDropDown.Items.RemoveAt(int index);
to remove it using C#.
There is also a slightly simpler way of removing the value.
mydropdownid.Items.Remove("Chicago");
<dropdown id=mydropdown .....>
values
Florida
Texas
Utah
Chicago
As other people have answered, you need to do;
myDropDown.Items.Remove(ListItem li);
but if you want the page to refresh asynchronously, the dropdown needs to be inside an asp:UpdatePanel
after you do the Remove call, you need to call:
yourPanel.Update();
myDropDown.Items.Remove(myDropDown.Items.FindByText("Chicago"));
I have Done Like this, i have remove all items except the value coming as 1 and 3.
ListItemCollection liCol = ddlcustomertype.Items;
for (int i = 0; i < liCol.Count;i++ )
{
ListItem li = liCol[i];
if (li.Value != "1" || li.Value != "3")
ddlcustomertype.Items.Remove(li);
}
Try this code.
If you can add any item and set value in dropdown then try it.
dropdown1.Items.Insert(0, new ListItem("---All---", "0"));
You can Removed Item in dropdown then try it.
ListItem removeItem = dropdown1.Items.FindByText("--Please Select--");
dropdown1.Items.Remove(removeItem);
I would add an identifying Id or class to the dropbox and remove using Javascript.
The article here should help.
D
to insert into DropDownList:
DropDownList.Items.Insert(0, new ListItem("-- Select item --", "0"));
and to remove item from DropDownList:
DropDownList.Items.Remove(new ListItem("-- Select item --", "0"));

Categories