如何获取用户控制下拉列表的ClientID

时间:2017-02-07 05:29:45

标签: javascript c# asp.net user-controls clientid

我创建了一个包含下拉列表的用户控件。我无法获取ClientID以获取所选值或文本。

实施

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

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

ascx文件

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

<div runat="server" id="Reasons">
<asp:DropDownList runat="server" AutoPostBack="true" Width="158"
ID="ReasonPickerDropDown"OnSelectedIndexChanged="
ReasonPickerDropDown_SelectedIndexChanged"
onchange="ChangeVisibility(this)">
</asp:DropDownList>
</div>
</div>

我尝试了以下方法,但没有运气

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

目前我正在使用以下代码段来完成任务。

var reason =
$('#ctl00_MainContentPlaceHolder_ReasonPicker_ReasonPickerDropDown
option:selected').text();

我需要使用&lt;%ClientID%&gt;访问其ClientID来执行此任务。帮我。感谢

2 个答案:

答案 0 :(得分:2)

试试这个

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

OR

var ddlReason =  document.getElementById('<%=ReasonPicker.FindControl("ReasonPickerDropDown").ClientID %>');
var reason = ddlReason.options[ddlReason.selectedIndex].text;

答案 1 :(得分:1)

  1. 覆盖WebControl的ClientId属性;
  2. 然后致电ReasonPicker.ClientID
  3. 示例代码:

    public override string ClientID
    {
        get
        {
            return ReasonPickerDropDown.ClientID;
        }
    }