Get values from ItemTemplate OnClientSelectedIndexChanged

4 posts, 2 answers
  1. KorsG
    KorsG avatar
    8 posts
    Member since:
    Mar 2009

    Posted 26 Apr 2010 Link to this post

    Hi,
    I have a combobox, that when changed, should change the selected value of 2 other comboboxed based on the selection.

    <telerik:RadComboBox ID="RadComboBox1" runat="server"  DataTextField="Name" DataValueField="ActTypeNo"                                              
        OnClientSelectedIndexChanged="OnClientSelectedIndexChanged"  > 
    <ItemTemplate> 
        <table> 
           <tr> 
             <td style="width: 200px;"
                     <asp:Label ID="ChaufName" runat="server" EnableViewState="false" Text='<%#DataBinder.Eval(Container.DataItem, "Name")%>'></asp:Label> 
             </td> 
           <td style="display:none;"
               <asp:Label ID="ChaufGr1" runat="server" EnableViewState="false" Text='<%#DataBinder.Eval(Container.DataItem, "Gr1")%>'></asp:Label> 
               </td> 
           <td style="display:none;"
               <asp:Label ID="ChaufGr2" runat="server" EnableViewState="false" Text='<%#DataBinder.Eval(Container.DataItem, "Gr2")%>'></asp:Label> 
               </td> 
           </tr> 
    </table> 
    </ItemTemplate> 
     </telerik:RadComboBox> 

    My goal is to get the values of "ChaufGr1" and "ChaufGr2" in a javascript function:
    function OnClientSelectedIndexChanged(sender, args) { 
                    //get values 
                   
                    //set selected value of other combos - i know how to do this 
                } 

    I have searched the documentation and forums, but i couldn't find anything exactly like this. 
    Can you point me in the right direction?
  2. Answer
    robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 26 Apr 2010 Link to this post

    You should them as attributes to the RadComboBoxItem similar to this RadListBox example:


    So you could loop through the RadComboBox.Items collection and add the attributes to each one. Then you can retrieve the values from the attributes collection.

    I hope that helps.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 27 Apr 2010 Link to this post

    Hello,

    In fact, Robert said is correct. You can make use of custom attribute in order to get additional values. Here is the example that I tried for similar scenario.

    CS:

     
        protected void RadComboBox1_ItemDataBound(object sender, Telerik.Web.UI.RadComboBoxItemEventArgs e)  
        {  
            DataRowView dataSourceRow = (DataRowView)e.Item.DataItem;  
            //set custom attributes from the datasource:      
            e.Item.Attributes["Gr1"] = dataSourceRow["Gr1"].ToString();  
            e.Item.Attributes["Gr2"] = dataSourceRow["Gr2"].ToString();    
        } 

    JavaScript:

     
    <script type="text/javascript">  
        function OnClientSelectedIndexChanged(sender, args) {  
            var item = args.get_item();  
            // Alert the values  
            alert(item.get_attributes().getAttribute("Gr1"));  
            alert(item.get_attributes().getAttribute("Gr2"));  
        }  
    </script> 

    -Shinu.

  5. KorsG
    KorsG avatar
    8 posts
    Member since:
    Mar 2009

    Posted 28 Apr 2010 Link to this post

    Thank you both, this helped me solve my problem! :-)

    My final js and code behind ended up like this:
    function OnClientSelectedIndexChanged(sender, args) { 
                    //get values 
                    var item = args.get_item(); 
                    var gr1ID = item.get_attributes().getAttribute("Gr1"); 
                    var gr2ID = item.get_attributes().getAttribute("Gr2"); 
                    //set values 
                    var gr1Combo = $find("<%=Gr1Combo.ClientID%>"); 
                    var gr1Item = gr1Combo.findItemByValue(gr1ID); 
                    if (gr1Item) { 
                        gr1Item.select(); 
                    } 
                    var gr2Combo = $find("<%=Gr2Combo.ClientID%>"); 
                    var gr2Item= gr2Combo.findItemByValue(gr2ID); 
                    if (gr2Item) { 
                        gr2Item.select(); 
                    } 

    As i use a LinqDataSource i had to use the databinder
    Protected Sub Chauffeurs_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadComboBoxItemEventArgs) Handles Chauffeurs.ItemDataBound  
            e.Item.Attributes("Gr1") = DataBinder.Eval(e.Item.DataItem, "Gr1")  
            e.Item.Attributes("Gr2") = DataBinder.Eval(e.Item.DataItem, "Gr2")  
        End Sub  
     


Back to Top