我有一个被填充的未绑定(bind)列表。
<asp:DropDownList ID="ddlState" runat="server" style="margin-left: 81px"
onselectedindexchanged="ddlState_SelectedIndexChanged"
CssClass="styled-select">
</asp:DropDownList>
<asp:DropDownList ID="ddlManagerName" runat="server" Height="22px"
style="margin-left: 08px; width:auto;" ></asp:DropDownList>
和一个运行以在单击按钮时获取 id 的 javascript
function validateDynamic() {
var getState = document.getElementById('<%=ddlState.ClientID %>').selectedIndex;
var getManager = document.getElementById('<%=ddlManagerName.ClientID %>').selectedIndex;
if(getState == 0)
{
alert("State is a required field !");
}
if(getManager == 0)
{
alert("Manager Name is a required field !");
}
return false;
}
我已经尝试使用查看源代码中的以下内容
name="ctl00$MainContent$ddlState"id="MainContent_ddlState"name 和 id,我得到的只是 undefined object 。
这是调用函数的按钮代码
<asp:Button id="btnSaveTM" runat="server" Text="Add Team Member" class="btn" onmouseover="this.className='btn btnhov'"
onmouseout="this.className='btn'" style=" Height:34px" OnClientClick="validateDynamic();"
onclick="btnSaveTM_Click" UseSubmitBehavior="true" CausesValidation="true" />
当我调试时,我得到 getState 和 manager 未定义。感谢您的帮助。
最佳答案
使用它来获取 DropdownList 的选定值:
var getState = (document.getElementById('<%= ddlState.ClientID %>')
.options[document.getElementById("<%=ddlState.ClientID%>")
.selectedIndex].value)
关于c# - 无法获取 asp :dropdownlist which gets populated dynamically 的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18729709/