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

Enable/Disable ddl2 based on ddl1 on Edit in radgrid

3 Answers 136 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sachin
Top achievements
Rank 1
Sachin asked on 21 Mar 2009, 04:30 AM
I am using radgrid and having GridboundColumn and GridDropDownColumns in it with edit/insert feature. I am implementing the feature like if user click on Edit of the gird the form is opening up enabling the user to make the changes, but depending on value of 'qualificaton' dropdown i have to disbale/enable the dropdown 'Profession' so i used itemdatabound event of the radgrid and able to disable the dropdown(Profession) depending on 'qualificaton' dropdown ... now i also have autopostback true on dropdown (Profession) to enable/disable 'profession' dropdown

i.e. say dropdown 1st has value as 1 and user click on Edit and i disabled the 2nd dropdown using itemdatabound event. Now user changed the 1st dropdown value to 2 now i have to enable the dropdown two so tried to use indexchangedevent for 1st dropdown(autopostback = true) but i am not able to enable the dropdown two may be because i disabled it earlier onitemdatabound event. Please let me know the workaround to achieve it.

<telerik:RadGrid ID="radGrid1" runat="server" AllowAutomaticDeletes="True"
                                        AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowPaging="True"
                                        AllowSorting="True" DataSourceID="SqlDataSource1" GridLines="None"
                                        onitemcreated="radGrid1_ItemCreated"
                                        onitemupdated="radGrid1_ItemUpdated" PageSize="3" Skin="Office2007"
                                        Width="600px" onitemdatabound="radGrid1_ItemDataBound"
                                        onselectedindexchanged="radGrid1_SelectedIndexChanged">
                                        <clientsettings>
                                            <scrolling allowscroll="True" usestaticheaders="True" />
                                        </clientsettings>
                                        <mastertableview autogeneratecolumns="False" commanditemdisplay="Top"
                                            datakeynames="user_id" datasourceid="SqlDataSource1" pagesize="5">
                                            <Columns>
                         <telerik:GridBoundColumn DataField="user_id" HeaderText="ID" ReadOnly="True"
                                                    SortExpression="user_id" UniqueName="userid" Visible="False">                        
                                                </telerik:GridBoundColumn>
                        <telerik:GridDropDownColumn DataField="qualificaton"
                                                    DataSourceID="SqlDataSource2" DropDownControlType="DropDownList"
                                                    HeaderText="Qualificaton" ListTextField="qualificaton"
                                                    ListValueField="qualificaton" UniqueName="qualificaton">
                                                </telerik:GridDropDownColumn>
                         <telerik:GridDropDownColumn DataField="field"
                                                    DataSourceID="SqlDataSource3" DropDownControlType="DropDownList"
                                                    HeaderText="Profession" ListTextField="field"
                                                    ListValueField="field" UniqueName="field">
                                                </telerik:GridDropDownColumn>
                        </columns>
                    </mastertableview>
                    </telerik:radgrid>


protected void radGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {   
        if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            GridEditManager editMan = editedItem.EditManager;

            GridDropDownColumnEditor ddl1 = editMan.GetColumnEditor("qualificaton") as GridDropDownColumnEditor;


            if (ddlStaff.SelectedText == "1")
            {
                if (e.Item.ItemType == GridItemType.EditFormItem)
                {

                    GridEditableItem editedItem1 = e.Item as GridEditableItem;

                    editedItem1["field"].Enabled = false;
                }
            }
        }        
    }


protected void radGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {        
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
         
     
            // to assign the indexchangedevent and to set autopostback true of the dropdown in edit mode of the radgrid
            GridEditableItem item = (GridEditableItem)e.Item;
            DropDownList ddl1 = item["qualificaton"].Controls[0] as DropDownList;
            ddl1.AutoPostBack = true;
            ddl1.SelectedIndexChanged += new EventHandler(this.ddl1_SelectedIndexChanged);
        }
        
    }

    void ddlStaff_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridEditableItem editedItem = (sender as DropDownList).NamingContainer as GridEditableItem;
        DropDownList ddl1 = editedItem["qualificaton"].Controls[0] as DropDownList;

        DropDownList ddl2 = editedItem["field"].Controls[0] as DropDownList;

        if (ddl1.SelectedValue == "1")
        {         
            ddl2.Enabled = false;            
        }
        else
        {         
            ddl2.Enabled = true;            
        }
    } 

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 23 Mar 2009, 07:34 AM
Hi Sachin,

In the above given code you are accessing the DropDownList in the ItemCreated event as 'ddl1' but you have assigned the SelectedIndexChanged event for 'ddlStaff'. Try with the following code snippet and see if it works.

CS:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode) 
        { 
            GridEditableItem editedItem = e.Item as GridEditableItem; 
 
 
            DropDownList ddl1 = (DropDownList)editedItem["qualificaton"].Controls[0]; 
 
 
            if (ddl1.SelectedValue == "1"
            { 
 
                DropDownList ddl = editedItem["field"].Controls[0] as DropDownList; 
                    ddl.Enabled=false
                
            } 
        }         
    }  
 
 
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
        { 
            // to assign the indexchangedevent and to set autopostback true of the dropdown in edit mode of the radgrid 
            GridEditableItem item = (GridEditableItem)e.Item; 
            DropDownList ddl1 = item["qualificaton"].Controls[0] as DropDownList; 
            ddl1.AutoPostBack = true
            ddl1.SelectedIndexChanged += new EventHandler(this.ddl1_SelectedIndexChanged); 
        } 
    } 
    void ddl1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
 
        GridEditableItem editedItem = (sender as DropDownList).NamingContainer as GridEditableItem; 
        DropDownList ddl1 = editedItem["qualificaton"].Controls[0] as DropDownList; 
 
        DropDownList ddl2 = editedItem["field"].Controls[0] as DropDownList; 
 
        if (ddl1.SelectedValue == "1"
        { 
            ddl2.Enabled = false
        } 
        else 
        { 
            ddl2.Enabled = true
        } 
    }   


Thanks
Shinu.


0
Sachin
Top achievements
Rank 1
answered on 23 Mar 2009, 04:22 PM
Hi Shinu,

It was a typo... i am using it as a ddl1 only,  i am having the code you sent me and not able to accomplish this way.
0
Shinu
Top achievements
Rank 2
answered on 24 Mar 2009, 04:49 AM
Hi Sachin,

In your above given code you are accessing the GridColumnEditor and then trying to disable the second field depending on whether the value is 1 or not. But in my code I am accessing the DropDownList as such and disabling the second one accordingly. This was working fine on my end. Could you please point me where this code is breaking.

Shinu
Tags
Grid
Asked by
Sachin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Sachin
Top achievements
Rank 1
Share this question
or