Welcome Guest Search | Active Topics | Sign In | Register

Change options of dropdownlist in grid Options
RobertVH
Posted: Wednesday, October 21, 2009 9:50:42 AM
Rank: Newbie
Groups: Member

Joined: 10/21/2009
Posts: 4
I would like to have a grid containing two CustomColumns with dropdown boxes. The first column dropdown options must be retrieved from a database (just like in the demo). Each dropdown in this column can have the same options. After selecting a option in the first column in a row, the second dropdown in this row should have its possible values populated by a database query.

Would this be possible with the Grid control? Thanks for your help!
eo_support
Posted: Wednesday, October 21, 2009 10:21:46 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Yes. This is possible. There are several ways to do this:

1. With a CallbackPanel. This way once you select an option from the first drop down, the Grid is AJAX updated so that you can use server side code to update the second drop down. This will be the easiest approach for you if you MUST run server side code to populate the second drop down (for example, you must run a query against the database). Here is a working example that demonstrates this approach:

http://demo.essentialobjects.com/Default.aspx?path=Grid\_i1\grid_custom_column_adv2

The second column of the sample is a regular column, not a custom column, however the idea is the same --- once user selects an item from the drop down, your server side code is triggered to update the second column;

2. Render all possible items to the client side all together. If the total number of options is limited, then you can use this approach. For example, if your first drop down only have "America" and "Europe", and for each option you have a number of countries, then you can render the some JavaScript code like this:

Code: JavaScript
var map = [];
map["America"] = ["U.S.", "Canada", "Mexico", "Brazil"];
map["Europe"] = ["U.K", "France", "Netherlands", "German"];

You would then code your custom item to have select elements like this:

Code: HTML/ASPX
<eo:CustomColumn>
    <EditorTemplate>
        <select id="dropdown1" onchange="fill_second_column()">
            <option>-- Please Select--</option>
            <option>America</option>
            <option>Europe</option>
        </select>
    </EditorTemplate>
</eo:CustomColumn>
<eo:CustomColumn>
    <EditorTemplate>
        <select id="dropdown2">
        </select>
    </EditorTemplate>
</eo:CustomColumn>


Code: JavaScript
function fill_second_column()
{
    //Get both drop downs
    var dropdown1 = document.getElementById("dropdown1");
    var dropdown2 = document.getElementById("dropdown2");

    //Get the list for the second drop down based
    //on user selection from the first drop down
    var key = dropdown1.options[dropdown1.selectedIndex].text;
    var list = map[key];
    dropdown2.options.length = 0;
    
    //Update the second list
    if (list)
    {
        for (var i = 0; i < list.length; i++)
        {
            var option = document.createElement("option");
            option.text = option.value = list[i];
            if (dropdown2.options.add)
                dropdown2.options.add(option);
            else
                dropdown2.options[i] = option;
        }
    }
}


Note the first drop down is set to call fill_second_column when user selects an option, which then fills the second drop down based on the data you already rendered. This approach is very fast since everything is done on the client side. But because it does not go back to the server side, all the option must be render to the client side at the very beginning.

3. A hybrid approach is to modify approach 2 to send an AJAX call back to the server side to retrieve the list. You would change this line:

Code: JavaScript
var list = map[key];

to something like this:

Code: JavaScript
var list = get_list_for_key(key);

Where you would implement get_list_for_key to make an AJAX call to the server (for example, calling a Web service) to retrieve the list. The details for this would be out of the scope of our support, but you should be able to find plenty information on the web about how to call Web service from JavaScript;

Hope this helps.

Thanks!

RobertVH
Posted: Wednesday, October 21, 2009 10:32:04 AM
Rank: Newbie
Groups: Member

Joined: 10/21/2009
Posts: 4
Thanks for your quick reply, I think I will use the first solution.

I have a question about this:
"The second column of the sample is a regular column, not a custom column, however the idea is the same --- once user selects an item from the drop down, your server side code is triggered to update the second column;"

When I look at the demonstration code, I think this line of codes performs the actual updating of a field in a (static) column: e.Item.Cells(2).Value = productPrice

How would this code look if I want to update a cell of a CustomColumn to get a DropDown list? Would it be like e.Item.Cells(2).Value = "<option>1</option><option>2</option>" ?
eo_support
Posted: Wednesday, October 21, 2009 10:37:02 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

That's not how you do it. First of all you must change the second column into a CustomColumn, which means you will no longer use e.Item.Cells(2).Value to fill the cell. You will need to fill the second column the same as you fill the first column (The "Product" column). Please see code in Page_Load for how to do that. You basically duplicate everything from the first column but use a different SQL query.

Thanks


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.