This is a migrated thread and some comments may be shown as answers.

How to get postback or event to fire when GridDropDown selection changes?

13 Answers 277 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 13 Aug 2008, 10:54 PM
I have a grid with a column that is a GridDropDownColumn and I want it to perform a postback or fire an event at the point the user selects a different item in the dropdown (not on submission of the edit form).

How do I do this?

13 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 14 Aug 2008, 05:35 AM
Hi Rob,

You can use ItemCreated to get reference to the DropDownList where you can set AutoPostBack and attach desired event:

if(e.Item is GridEditableItem && e.Item.IsInEditMode)
{
   
DropDownList DropDownList1 = (DropDownList)(e.Item as GridEditableItem)["YourColumnUniqueName"].Controls[0];
    ...
}

Greetings,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Shinu
Top achievements
Rank 2
answered on 14 Aug 2008, 07:51 AM
Hi Rob,

You can also refer the following help articles to get more details on accessing cells and rows of a Grid in regular mode and edit mode.
Accessing cells and rows
Edit forms
In place

Thanks
Shinu.
0
d-cpt
Top achievements
Rank 2
answered on 16 Sep 2008, 08:45 PM
Hi Vlad,

I have similar question. My radgrid has 2 griddropdowncolumns says ddlName and ddlEmail. both share the same UserId. In edit mode, how I create a event for when I select 'Mario' on ddlName and it will fire an event to select Mario's email on ddlEmail.

Thanks,

d-cpt.
0
Rob
Top achievements
Rank 1
answered on 16 Sep 2008, 11:20 PM
Thanks but I get only Literal controls back.

Since this column is a GridTemplateColumn with an EditItemTemplate that contains my dropdown, how do I get a reference to that dropdown?
0
Rob
Top achievements
Rank 1
answered on 16 Sep 2008, 11:55 PM
I did follow this example I found but it seems out of date [namespace wrong] and the ItemDataBound call is never fired [maybe because it is never wired up in the sample aspx code on the same page??]

http://www.telerik.com/help/aspnet/grid/grdoperationswithdropdownlistinedititemtemplate.html

Is there an updated posting to this article?
0
Vlad
Telerik team
answered on 17 Sep 2008, 05:40 AM
Hi Rob,

This article is for our RadGrid "Classic". Here is the same for RadGrid for ASP.NET AJAX:
http://www.telerik.com/help/aspnet-ajax/grdoperationswithdropdownlistinedititemtemplate.html

Greetings,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rob
Top achievements
Rank 1
answered on 17 Sep 2008, 05:23 PM
Still ItemDataBound is never called.

The RadGrid has AutoGenerateColumns set to true and the DropDown in the template column for the EditItemTemplate IS NOT DataBound.  It is a multicheckbox solution that drives a many-to-many mapping table so I have to prepopulate it before hand and then read the values afterwards.

I can get a reference to the ItemTemplate control but not to the EditItemTemplate control from within the ItemCommand function.  I populate the dropdown on e.CommandName == "Edit" and also "InitInsert" from the mapping table and lookups.  I am populating all possible mappings in the drop down and then setting checked=true wherever a mapping exists.  Then on ItemInserted and ItemUpdated functions, if there was no exception I then read the checked states in the drop down and add/remove entries from the mapping table as necessary.

The data scenario is a simple one:

An Employer can have multuple Contacts, each of those Contacts can have zero, one, or many ContactTypes associated with them.  Thus there are 4 tables (Employer, EmployerContacts, ContactTypes, and the mapping table EmployerContactTypes which holds 2 FKs (ContactTypeID and EmployerContactID).

This is a very very common database scenario, but I can't find a single example that shows how to implement this in RadGrid.

I need a way to show a dropdown in a cell that shows all the possible mappings (ie it lists all the ContactTypes of which there are about 10) and then lets the user check on or off the correct types that should be associated with this specific contact. 

Currently, the closest I can get is to have the dropdown in a GridTemplateColumn in an EditItemTemplate since the dropdown should only show in the edit form (I use a label to show the contatinated string of selections in the ItemTemplate).  This dropdown is not databound so I prepopulate it in ItemCommand:Edit and ItemCommand:InitInsert.  It is here that I run into the problem as I can't seem to get a reference to the dropdown control using FindControl() or the Controls[x] array anywhere.

Here is the aspx code:
        <telerik:RadGrid Skin="Vista" ID="RadGrid1" runat="server" ShowStatusBar="True" Width="90%" AllowPaging="True" GridLines="None"   
            DataSourceID="SqlDataSource1"   
            AllowAutomaticDeletes="True" 
            AllowAutomaticInserts="True"   
            AllowAutomaticUpdates="True"   
            AutoGenerateDeleteColumn="False"   
            AutoGenerateEditColumn="True" 
            OnItemCommand="RadGrid1_ItemCommand"               
            oniteminserted="RadGrid1_ItemInserted"   
            onitemupdated="RadGrid1_ItemUpdated" 
            PageSize="20" > 
            <MasterTableView Width="100%" CommandItemDisplay="TopAndBottom" AutoGenerateColumns="False" 
                DataKeyNames="EmployerContactID" > 
                  
                <RowIndicatorColumn Visible="False">  
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn Visible="False" Resizable="False">  
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </ExpandCollapseColumn> 
              
                <Columns> 
                    <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">  
                        <ItemStyle Width="50px" /> 
                    </telerik:GridEditCommandColumn> 
                    <telerik:GridTemplateColumn UniqueName="LookupTypes" HeaderText="Contact Type (*)" SortExpression="LookupType" > 
                        <ItemTemplate> 
                            <asp:Label ID="LookupTypesLabel" runat="server" Text=""></asp:Label> 
                        </ItemTemplate> 
                        <EditItemTemplate> 
                            <telerik:RadComboBox ID="LookupTypesDropDown" runat="server"   
                                AllowCustomText="false" EmptyMessage="" HighlightTemplatedItems="true"   
                                Width="250px" DropDownWidth="250px" AutoPostBack="false" > 
                                <Items> 
                                </Items> 
                                <ItemTemplate>   
                                    <asp:CheckBox runat="server" ID="LookupTypeCheckBox" Text="" AutoPostBack="false"/> <%# DataBinder.Eval(Container, "Text") %>   
                                </ItemTemplate>   
                            </telerik:RadComboBox> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridBoundColumn UniqueName="NameFirst" SortExpression="NameFirst" 
                        HeaderText="Name, First (*)" DataField="NameFirst" MaxLength="255" ColumnEditorID="BigEditBox">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="NameMiddle" SortExpression="NameMiddle" Visible="false" 
                        HeaderText="Name, Middle" DataField="NameMiddle" MaxLength="255" ColumnEditorID="BigEditBox">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="NameLast" SortExpression="NameLast" 
                        HeaderText="Name, Last (*)" DataField="NameLast" MaxLength="255" ColumnEditorID="BigEditBox">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="Title" SortExpression="Title" Visible="true" 
                        HeaderText="Job Title" DataField="Title" MaxLength="255" ColumnEditorID="BigEditBox">  
                    </telerik:GridBoundColumn> 
                    </telerik:GridCheckBoxColumn>   
                    <telerik:GridBoundColumn UniqueName="PhoneNumber" SortExpression="PhoneNumber" Visible="false" 
                        HeaderText="Phone Number" DataField="PhoneNumber" MaxLength="255">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="Email" SortExpression="Email" Visible="false" 
                        HeaderText="E-mail (*)" DataField="Email" MaxLength="255" ColumnEditorID="BigEditBox">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="Notes" SortExpression="Notes" Visible="false" 
                        HeaderText="Notes" DataField="Notes" MaxLength="255" ColumnEditorID="BigEditBox">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridButtonColumn Text="Delete" UniqueName="DeleteCommandColumn" CommandName="Delete" ConfirmText="You are about to delete this contact. Are you sure?"></telerik:GridButtonColumn>   
                </Columns> 
 
                <EditFormSettings EditColumn-UpdateText="Save Changes" EditColumn-ButtonType="PushButton">  
                    <EditColumn UniqueName="EditCommandColumn1"></EditColumn> 
                    <PopUpSettings ScrollBars="None"></PopUpSettings> 
                </EditFormSettings> 
            </MasterTableView> 
        </telerik:RadGrid> 
 

Here is the code structure for ItemCommand():
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
    {  
        Hashtable newnewValues = new Hashtable();  
 
        if (e.Item is GridEditableItem)  
        {  
            GridEditableItem eeditItem = e.Item as GridEditableItem;  
            switch (e.CommandName)  
            {  
                case "Update":  
                    IDictionary oldValues = editItem.SavedOldValues;  
                    e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editItem);  
                    Alerts.NewAlert("User Edited A Contact", Alerts.GetDetailsOfChange(oldValues, newValues), false);  
                    break;  
 
                case "PerformInsert":  
                    e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editItem);  
                    Alerts.NewAlert("User Added A New Contact", Alerts.GetDetailsOfChange(null, newValues), false);  
                    break;  
 
                case "Delete":  
                    e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editItem);  
                    Alerts.NewAlert("User Deleted A Contact", Alerts.GetDetailsOfChange(null, newValues), false);  
                    break;  
 
                case "Edit":  
                    if (e.Item.IsInEditMode)  
                    {  
                        RadComboBox dropdown = (RadComboBox)editItem["LookupTypes"].FindControl("LookupTypesDropDown");  
                        if (dropdown != null)  
                        {  
                            PopulateDropDownCheckBoxes(dropdown.Items);  
                        }  
                    }  
                    break;  
            }  
        }  
        else if (e.Item is GridCommandItem)  
        {  
            switch (e.CommandName)  
            {  
                case "InitInsert":  
                    RadComboBox dropdown = (RadComboBox)e.Item.FindControl("LookupTypesDropDown");  
                    if (dropdown != null)  
                    {  
                        PopulateDropDownCheckBoxes(dropdown.Items);  
                    }  
                    break;  
            }  
        }  
    }  
 

The problem here is that dropdown is always null as e.Item.FindControls() always returns null.  Also I tried Controls[0] and the other indexes there.

I also have tried casting e.Item as an edit form item but that results in an exception since isInEditMode is always false at this point.

So I have followed the URL example above as much as possible but since the ItemDataBound call is never made I dont have a condition where that code ever executes.

Any help in this very common kind of problem would be wonderful.  I would also suggest that some kind of built-in handling for many-to-many mappings or built in handling of a multi-checkbox drop down that can databind to a mapping table would be very valuable along with any tutorials in the mean time on how to handle this...

Thanks in advance.
0
Rosen
Telerik team
answered on 19 Sep 2008, 01:44 PM
Hi Rob,

The cause of this behavior is that when edit command is fired in ItemCommand event the edit item is not created yet. Therefore the combobox does not exist. In order to accomplish the desired scenario I suggest you to use ItemCreated and ItemDataBound events. I have attached a page which demonstrates a simple implementation.

Please give it a try and see if this helps.

Sincerely yours,
Rosen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rob
Top achievements
Rank 1
answered on 19 Sep 2008, 10:06 PM
Thank you.  The ItemCreated event does now get the RadComboBox reference properly.  But I cannot get access to the CheckBox control that is in the RadComboBox's ItemTemplate in order to prepopulate the checkbox value with what's in the database mapping table during the ItemCreated event.

I really appreciated the effor for you to make the previous sample, but could I ask you to expand that sample to prepopulate the checkbox value in the example for LookupTypeCheckbox control?  (just set it to true would be fine...)

Much appreciated.
Rob
0
BaiH
Top achievements
Rank 1
answered on 22 Sep 2008, 01:16 PM
Hi Rob,

You can hook to RadComboBox's ItemCreated event and inside its event handler set a value for the item's template's checkbox. Similar to this:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
        {  
            RadComboBox comboBox = ((GridEditableItem)e.Item)["TemplateColumn"].FindControl("LookupTypesDropDown"as RadComboBox;  
            if (comboBox != null)  
            {                  
                comboBox.SelectedIndexChanged += comboBox_SelectedIndexChanged;  
                comboBox.ItemCreated += comboBox_ItemCreated;  
            }  
        }  
    }  
 
    void comboBox_ItemCreated(object sender, RadComboBoxItemEventArgs e)  
    {  
        CheckBox checkBox = e.Item.FindControl("LookupTypeCheckBox"as CheckBox;  
        if (checkBox != null)  
            checkBox.Checked = true;  
    } 

All best,
Rosen
0
Rob
Top achievements
Rank 1
answered on 23 Sep 2008, 12:25 AM
I did exactly as you suggested and wired up the comboBox_ItemCreated event but it does not get called on creating a new record or on editing an existing record.  it only gets called when i hit cancel or save in the edit form which does not let me set default checked states for the checkboxes in the dropdown...

Can you try your example and set a breakpoint and see if during hitting edit on a RadGrid row, it calls the event while it is creating the edit form?

Cheers,
Rob
0
Rosen
Telerik team
answered on 23 Sep 2008, 11:46 AM
Hi Rob,

I'm afraid I'm unable to reproduce the behavior, which you have described. I have attached a full modified version of the previously sent page. Please give it a try and see if I'm missing something obvious.

Sincerely yours,
Rosen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rob
Top achievements
Rank 1
answered on 23 Sep 2008, 11:24 PM
Thanks, it turned out I just needed to assign the event higher up in the code.

Tags
Grid
Asked by
Rob
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Shinu
Top achievements
Rank 2
d-cpt
Top achievements
Rank 2
Rob
Top achievements
Rank 1
Rosen
Telerik team
BaiH
Top achievements
Rank 1
Share this question
or