Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help with Drop Down issue

Status
Not open for further replies.

linuxjr

Programmer
Jun 2, 2001
135
0
0
US
I am currently teaching myself how to program in C# and creating some ASP.NET pages for my department. I am currently trying to implement a way to populate two drop down list with data from my database. The two drop downs consist of buildings and floors. So here is what I did so far.

1) I created a class for my buildings as follows:

Code:
	public class BL
	{
		public string _blid;
		public string _blname;

		public BL(string blid, string blname){
			_blid = blid;
			_blname = blname;
		}
	}

2) I created a class for my floors as follows:
Code:
	public class Floor
	{
		public string _blid;
		public string _flid;
		public string _flname;

		public Floor(string BLID, string FLID, string FLNAME)
		{
			_blid = BLID;
			_flid = FLID;
			_flname = FLNAME;
		}

	}

Now in my web page I have the two drop down lists controls added that I want to populate. I then created two private Arraylists that I want to store my buildings and floors as follow:

Code:
ArrayList building = new ArrayList();
ArrayList floor  = new ArrayList();

In my page Load I have two functions to fill in my drop down lists as follows:

Code:
if (!Page.IsPostBack){
    FillBL();		
    FillFloor();
}

Here is the code I used to fill in the drop down's:

From FillBL():
Code:
while (reader.Read()){
    ListItem bl = new ListItem();
    building.Add(new BL(reader.GetString(0), reader.GetString(1)));
    bl.Text = reader.GetString(1);
    bl.Value = reader.GetString(0);
    BLDDL.Items.Add(bl);
}

From FillFloor():
Code:
while (reader.Read()){
    ListItem fl = new ListItem();
    floor.Add(new Floor(reader.GetString(0), reader.GetString(1), reader.GetString(2)));
    fl.Text = reader.GetString(3) + " - " + reader.GetString(2);
    fl.Value = reader.GetString(1);
    FLDDL.Items.Add(fl);
}


So the first time the page loads the information gets loaded correctly and I have my nice two arraylists so I can do comparisons and use them throughout the page until the user closes the form.

Now the second phase is to filter the floors based on the user's selcection of the building. When I use the select index change event does not fire and I know the answer is to set the autopostback = true which I have done.

This creates a problem that I love to call as the Postback curse. I realize as soon as the page refreshes my arraylists are emptied :( which is not what I want. I started researching on solutions to keep my arraylists and I notice from posts that they say to use viewstate. Since I am learning I tried to create a viewstate variable as such:
Code:
   ViewState["blarray"] = building;

then outside of my (!Page.IsPostback) I added this code:
Code:
    if (ViewState["blarray"] != null)
        building = (ArrayList)ViewState["blarray"];
    else
        Response.Write("Viewstate is null");

This generates an error saying I do not have a TypeConvertor or its not serializble.

So if anyone can tell me what I have done wrong or know a better way to save my arraylists from being wiped out I would appreciate any tips or suggestions.

I also wanted to ask too to sort of kill 2 birds with on stone is how do I filter the second arraylist (floor) to show only the floors for say building 1.

Thank you for your time and have a nice day.
 
Page_Load:
Code:
ArrayList blArray = (ArrayList)ViewState["BLArray"];
			if (blArray == null)
			{
				blArray = new ArrayList();
				ViewState["BLArray"] = blArray;
			}

Your Class:
Code:
[b][Serializable][/b]
public class BL
    {
        public string _blid;
        public string _blname;

        public BL(string blid, string blname){
            _blid = blid;
            _blname = blname;
        }
    }
To Populate:
Code:
ArrayList blArray = (ArrayList)ViewState["BLArray"];	
while (reader.Read()){
    ListItem bl = new ListItem();
    blArray.Add(new BL(reader.GetString(0), reader.GetString(1)));
    bl.Text = reader.GetString(1);
    bl.Value = reader.GetString(0);
    BLDDL.Items.Add(bl);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top