Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

i am trying to fetch html dropdownlist selected text from asp.net

i know how to fetch html dropdownlist value.
i want to know how to fetch its selected text.

To Fetch dropdownlist selected value.

C#
Response.Write(Request["ddlCountry"]);


Please tell me how to fetch dropdownlist text.


Regards,
SUNIL
Posted

I created HiddenField in jQuery ande allocated DropDown text to HiddenField.
JavaScript
$('[id$=ddlCountry]').change(function () {
    var selectedOption = $("#ddlCountry option:selected").text();
    $("<input/>", { type: 'hidden', name: 'CountryName' }).val(selectedOption).appendTo("form");

    alert("Handler for .change() called.");
});


In code behind
C#
Response.Write(Request["CountryName"]);


To read HiddenField text
So I got text in my code behind.

Thanks everyone for participating.

Regards,
SUNIL
 
Share this answer
 
v2
Comments
Nice Workaround... :) My 5+. :)
If you have a select element that looks like this:
XML
<select id="ddlViewBy">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>

Running this code:
JavaScript
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;

Would make strUser be 2. If what you actually want is test2, then do this:
JavaScript
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].text;

Which would make strUser be test2
 
Share this answer
 
v3
Comments
sunil mali 10-Sep-13 3:46am    
Sir i want to fetch it in my c# code
For HTML DropDownList

Add runat="server" attribute to mark-up and use the following code in code behind.
C#
string selectedCountry = ddlCountry.Items[ddlCountry.SelectedIndex].Text;
 
Share this answer
 
v3
Comments
sunil mali 10-Sep-13 3:53am    
my dropdownlist is html control.
it will not work this way
Add runat="server" attribute to that DropDownList.
Did you add and check?
sunil mali 10-Sep-13 4:14am    
it gives me null.
Ok... Let me try it at my end. I will reply back soon.
Hello all,

I created hidenn field in jquery ande allocated dropdown text to hidden field

Collapse | Copy Code
$('[id$=ddlCountry]').change(function () {
var selectedOption = $("#ddlCountry option:selected").text();
$("<input/>", { type: 'hidden', name: 'CountryName' }).val(selectedOption).appendTo("form");

alert("Handler for .change() called.");
});


In code behind
i used
Response.Write(Request["CountryName"]);

To read hidden field text
so i got text in my codebehined.
Thanks everyone for participating.

Regards,
SUNIL
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900