How to get selected text from HTML dropdown in asp.net without using runat server?


In this get selected text of dropdown in asp.net tutorial, we will discuss how to get selected text from an HTML dropdown list without using runat=”server” attribute in asp.net.


New to Office 365 SharePoint Online? Get Office 365 Enterprise E3 Subscription & Try out all the features

Here I have a html dropdownlist which have some options like below:

<select name=”service” id=”ddlservice”>
<option value=”0″>SharePoint Project Work</option>
<option value=”1″>SharePoint classroom training</option>
<option value=”2″>SharePoint Corporate training</option>
<option value=”3″>SharePoint Online training</option>
</select>

I wanted to get the selected value in the .cs file. It does not have a runat=”server” attribute, it is not straightforward.

For this, first I added a hidden variable below to the drop down list like below:

<input type=”hidden” id=”hfServicename” name=”serviceName” />

Then I wrote a small JavaScript function which will take the selected value and assign into the hidden variable.

<script type=”text/javascript”>
function SetSelectedText(ddlservice) {
var selectedText = ddlservice.options[ddlservice.selectedIndex].innerHTML;
document.getElementById(“hfServicename”).value = selectedText;
}
</script>

Then I call this javascript function in the onchange event of the dropdownlist like below:

<select name=”service” id=”ddlservice” onchange=”SetSelectedText(this)”>

Then below is the way you can access the value from the variable.


string service = Request.Form[“serviceName”];

You may like following asp.net tutorial:

Hope this will be helpful to how to get selected text from HTML dropdownlist in asp.net without using runat server?

Check out Best Alternative to InfoPath -> Try Now

FREE SHAREPOINT 2019 PDF

(Installation & Features)



About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →