Skip to content
Advertisement

Could not find control ‘DropCity’ in ControlParameter ‘City’ ERROR

Context: I am trying to nest two dropdownlist(ddl) inside a gridview, the first ddl is called “Ciudad” and the second one is called “Comuna”, I want that according to the “Ciudad” I choose the “Comunas” change to that corresponding “ciudad”. but I get an error and I can not solve it.

My GridView:

My SqlDataSource’s:

Advertisement

Answer

This can be a challenge – and the REASON why is that the one combo box in the list is NOT bound, and does NOT exist in the database. It is ONLY a filter for the cascaded combo that DOES exist in the database.

So, you can do it this way. We will assume a person, and they want to select a hotel, but we provide a “filter”/cascade combo. We allow the user to select a city, and THENN ONLY hotels from that city.

So, our markup can be like this. The one cbo is city filter, and then the actual data value (hotel_id) is part of the data – but the city cbo as noted is JUST a fiter.

So, this markup:

Ok, now our code to load the grid.

NOTE close I declare rstCity at the class level – it will persist during the data bind process (saves us having to re-pull city combo list over and over).

so far, so easy. But we now have to for each row do the following:

So, this is a bit of code, but looks like this:

And now we see this:

enter image description here

Now, the above of course is based on each row “hotel_id”.

However, we do need to wire up the city filter and cascade to the hotel.

So, we need to add a a event to the cboCity.

So, we do this:

enter image description here

Since the cbo box is inside of the grid, we can’t use the property sheet, but as above shows, we simply start typing in markup. intel-sense will prompt that create new event – don’t look like anything occured, but when we flip back to code behind, the code stub will exist. We also of course had to set autopostback=true.

Ok, so this cascade code is much the same as what we did in the data bind routine. Of course, when a user selects a new city, we do have to blow out the existing value, since the filter is for a different city.

That bit of code is this:

Now we do have ONE spot where we concatenate the city input. Since it does come from a cbo box, then the risk of sql injection is very low. But if you really sticky on that issue, then we could parametrize that bit of code.

Also, I used a helper routine that returns a data table, and that code was this:

Edit:

the poster has noted they are looking for a C# version. I am fluent in both – but the vb code is less effort, since vb does a LOT OF auto casting for you.

However, at the end of the day, the concpets here and HOW you approach this problem is really the value here.

However, for good measure, here is the code as C#

Code to load the grid:

And now the item data bound event:

And of course the “cascade” code for the grid. While above fills the gird correct, we still want the cbo’s to function. So this:

And of course my “helper” routine to return a table (gets too tiring to write that over and over!!!).

So this:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement