Harsha W Harsha W - 4 years ago 105
ASP.NET (C#) Question

How to acquire the ClientID of User Control Drop Down List

I have created a user control containing a drop down list. I am unable to acquire the ClientID in order to get the selected value or text.


<%@ Register Src="~/Common/Controls/ReasonPicker.ascx"
TagName="ReasonPicker" TagPrefix="uc2" %>

<uc2:ReasonPicker ID="ReasonPicker" Width="100%" AutoPostBack="false"
runat="server" AddAny="True" />

ascx file

<%@ Control Language="C#" AutoEventWireup="true"
Inherits="********.********.com.web.Common.Controls.ReasonPicker" %>

<div runat="server" id="Reasons">
<asp:DropDownList runat="server" AutoPostBack="true" Width="158"

I tried the following methods but no luck

var reason = $('<%=ReasonPicker.ClientID%> option:selected').text();
var reason = $('<%=ReasonPickerDropDown.ClientID%> option:selected').text();

Currently I am using the below code segment in order to complete the task.

var reason =

I need to do this task by accessing its ClientID using <%ClientID%>. Help Me. Thanks

Answer Source

Try this

var reason = $('<%=ReasonPicker.ClientID%>').find('select option:selected').text();


var ddlReason =  document.getElementById('<%=ReasonPicker.FindControl("ReasonPickerDropDown").ClientID %>');
var reason = ddlReason.options[ddlReason.selectedIndex].text;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download