开发者

Accessing ASP.NET controls dynamically from JavaScript

here's what I mean:

I have a few controls, for example,

<asp:DropDownList ID="ddlDate1" runat="server" />
<asp:DropDownList 开发者_如何学GoID="ddlDate2" runat="server" />
<asp:DropDownList ID="ddlDate3" runat="server" />

in my javascript, I want to do something like this:

for (i = 1; i <= count; i++) {
    something[i] = document.getElementById("<%= ddlDateRange" + i + ".ClientID %>");
}

any way I can make this work, or any alternatives?


Put your dropdown lists into an array. Then you can populate a javascript array like so

var dropDownListIds = [];
<% foreach (DropDownList ddl in myListOfDropDowns) { %>
dropDownListIds.push('<%= ddl.ClientID %>');
<% } %>


Why not use jQuery and just do something like:

<asp:DropDownList CssClass="ddl" ID="ddlDate1" runat="server" />
<asp:DropDownList CssClass="ddl" ID="ddlDate2" runat="server" />
<asp:DropDownList CssClass="ddl" ID="ddlDate3" runat="server" />

$('.ddl').each(function(){
    var ddl = $(this);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜