Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey all,

I am having one of the weirdest problems ever....

I have n custom aspx page that gets called from ms cr 4.0 button. When the page loads it poulates a dropdown box with all the coutrys then when i select a country it must load all the provinces for that country. It loads the country data when the form loads but when i select a country and the for does a postback to load the selected countrys provines it loses all the country data??? WHY ??

HTML :
ASP.NET
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <table style="border: none; width: 500px">
                <table style="border: none; width: 300px">
                    <tr>
                        <td class="style5">
                            Country<br />
                             
                        </td>
                        <td class="style8">
                            <asp:DropDownList ID="cboLocationCountry" runat="server" AutoPostBack="True" OnSelectedIndexChanged="cboLocationCountry_SelectedIndexChanged"
                                TabIndex="5" Width="196px">
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <td class="style5">
                            Province<br />
                             
                        </td>
                        <td class="style8">
                            <asp:DropDownList ID="cboLocationProvince" runat="server" AutoPostBack="True" Width="196px"
                                     OnSelectedIndexChanged="cboLocationProvince_SelectedIndexChanged" TabIndex="6">
                            </asp:DropDownList>
                        </td>
        </ContentTemplate>
</asp:UpdatePanel>


Page Load :
C#
     if (!IsPostBack)
{
    // clear country list
    //load all vehicles from crm db and populate dropdown
    cboLocationCountry.Items.Clear();
    loadCRMLookups("country", cboLocationCountry, "");
}


And loading the province data when a country is selected:
C#
protected void cboLocationProvince_SelectedIndexChanged(object sender, EventArgs e)
{
     // load selected province town's
    cboLocationTown.Items.Clear();
    cboLocationSuburb.Items.Clear();
    cboLocationZone.Items.Clear();
    loadCRMLookups("town", cboLocationTown, cboLocationProvince.SelectedValue);
    loadCRMLookups("zone", cboLocationZone, cboLocationProvince.SelectedValue);
    loadCRMLookups("suburb", cboLocationSuburb, cboLocationTown.SelectedValue);
}
Posted

I don't think 'Items' is preserved across postbacks. (That would be a very large amount of data to send back to the server, in most cases unnecessarily.) If you set it in PageInit instead of PageLoad then server-side magic might keep hold of it for you, otherwise you'll have to cache the result of the lookup in the session and rebuild the Items array in posted back pages as well. In this case you should definitely do it in Init (not Load) so the posted back value for SelectedItem works.

As a side note, pages like this are extremely annoying, particularly if you have some latency on your connection. I strongly advise you to either make these free text fields or at least to use AJAX to rebuild the contents of secondary options.
 
Share this answer
 
Alternatively, instead of manually populating cboLocationCountry you can use one of the DataSource controls and bind cboLocationCountry to it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900