This is a migrated thread and some comments may be shown as answers.
Depenent ListBoxes
2 Answers 46 Views
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 19 Aug 2009, 02:52 PM
I have a scenario where I want to select a "State" from listbox A and have it dynamically update listbox B with related "Counties".  My datasource would be SQL Server tables.  What would be the most efficient way to program this using Ajax?

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 1
answered on 20 Aug 2009, 08:18 AM
Hello Stephen,

One suggestion is getting the ListItem in OnClientSelectedIndexChanged event of RadListBox and make a ajaxrequest and populate in the ajaxRequest event handler on the server. Here is the code that I tried.

ASPX:
 
<telerik:RadListBox runat="server" ID="RadListBoxSource" Height="200px" Width="230px" 
    OnClientSelectedIndexChanged="OnClientSelectedIndexChanged"
    <Items> 
     . . . 
    </Items> 
</telerik:RadListBox> 
<telerik:RadListBox runat="server" ID="RadListBoxDestination" Height="200px" Width="200px"
</telerik:RadListBox> 
 
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest"
    <AjaxSettings> 
        <telerik:AjaxSetting AjaxControlID="RadAjaxManager1"
            <UpdatedControls> 
                <telerik:AjaxUpdatedControl ControlID="RadListBoxDestination" /> 
            </UpdatedControls> 
        </telerik:AjaxSetting> 
    </AjaxSettings> 
</telerik:RadAjaxManager> 

JavaScipt:
 
<script type="text/javascript"
var ajaxmanager; 
function pageLoad() 
    ajaxmanager = $find("<%=RadAjaxManager1.ClientID%>"); 
function OnClientSelectedIndexChanged(sender, args) 
{    
    var text = args.get_item().get_text();     
    ajaxmanager.ajaxRequest(text); 
</script> 

C#:
 
protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e) 
    string itemText = e.Argument.ToString(); 
    // SQL query with parameter itemText in order to populate the second ListBox 
    // Set the DataSource for second ListBox 
    RadListBoxDestination.DataBind(); 

-Shinu.
0
Stephen
Top achievements
Rank 1
answered on 20 Aug 2009, 03:59 PM
Thank you very much for your expert help.  I tested the code today from my project and it works great.
Asked by
Stephen
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Stephen
Top achievements
Rank 1
Share this question
or