我找不到动态创建的RadComboBox

时间:2013-10-18 10:04:29

标签: jquery asp.net vb.net telerik radcombobox

我动态创建一些RadCombobox。用户选择后,我尝试从RadComboBox获取值。但我找不到控制。

这是我的代码:

asp.net:

    <asp:Panel ID="pnlErsteMenuesAnlegenSchritt3" runat="server">
        <ul runat="server" id="MenuAusMenuPunktenZusammengesetzt" class="MenuColletionPos">
        </ul>

        <telerik:RadButton ID="rtbnMenuElementeHrefEingabeVerarbeiten" runat="server" Text="Weiter" OnClientClicking="CollectMenuElementeWithHref"></telerik:RadButton>
    </asp:Panel>

vb.net:

    Public Sub MenuHrefAnlegen(ByVal endVal As Integer, ByVal strArrQuerystring As Array)

    Dim width As String = "width:199px;"
    Dim count As Integer = 1
    Dim i As Integer
    Dim startval As Integer
    startval = 1
    For i = startval To endVal
        'erzeuge menü element

        Dim combo As New RadComboBox()
        combo.ID = "rcbHrefAuswaehlen" & count
        combo.EnableLoadOnDemand = True
        combo.Skin = "Metro"
        combo.Items.Insert(0, New RadComboBoxItem("Über uns", String.Empty))
        combo.Items.Insert(1, New RadComboBoxItem("Baufinanzierung", String.Empty))
        combo.Items.Insert(2, New RadComboBoxItem("Denkmal-Immobilien", String.Empty))
        combo.Items.Insert(3, New RadComboBoxItem("Photovoltaik", String.Empty))
        combo.Items.Insert(4, New RadComboBoxItem("Assekuranz", String.Empty))
        combo.Items.Insert(5, New RadComboBoxItem("Kontaktseite", String.Empty))
        combo.Items.Insert(6, New RadComboBoxItem("Impressum", String.Empty))
        combo.Items.Insert(7, New RadComboBoxItem("Benutzerdefinierte Seite", String.Empty))

        Dim hfMenuElement As New HiddenField
        hfMenuElement.ID = "hfMenuElement" + count.ToString()
        hfMenuElement.Value = strArrQuerystring(count)

        MenuAusMenuPunktenZusammengesetzt.Controls.Add(New LiteralControl("<li class=""floatKill""><a href=""#"" id=""hrefm" & count & """ runat=""server"" onmouseover=""menuov('menuleft" & count & "', 'menupuffer" & count & "', 'menuright" & count & "');"" onmouseout=""menuovf('menuleft" & count & "', 'menupuffer" & i & "', 'menuright" & count & "');""><div id=""menuleft" & count & """ class=""menu_left""></div><div id=""menupuffer" & count & """ class=""menu_puffer"" style=""" & width & """>" & strArrQuerystring(count) & "</div><div id=""menuright" & count & """ class=""menu_right""></div></a><div class=""pfeil links"" style=""margin-left:15px;margin-top:8px;""></div><div class=""pfeil links"" style=""margin-top:8px;""></div><div class=""pfeil links"" style=""margin-right:15px;margin-top:8px;""></div></li>"))
        MenuAusMenuPunktenZusammengesetzt.Controls.Add(combo)
        MenuAusMenuPunktenZusammengesetzt.Controls.Add(hfMenuElement)

        count += 1
    Next

End Sub

jquery的:

           function CollectMenuElementeWithHref(sender, eventArgs) {
            var VID = $('input[id$=hfVermittlerID]').val();
            var obj = { ID: VID };
            for (var i = 1; i < 9; i++) {
                var hfVal = $('[id$=hfMenuElement' + i + ']').val();

                //At this point i get Null back
                var combo = $find("<%= rcbHrefAuswaehlen" + i + ".ClientID %>");
                var item = combo.get_items().getItem(2);

                if (hfVal) {
                    obj["pa" + i] = hfVal
                    obj["href" +1] = item.get_text()
                }
            }
            eventArgs.set_cancel(true);
            window.location.href = 'VermittlerBearbeiten.aspx?' + $.param(obj);
        }

我总是得到Null。

var combo = $find("<%= rcbHrefAuswaehlen" + i + ".ClientID %>");

我有什么办法可以创建这个控件吗? 我希望有人可以帮助我。

谢谢大家阅读:)

2 个答案:

答案 0 :(得分:1)

找到RadCombobox。请在创建控件后检查是否正在调用javascript

var combo = $find("<%= rcbHrefAuswaehlen" + i + ".ClientID %>");//u r doing right

OR

var combo = $("[id$='rcbHrefAuswaehlen''"+ i +"']");

HELP

答案 1 :(得分:1)

请尝试使用以下代码段。

 var combo = $find("rcbHrefAuswaehlen" + i);

完整演示代码

<强> ASPX

<html>
<head runat="server">
    <title></title>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script>

            function CollectMenuElementeWithHref() {
                for (var i = 0; i < 3; i++) {
                    var combo = $find("rcbHrefAuswaehlen" + i);
                    var item = combo.get_items().getItem(1);
                }
            }

        </script>
    </telerik:RadCodeBlock>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
            <asp:Panel ID="pnlCombo" runat="server"></asp:Panel>
            <telerik:RadButton ID="rtbnMenuElementeHrefEingabeVerarbeiten" runat="server" Text="Weiter" OnClientClicking="CollectMenuElementeWithHref"></telerik:RadButton>
        </div>
    </form>
</body>
</html>

<强> ASPX.CS

protected void Page_Init(object source, System.EventArgs e)
{
    GenerateCombo();
}


protected void GenerateCombo()
{
    for (int i = 0; i < 3; i++)
    {
        RadComboBox r1 = new RadComboBox();
        r1.ID = "rcbHrefAuswaehlen" + i;
        r1.Items.Add(new RadComboBoxItem() { Text = "a", Value = "b" });
        r1.Items.Add(new RadComboBoxItem() { Text = "aa", Value = "bb" });
        r1.Items.Add(new RadComboBoxItem() { Text = "aaa", Value = "bbb" });
        pnlCombo.Controls.Add(r1);
    }
}

如果我不明白你的问题,请告诉我。