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

Multiple Combos for Address

8 Answers 173 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Trimox
Top achievements
Rank 1
Trimox asked on 26 Feb 2008, 04:17 PM
using the below code..

I select an item in the City combo and it does a SetFocus to the Zip combo. It automatically puts the first zipcode in the ZipCode.Text field. I can't seem to get this to work no matter what I've tried..It always has the previous ZipCode selection in the list

Based on a City selection I'd like to auto select a Zip if only one is available and then auto select the County and State. But I also have to allow custom text in City and Zip for Out Of State entries..

            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                    <asp:SqlDataSource ID="SqlradCity" runat="server"
                        ConnectionString="<%$ ConnectionStrings:db_DELJISDataConnectionString %>"                                
                        SelectCommand="SELECT DISTINCT [Description] FROM [Zip_Codes] ORDER BY Description ASC">
                    </asp:SqlDataSource>
                    <asp:SqlDataSource ID="SqlradZipMail" runat="server"
                        ConnectionString="<%$ ConnectionStrings:db_DELJISDataConnectionString %>"
                        SelectCommand="SELECT [Code],[County] FROM [Zip_Codes] WHERE ([Description] = @Description) ORDER BY Code ASC">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="radCityMail" Name="Description"
                                PropertyName="Text" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                    <table>
                        <tr>
                            <td>
                                City:&nbsp;
                                <telerik:RadComboBox id="radCityMail" runat="server" CausesValidation="False"
                                    Skin="Outlook" AllowCustomText="True" Height="150px" MarkFirstMatch="True"
                                    MaxLength="18" AutoPostBack="true" OnTextChanged="radCity_TextChanged"
                                    DataSourceID="SqlradCity" DataTextField="Description" CollapseAnimation-Type="None" />
                            </td>
                            <td>
                                Zip Code:&nbsp;
                                <telerik:RadComboBox id="radZipCodeMail" runat="server" MaxLength="9"
                                    Skin="Outlook" AllowCustomText="True" Height="150px" Width="70px" EnableLoadOnDemand="true"
                                    CausesValidation="False" AutoPostBack="true" OnTextChanged="radZipCode_TextChanged"
                                    DataSourceID="SqlradZipMail" DataTextField="Code" DataValueField="County" CollapseAnimation-Type="None" />
                            </td>
                            <td>
                                County:&nbsp;
                                <asp:DropDownList ID="ddlCountyMail" runat="server">
                                    <asp:ListItem />
                                    <asp:ListItem Value="N" Text="New Castle County" />
                                    <asp:ListItem Value="K" Text="Kent County" />
                                    <asp:ListItem Value="S" Text="Sussex County" />
                                    <asp:ListItem Value="O" Text="Other" />
                                </asp:DropDownList>
                            </td>
                            <td>
                                State:&nbsp;
                                <asp:DropDownList ID="ddlStateMail" runat="server" />
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>


And Code Behind
        #region City/ZIP Events
        /* Handles ALL rad city textchanged events */
        protected void radCity_TextChanged(object sender, EventArgs e) {
            Telerik.Web.UI.RadComboBox zip = null, city = null;
            if(sender.Equals(radCityMail)) { // mail address
                zip = radZipCodeMail; city = radCityMail;
            }
            if(sender.Equals(radCityPhysical)) { // physical address
                zip = radZipCodePhysical; city = radCityPhysical;
            }

            if(city.SelectedIndex > -1) { // in-state
                zip.AllowCustomText = false;
            } else { // out of state
                zip.AllowCustomText = true;
            }
            SetFocus(zip);
            radZipCode_TextChanged(zip, null);
        }

        /* Handles ALL rad zip code textchanged events */
        protected void radZipCode_TextChanged(object sender, EventArgs e) {
            Telerik.Web.UI.RadComboBox zip = null, city = null;
            DropDownList county = null, state = null;
           
            if(sender.Equals(radZipCodeMail)) { // mail address
                zip = radZipCodeMail; city = radCityMail;
                county = ddlCountyMail; state = ddlStateMail;
            }
            if(sender.Equals(radZipCodePhysical)) { // physical address
                zip = radZipCodePhysical; city = radCityPhysical;
                county = ddlCountyPhysical; state = ddlStatePhysical;
            }

            if(city.SelectedItem != null) { // in-state
                state.ClearSelection(); state.SelectedIndex = -1;
                county.ClearSelection(); county.SelectedIndex = -1;
                county.Items.FindByValue("O").Enabled = false;
                state.Items.FindByValue("DE").Enabled = true;
                state.Items.FindByValue("DE").Selected = true;
                state.Enabled = false; county.Enabled = true;
                county.Items.FindByValue(zip.SelectedValue).Selected = true;
                SetFocus(county);
            } else { // out of state
                state.ClearSelection(); state.SelectedIndex = -1;
                county.ClearSelection(); county.SelectedIndex = -1;
                state.Items.FindByValue("DE").Enabled = false; state.Enabled = true;
                county.Items.FindByValue("O").Enabled = true;
                county.Items.FindByValue("O").Selected = true; county.Enabled = false;
                SetFocus(state);
            }
        }
        #endregion

8 Answers, 1 is accepted

Sort by
0
Rosi
Telerik team
answered on 29 Feb 2008, 09:58 AM
Hi Trimox,

I suggest you that achieve your task by using our approach illustrated here.

Regards,
Rosi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Trimox
Top achievements
Rank 1
answered on 29 Feb 2008, 02:12 PM
I have tried that as well, however the e.text value is always blank on the LoadCountries().

Suggestions?
0
Trimox
Top achievements
Rank 1
answered on 29 Feb 2008, 02:42 PM
I just saw that you updated the documentation for the clientdatastring related to this demo. It works great the first pass through, however if I change the Contienent it does not see a new FilterString on a 2nd attempt..
0
Trimox
Top achievements
Rank 1
answered on 29 Feb 2008, 05:18 PM
I got some more issues ironed out. However on the 2nd combobox. If there are multiple selections in the list and I click any item but the 1st item and then click outside the box it resets it back to item 0.
0
Rosi
Telerik team
answered on 03 Mar 2008, 10:30 AM
Hello Trimox,

Please see our online example illustrating multiple selection (the second RadComboBox). Could you reproduce the problem by using this demo?

Also, could you explain more about the problem for the FilterString? Could you provide us steps to reproduce or open a support ticket and send us a simple running project illustrating the problem? We will test it locally and provide you with more specific instructions.

Regards,
Rosi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Trimox
Top achievements
Rank 1
answered on 03 Mar 2008, 01:20 PM
Attached is my new code. The City dropdown is still resetting to the first item in the list if there are more then one available in the list. The others all appear to work fine.

            <table>
                <tr>
                    <td>
                        City:&nbsp;
                        <telerik:RadComboBox id="radCityMail" runat="server"
                            Skin="Outlook" AllowCustomText="True" Height="150px" MarkFirstMatch="True"
                            MaxLength="18" OnClientSelectedIndexChanging="LoadZipCodes" OnItemsRequested="radCity_ItemsRequested"
                            CollapseAnimation-Type="None" />
                    </td>
                    <td>
                        Zip Code:&nbsp;
                        <telerik:RadComboBox id="radZipCodeMail" runat="server" MaxLength="9"
                            Skin="Outlook" Height="150px" Width="70px"
                            OnClientSelectedIndexChanging="LoadCounty" EnableLoadOnDemand="true"
                            OnClientItemsRequested="ItemsLoaded" OnItemsRequested="radZipCode_ItemsRequested"
                            CollapseAnimation-Type="None" />
                    </td>
                    <td>
                        County:&nbsp;
                        <telerik:RadComboBox id="radCountyMail" runat="server" Skin="Outlook" Height="150px" Width="100px">
                            <Items>
                                <telerik:RadComboBoxItem runat="server" />
                                <telerik:RadComboBoxItem runat="server" Value="N" Text="New Castle" />
                                <telerik:RadComboBoxItem runat="server" Value="K" Text="Kent County" />
                                <telerik:RadComboBoxItem runat="server" Value="S" Text="Sussex" />
                                <telerik:RadComboBoxItem runat="server" Value="O" Text="Other" />
                            </Items>
                        </telerik:RadComboBox>
                      </td>
                    <td>
                        State:&nbsp;
                        <telerik:RadComboBox id="radStateMail" runat="server" MaxLength="9"
                            Skin="Outlook" Height="150px" Width="130px" />
                    </td>
                </tr>
            </table>


Javascript:
    <script type="text/javascript">
        function LoadZipCodes(combo, eventarqs) {
               var zipCodeCombo;
               if(combo.get_id() == "ctl00_ContentPlaceHolder1_radCityMail") {
                zipCodeCombo = $find("ctl00_ContentPlaceHolder1_radZipCodeMail");
            } else if(combo.get_id() == "ctl00_ContentPlaceHolder1_radCityPhysical") {
                zipCodeCombo = $find("ctl00_ContentPlaceHolder1_radZipCodePhysical");
            }

            var item = eventarqs.get_item();          
            if (item.get_index() > 0) {
                zipCodeCombo.requestItems(item.get_text(), false);
            } else {
                zipCodeCombo.set_text("");
                zipCodeCombo.clearItems();
            }
        }

        function LoadCounty(combo, eventarqs) {
            var countyCombo;
               var stateCombo;
               if(combo.get_id() == "ctl00_ContentPlaceHolder1_radZipCodeMail") {
                countyCombo = $find("ctl00_ContentPlaceHolder1_radCountyMail");
                stateCombo = $find("ctl00_ContentPlaceHolder1_radStateMail");
            } else if(combo.get_id() == "ctl00_ContentPlaceHolder1_radZipCodePhysical") {
                countyCombo = $find("ctl00_ContentPlaceHolder1_radCountyPhysical");
                stateCombo = $find("ctl00_ContentPlaceHolder1_radStatePhysical");
            }

            var item = eventarqs.get_item();
            if (item.get_index() == 0) {
                countyCombo.findItemByValue(item.get_value()).select();
                stateCombo.findItemByValue("DE").select();
            }
        }
       
        function ItemsLoaded(combo, eventarqs) {
            if (combo.get_items().get_count() > 0) {
                combo.set_text(combo.get_items().getItem(0).get_text());
                combo.get_items().getItem(0).highlight();
                combo.showDropDown();
            }
        }
    </script>


Code-Behind:
        #region City/ZIP Events
        /* Handles ALL rad city item requested events */
        protected void radCity_ItemsRequested(object o, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e) {
            initCity((Telerik.Web.UI.RadComboBox)o);
        }

        /* Handles ALL rad zip code item requested events */
        protected void radZipCode_ItemsRequested(object o, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e) {
            loadZipCodes((Telerik.Web.UI.RadComboBox)o, e.Text);
        }
        #endregion

And the loading of the data..
        void initCity(Telerik.Web.UI.RadComboBox city) {
            try {
                SqlConnection cn = new SqlConnection("User ID=XXX;Initial Catalog=CJIS_Data;Data Source=172.31.70.140;PASSWORD=XXX;");

                SqlCommand cmd = new SqlCommand("SELECT DISTINCT [Description] FROM [Zip_Codes] ORDER BY Description ASC", cn);
                cn.Open();
                SqlDataReader dr = (SqlDataReader)cmd.ExecuteReader();

                city.Items.Clear();
                while(dr.Read()) {
                    Telerik.Web.UI.RadComboBoxItem item = new Telerik.Web.UI.RadComboBoxItem(dr["Description"].ToString());
                    city.Items.Add(item);
                }
                dr.Close();
                cn.Close();
            } catch { }
        }
        void loadZipCodes(Telerik.Web.UI.RadComboBox zip, string cityID) {
            try {
                SqlConnection cn = new SqlConnection("User ID=XXX;Initial Catalog=CJIS_Data;Data Source=172.31.70.140;PASSWORD=XXX;");

                SqlCommand cmd = new SqlCommand("SELECT [Code],[County] FROM [Zip_Codes] WHERE [Description] = '" + cityID + "' ORDER BY Code ASC", cn);
                cn.Open();
                SqlDataReader dr = (SqlDataReader)cmd.ExecuteReader();

                zip.Items.Clear();
                while(dr.Read()) {
                    Telerik.Web.UI.RadComboBoxItem item = new Telerik.Web.UI.RadComboBoxItem(dr["Code"].ToString());
                    zip.Items.Add(item);
                    zip.Items.FindItemByText(dr["Code"].ToString()).Value = dr["County"].ToString().ToUpper();
                }
                dr.Close();
                cn.Close();
            } catch { }
        }
        void initState() {
            try {
                SqlDataSource sdS = new SqlDataSource();
                sdS.ConnectionString = "User ID=XXX;Initial Catalog=CJIS_Data;Data Source=172.31.70.140;PASSWORD=XXX;";
                sdS.SelectCommand = "SELECT Code,Description FROM State_Country_Codes ORDER BY Description";

                radStatePhysical.DataTextField = "Description";
                radStatePhysical.DataValueField = "Code";
                radStatePhysical.DataSource = sdS;
                radStatePhysical.DataBind();

                radStateMail.DataTextField = "Description";
                radStateMail.DataValueField = "Code";
                radStateMail.DataSource = sdS;
                radStateMail.DataBind();
            } catch { }


0
Karl
Top achievements
Rank 1
answered on 12 Mar 2008, 07:52 AM
Hi Trimox,

Your thread just breaks off without a satisfying sollution.  Was a satisfying solution found?  If so, what was the problem.  I'm curious because I am in a similar situation and have regressed away from prometheus but wouldn't mind to find out what the problem is.

Karl 
0
Accepted
Trimox
Top achievements
Rank 1
answered on 12 Mar 2008, 12:54 PM
Actually Telerik support found the issue after I sent a support request and example project for them.

Turns out that the value for the ZipCode dropdown had duplicates. Which was causing it to reset to the first item in the list for certain ZipCodes. The value for zipcode was the county, and some of our zip's have the same county. So what I did was made the County an attribute and made the value blank..

Below is the working code..

This is the javascript:
    <script type="text/javascript">
        function LoadZipCodes(combo, eventarqs) {
               var zipCodeCombo; var countyCombo; var stateCombo;

               if(combo.get_id() == "ctl00_ContentPlaceHolder1_radCityMail") {
                zipCodeCombo = $find("ctl00_ContentPlaceHolder1_radZipCodeMail");
                countyCombo = $find("ctl00_ContentPlaceHolder1_radCountyMail");
                stateCombo = $find("ctl00_ContentPlaceHolder1_radStateMail");
            } else if(combo.get_id() == "ctl00_ContentPlaceHolder1_radCityPhysical") {
                zipCodeCombo = $find("ctl00_ContentPlaceHolder1_radZipCodePhysical");
                countyCombo = $find("ctl00_ContentPlaceHolder1_radCountyPhysical");
                stateCombo = $find("ctl00_ContentPlaceHolder1_radStatePhysical");
            }

            var item = eventarqs.get_item();
            zipCodeCombo.clearSelection();
            countyCombo.clearSelection();
            stateCombo.clearSelection();
            zipCodeCombo.requestItems(item.get_text(), false);
        }

        function LoadCounties(combo, eventarqs) {
            var stateCombo; var countyCombo;
               if(combo.get_id() == "ctl00_ContentPlaceHolder1_radZipCodeMail") {
                countyCombo = $find("ctl00_ContentPlaceHolder1_radCountyMail");
                stateCombo = $find("ctl00_ContentPlaceHolder1_radStateMail");
            } else if(combo.get_id() == "ctl00_ContentPlaceHolder1_radZipCodePhysical") {
                countyCombo = $find("ctl00_ContentPlaceHolder1_radCountyPhysical");
                stateCombo = $find("ctl00_ContentPlaceHolder1_radStatePhysical");
            }
            stateCombo.findItemByValue("DE").select();
            var item = eventarqs.get_item();
            countyCombo.findItemByValue(item.get_attributes().getAttribute("County")).select();
        }
      
        function ItemsLoaded(combo, eventarqs) {
            if (combo.get_items().get_count() > 0) {
                combo.set_text(combo.get_items().getItem(0).get_text());
                combo.get_items().getItem(0).highlight();
            }
            combo.showDropDown();
        }       
    </script>
Here is the HTML:
            <table>
                <tr>
                    <td>
                        City:&nbsp;
                        <telerik:RadComboBox id="radCityMail" runat="server" MaxLength="18"
                            OnClientSelectedIndexChanging="LoadZipCodes"
                            Skin="Outlook" AllowCustomText="True" MarkFirstMatch="True" />
                    </td>
                    <td>
                        Zip Code:&nbsp;
                        <telerik:RadComboBox id="radZipCodeMail" runat="server" MaxLength="9"
                            Skin="Outlook" Width="70px" OnClientItemsRequested="ItemsLoaded"
                            OnClientSelectedIndexChanging="LoadCounties" AllowCustomText="True"
                            OnItemsRequested="radZipCode_ItemsRequested" />
                    </td>
                    <td>
                        County:&nbsp;
                        <telerik:RadComboBox id="radCountyMail" runat="server" Skin="Outlook" Width="100px">
                            <Items>
                                <telerik:RadComboBoxItem runat="server" Value="" Text="" />                              
                                <telerik:RadComboBoxItem runat="server" Value="N" Text="New Castle" />
                                <telerik:RadComboBoxItem runat="server" Value="K" Text="Kent County" />
                                <telerik:RadComboBoxItem runat="server" Value="S" Text="Sussex" />
                                <telerik:RadComboBoxItem runat="server" Value="O" Text="Other" />
                            </Items>
                        </telerik:RadComboBox>
                      </td>
                    <td>
                        State:&nbsp;
                        <telerik:RadComboBox id="radStateMail" runat="server"
                            Skin="Outlook" Width="130px" />
                    </td>
                </tr>
            </table>
Here is the code-behind:
        #region City/ZIP Events
        /* Handles ALL rad zip code item requested events */
        protected void radZipCode_ItemsRequested(object o, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e) {
            loadZipCodes((Telerik.Web.UI.RadComboBox)o, e.Text);
        }
        #endregion
And the databinding:
        void initCity(Telerik.Web.UI.RadComboBox city) {
            try {
                SqlDataSource sdS = new SqlDataSource();
                sdS.ConnectionString = "User ID=xxx;Initial Catalog=CJIS_Data;Data Source=172.31.70.140;PASSWORD=xxx;";
                sdS.SelectCommand = "SELECT DISTINCT [Description] FROM [Zip_Codes] ORDER BY Description ASC";

                city.DataTextField = "Description";
                city.DataSource = sdS;
                city.DataBind();
            } catch { }
        }
        void loadZipCodes(Telerik.Web.UI.RadComboBox zip, string cityID) {
            try {
                SqlConnection cn = new SqlConnection("User ID=xxx;Initial Catalog=CJIS_Data;Data Source=172.31.70.140;PASSWORD=xxx;");

                SqlCommand cmd = new SqlCommand("SELECT [Record_ID],[Code],[County] FROM [Zip_Codes] WHERE [Description] = '" + cityID + "' ORDER BY Code ASC", cn);
                cn.Open();
                SqlDataReader dr = (SqlDataReader)cmd.ExecuteReader();

                zip.Items.Clear();
                Telerik.Web.UI.RadComboBoxItem item = new Telerik.Web.UI.RadComboBoxItem();
                while(dr.Read()) {
                    item = new Telerik.Web.UI.RadComboBoxItem(dr["Code"].ToString(),dr["Record_ID"].ToString());
                    item.Attributes.Add("County", dr["County"].ToString());
                    zip.Items.Add(item);
                }
                dr.Close();
                cn.Close();
            } catch { }
        }
        void loadStates(Telerik.Web.UI.RadComboBox state) {
            try {
                SqlDataSource sdS = new SqlDataSource();
                sdS.ConnectionString = "User ID=xxx;Initial Catalog=CJIS_Data;Data Source=172.31.70.140;PASSWORD=xxx;";
                sdS.SelectCommand = "SELECT Code,Description FROM State_Country_Codes ORDER BY Description";

                state.DataTextField = "Description";
                state.DataValueField = "Code";
                state.DataSource = sdS;
                state.DataBind();
            } catch { }
        }



Tags
ComboBox
Asked by
Trimox
Top achievements
Rank 1
Answers by
Rosi
Telerik team
Trimox
Top achievements
Rank 1
Karl
Top achievements
Rank 1
Share this question
or