This is a migrated thread and some comments may be shown as answers.

Can't find Combobox

4 Answers 85 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 17 Oct 2013, 01:23 PM
Hi Guys, i hope someone can help me with my Problem.

It adding a bunch of RadComboBox dynamiclly on PageLoad to my Page. This works without Problems.
But if i want to find any Combobox by $find i always get Null back.

Has this something to do with kind of adding?

Here is my Code:

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

And my find function:

            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);
            }

Thank you for reading :)
Daniel

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 18 Oct 2013, 03:32 AM
Hi Daniel,

Please have a look into the sample code that I tried to access a dynamically created RadComboBox in JavaScript.

ASPX:
<telerik:RadMenu ID="RadMenu1" runat="server" OnClientItemClicked="OnClientItemClicked1">
    <Items>
        <telerik:RadMenuItem Text="Menu1" runat="server">
        </telerik:RadMenuItem>
    </Items>
</telerik:RadMenu>

C#:
protected void Page_Load(object sender, EventArgs e)
{
    RadComboBox combo = new RadComboBox();
    combo.ID = "RadComboBox1";
    combo.Skin = "Metro";
    combo.EnableLoadOnDemand = true;
    combo.Items.Insert(0,new RadComboBoxItem("Item1","1"));
    combo.Items.Insert(1,new RadComboBoxItem("Item2","2"));
    combo.Items.Insert(2,new RadComboBoxItem("Item3","3"));
    combo.Items.Insert(3,new RadComboBoxItem("Item4","4"));
    combo.Items.Insert(4,new RadComboBoxItem("Item5","5"));
    RadMenu1.Controls.Add(combo);
}

JavaScript:
<script type="text/javascript">
    function OnClientItemClicked1(sender, args) {
        var combobox = $find('<%= RadMenu1.FindControl("RadComboBox1").ClientID %>');//accessing radcombobox
    }
</script>

Hope this will helps you.
Thanks,
Shinu.
0
Daniel
Top achievements
Rank 1
answered on 18 Oct 2013, 08:01 AM
Okay thank you, i will try this.
But for understanding, does it only work in this way and if yes, why?

EDIT:

Unfortunately it still not working.
I can creat the Combobox like in your example. But if i wanna get the value by javascript, i still can't find the Control.
What i'm doing wrong? :/

I tried it like this:

function CollectMenuElementeWithHref(sender, eventArgs) {
       .... 
 //Still get Null back
var combobox = $find('<%= RadMenu1.FindControl("<%= rcbHrefAuswaehlen" + i + ".ClientID %>").ClientID %>');//accessing radcombobox
//This also not working
        //var combobox = $find('<%= RadMenu1.FindControl("rcbHrefAuswaehlen' + i + '").ClientID %>');//accessing radcombobox
        var
item = combobox.get_items().getItem(2);
       ....
  }


        <asp:Panel ID="pnlErsteMenuesAnlegenSchritt3" runat="server">
            
            <telerik:RadMenu ID="RadMenu1" runat="server">
                <Items>
                    <telerik:RadMenuItem Text="Menu1" runat="server">
                    </telerik:RadMenuItem>
                </Items>
            </telerik:RadMenu>

            <telerik:RadButton ID="rtbnMenuElementeHrefEingabeVerarbeiten" runat="server" Text="Weiter" OnClientClicking="CollectMenuElementeWithHref"></telerik:RadButton>
        </asp:Panel>
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>"))
    '    This works without Problems
RadMenu1.Controls.Add(combo)
        MenuAusMenuPunktenZusammengesetzt.Controls.Add(hfMenuElement)
 
        count += 1
    Next
 
End Sub

Thank you,
Daniel
0
Daniel
Top achievements
Rank 1
answered on 18 Oct 2013, 12:17 PM
Sorry for Doublepost.

I can get the Control like this:

function CollectMenuElementeWithHref(sender, eventArgs) {
 ...
        var comboID = "rcbHrefAuswaehlen" + i
        var combo = $("[id$=" + comboID +"]");
        var item = combo.get_items().getItem(1);
  ....
}

But now i can't get the item :D
I get this Error:
TypeError: combo.get_items is not a function


Is this actually the right way?
0
Shinu
Top achievements
Rank 1
answered on 21 Oct 2013, 06:43 AM
Hi Daniel,

Using $find("<%= ControlID.ClientID %>") is the recommended way to get reference to client-side objects. In the above code you tried, the ID selector Selects a single element with the given id attribute which wont return the controls client object and hence the get_items() method wont work. <% %> are server side codes and you cannot get the Server side expression running by concatenation using JavaScript. When you are using $find to access a control from client side, the ControlID should be the ID of any server control on that page and there you cannot have any variable name which holds the ID of the desired control.

Thanks,
Shinu.

Tags
ComboBox
Asked by
Daniel
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Daniel
Top achievements
Rank 1
Share this question
or