Advertisement

02.11.2008 at 12:21PM PST, ID: 23154233
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

DropDownList SelectedIndexChanged AutoPostBack C# ASP.NET

2 DropDownList Boxes (ddStates, ddCounties)
Both are AutoPostBack.
ddStates_SelectedIndexChange says to populate ddCounties.
ddCounties_SelectedIndexChanged says to populate some other DropDownList.

When I select any county other than the first county then everything posts back and counties get reset to the first county.

I understand that the problem should be in ddCounties_SelectedIndexChanged but the problem is cleared up if I use a file to populate counties from ddStates_SelectedIndexChanged opposed to using a DB.

Oddly enough though, if you look at the "This works" code in ddStates_SelectedIndexChanged and use that code instead of the "This doesn't work" code, it works.

What is the difference between using a file as opposed to using a DB?

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
default.aspx
<%@ Page Language="C#" AutoEventWireup="false" Codebehind="default.aspx.cs" Inherits="SmartQueryXML._default" ValidateRequest="false" %>
 
<asp:DropDownList ID="ddStates" runat="server" OnSelectedIndexChanged="ddStates_SelectedIndexChanged" AutoPostBack="True" Width="150px"></asp:DropDownList>
<asp:DropDownList ID="ddCounties" runat="server" Width="150px" OnSelectedIndexChanged="ddCounties_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
 
=================================================
default.aspx.cs
    protected void ddStates_SelectedIndexChanged(object sender, EventArgs e) {
      string ErrorMessage = "";
      string state = ddStates.SelectedItem.Value;
 
      // This doesn't work
      DataSet ds = new DataSet();
      ds = Helper.GetCounties(state, "%%", ref ErrorMessage);
 
      ddCounties.Items.Clear();
      ddCounties.Items.Add(new ListItem("Choose a County", ""));
 
      foreach(DataRow dr in ds.Tables[0].Rows) {
        ddCounties.Items.Add(new ListItem(dr["Name"].ToString(), state));
      }
 
      // This works
      //DataSet ds = new DataSet("Jurisdiction");
      //FileStream fs = new FileStream(Server.MapPath("Jurisdiction.xml"), FileMode.Open, FileAccess.Read);
      //StreamReader sr = new StreamReader(fs);
      //ds.ReadXml(sr);
      //fs.Close();
      //DataView dv = new DataView(ds.Tables[0]);
 
      //dv.Table = ds.Tables[0];
      //dv.RowFilter = "State = '" + state + "'";
      //ddCounties.DataSource = dv;
      //ddCounties.DataMember = "jurisdiction";
      //ddCounties.DataTextField = "county";
      //ddCounties.DataValueField = "county";
      //ddCounties.DataBind();
 
    }
 
    protected void ddCounties_SelectedIndexChanged(object sender, EventArgs e) {
      // Reset inputs when user selects a county
      //  Clean slate.
      string county = ddCounties.SelectedItem.Value;
    }
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: famoso
Solution Provided By: famoso
Participating Experts: 3
Solution Grade: A
Views: 767
Translate:
Loading Advertisement...
02.11.2008 at 02:12PM PST, ID: 20870818

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 06:26PM PST, ID: 20872136

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 09:49PM PST, ID: 20872745

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2008 at 04:29AM PST, ID: 20874178

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2008 at 04:35AM PST, ID: 20874206

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2008 at 04:40AM PST, ID: 20874232

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2008 at 08:48PM PST, ID: 20881717

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.11.2008 at 02:12PM PST, ID: 20870818

Rank: Genius

I don't see how either of them work, because they both don't reset the selected value after clearing and readding counties.

Bob
 
02.11.2008 at 06:26PM PST, ID: 20872136
I don't understand and really value your opinion and help.  The commented "This works" code in ddStates populates the ddCounties DDL and when I select one of the counties from that list it sticks even after it posts.  What is missing that you think should be there (maybe I'm missing a step that .NET is allowing) or maybe I left out code that should have been shown.  I do believe that I placed all the code in here.
 
02.11.2008 at 09:49PM PST, ID: 20872745

Rank: Guru

If I'm understanding your problem correctly, this is giving the error right?:

<<<When I select any county other than the first county then everything posts back and counties get reset to the first county.>>>>

On Page_Load, call the event under If Not IsPostBack:

  if (!IsPostBack) {
        ddStates_SelectedIndexChanged(ddStates, null);
    }
 
02.12.2008 at 04:29AM PST, ID: 20874178
I may not have been clear enough when I said before this statement:
"When I select any county other than the first county then everything posts back and counties get reset to the first county."

I stated:
"
ddStates_SelectedIndexChange says to populate ddCounties.
ddCounties_SelectedIndexChanged says to populate some other DropDownList.
"

... meaning first I select a state, ddStates does its event and populates ddCounties.  Then, when I select a county (say the second one but could be the first or third) then the page posts again and I get the correct state I selected but the county is reset to the first county as if I never selected one.

So, digitalZo, I think that having
ddStates_SelectedIndexChanged(ddStates, null);
will be too late because !IsPostBack happened when I called the page.  Then I select ddState, then the ddState event happens and it posts back (IsPostBack), then the state I selected it there and ddCounties is filled.  I select ddCounties and ddCounties event happens and it posts back again (IsPostBack) and the state that I selected is still there but the ddCounties is reset to the beginning and doesn't hold the county I selected.

Maybe there is confusion in my Page_Load()  (see code snippet)
I also added the functions I call from Page_Load() also (LoadStates() and ClearQuestions()).

Also, I restated the events to ddStates and ddCounties.
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
    protected void Page_Load(object sender, EventArgs e) {
      // Request inputs
      // Request SessionID
      // Request if got questions already
 
      if(!IsPostBack) {
        LoadStates("");
        chkDeed.Attributes.Add("onclick", "javascript:checkDeed()");
        chkMortgage.Attributes.Add("onclick", "javascript:checkMortgage()");
        chkRefi.Attributes.Add("onclick", "javascript:checkMortgageRefi()");
        chkAssignment.Attributes.Add("onclick", "javascript:checkAssignment()");
        chkRelease.Attributes.Add("onclick", "javascript:checkRelease()");
        chkNewPolicy.Attributes.Add("onclick", "javascript:checkTitle()");
        chkReissuePolicy.Attributes.Add("onclick", "javascript:checkTitleReissue()");
 
        // Set Sessions
        Session["State"] = null;
        Session["County"] = null;
        Session["City"] = null;
 
        DisplayControl.Display = 0;
        DisplayControl.SetPanels(ref pnlOffice, ref pnlDocTypes, ref pnlQuestions, ref pnlDetails, 1);
 
        ClearQuestions();
 
      } else { // Page Posted Back
      }
    }
 
    private void LoadStates(string pState) {
      DataSet ds = new DataSet();
      FileStream fs = new FileStream(Server.MapPath("State.xml"), FileMode.Open, FileAccess.Read);
      StreamReader sr = new StreamReader(fs);
      ds.ReadXml(sr);
      fs.Close();
      DataView dv = new DataView(ds.Tables[0]);
      ddStates.DataSource = dv;
      ddStates.DataMember = "state";
      ddStates.DataTextField = "display";
      ddStates.DataValueField = "value";
      ddStates.DataBind();
      ddStates.SelectedValue = pState;
    }
 
    protected void ClearQuestions() {
      Session["Q_WasAsked"] = null;
    }
 
    protected void ddStates_SelectedIndexChanged(object sender, EventArgs e) {
      string ErrorMessage = "";
 
      if(ddStates.SelectedIndex != 0) {
        imgStateSummary.Visible = true;
      } else {
        imgStateSummary.Visible = false;
        imgOfficeDetail.Visible = false;
      }
 
      string state = ddStates.SelectedItem.Value;
      Session["State"] = state;
 
      // This doesn't work
      DataSet ds = new DataSet();
      ds = Helper.GetCounties(state, "%%", ref ErrorMessage);
 
      ddCounties.Items.Clear();
      ddCounties.Items.Add(new ListItem("Choose a County", ""));
 
      foreach(DataRow dr in ds.Tables[0].Rows) {
        ddCounties.Items.Add(new ListItem(dr["Name"].ToString(), state));
      }
 
      // This works
      //DataSet ds = new DataSet("Jurisdiction");
      //FileStream fs = new FileStream(Server.MapPath("Jurisdiction.xml"), FileMode.Open, FileAccess.Read);
      //StreamReader sr = new StreamReader(fs);
      //ds.ReadXml(sr);
      //fs.Close();
      //DataView dv = new DataView(ds.Tables[0]);
 
      //dv.Table = ds.Tables[0];
      //dv.RowFilter = "State = '" + state + "'";
      //ddCounties.DataSource = dv;
      //ddCounties.DataMember = "jurisdiction";
      //ddCounties.DataTextField = "county";
      //ddCounties.DataValueField = "county";
      //ddCounties.DataBind();
 
      //***************
 
      if(ddCounties.SelectedItem == null) {
        imgOfficeDetail.Visible = false;
      } else {
        if(ddCounties.SelectedItem.Value != "Choose a County") {
          imgOfficeDetail.Visible = true;
        } else {
          imgOfficeDetail.Visible = false;
        }
      }
 
      //***************
      // Reset inputs when user selects a state
      //  Clean slate.
      ClearQuestions();
      litQuestions.Text = "";
      litError.Text = "";
      litInfo.Text = "";
    }
 
    protected void ddCounties_SelectedIndexChanged(object sender, EventArgs e) {
      if(ddCounties.SelectedIndex != 0) {
        imgOfficeDetail.Visible = true;
      } else {
        imgOfficeDetail.Visible = false;
      }
 
      // Reset inputs when user selects a county
      //  Clean slate.
      string county = ddCounties.SelectedItem.Text;
      Session["County"] = county;
 
      ClearQuestions();
      litQuestions.Text = "";
      litError.Text = "";
      litInfo.Text = "";
    }
Open in New Window
 
02.12.2008 at 04:35AM PST, ID: 20874206
Sorry, my stupidity.  The error was in my DropDownList creation line 69

      foreach(DataRow dr in ds.Tables[0].Rows) {
        ddCounties.Items.Add(new ListItem(dr["Name"].ToString(), state));
      }

The above needed to be changed to

      foreach(DataRow dr in ds.Tables[0].Rows) {
        ddCounties.Items.Add(new ListItem(dr["Name"].ToString(), dr["Name"].ToString()));
      }
Accepted Solution
 
02.12.2008 at 04:40AM PST, ID: 20874232
I decided to view my html source to see what was coming back in the DDL.  What was coming back was:

            <option value="CA">Alameda</option>
            <option value="CA">Alpine</option>
            <option value="CA">Amador</option>
            <option value="CA">Butte</option>
            <option value="CA">Calaveras</option>
            <option value="CA">Colusa</option>
            <option value="CA">Contra Costa</option>

...which state was being selected and since they were all states it just showed the first each time.

I just needed to look at the problem differently.

Thanks all for being there.
 
02.12.2008 at 08:48PM PST, ID: 20881717

Rank: Guru

Since the counties is the dropdown being resetted, insert the SelectedIndexChanged event of that dropdown too in Not IsPostBack:

  if (!IsPostBack) {
        ddStates_SelectedIndexChanged(ddStates, null);
        ddCounties_SelectedIndexChanged(ddCounties, null);
    }       

Try this and let me know if it works.

http://msdn2.microsoft.com/en-us/library/ms227621(VS.80).aspx
 
 
05.04.2008 at 12:46PM PDT, ID: 21496727
simple, a text file don't have index so the SelectedIndexChange is not fired!

 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628