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

Change Radcombobox datasource by using another comobobox's selected value

2 Answers 666 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Shuja
Top achievements
Rank 1
Shuja asked on 25 May 2011, 02:24 PM
Hi,

i have a RadGrid with templated columns. I have a ReportLevel RadCombobox & a CostCentreLow RadComboBox in the edit template.
The CostCentreLow Radcombobox is populated from a SqlDatasource but what i want to do is base the sqldatasource select command on the value selected in the ReportLevel combobox & then rebind datasource to the CostCentreLow combobox. I need this to work on the reportlevel selectedIndexChanged.
I have tried the RadGrid ItemCreated method but i get a databind error when i try to change the SqlDataSource select command in the ReportLevel SelectedIndexChanged event & it doesn't work if i try to bind the datasource in the Radgrid ItemCreated event (as i need the datasource changed based on the value in the ReportLevel ComboBox).

here is the code for my project:

<telerik:RadGrid ID="RadGrid1" GridLines="None" runat="server" AllowAutomaticDeletes="True"
    AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowPaging="True"
    AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Skin="Web20" 
    Width="99%" AllowFilteringByColumn="True" OnItemUpdated="RadGrid1_ItemUpdated"
    OnItemDeleted="RadGrid1_ItemDeleted" OnItemInserted="RadGrid1_ItemInserted" 
    OnDataBound="RadGrid1_DataBound" OnItemDataBound="RadGrid1_ItemDataBound"  OnItemCreated="RadGrid1_ItemCreated"
    onitemcommand="RadGrid1_ItemCommand" AllowSorting="True" Culture="en-GB" 
    CssClass="Grid" Font-Size="X-Small" Font-Names="verdana" 
    PageSize="15"   >
    <PagerStyle Mode="NextPrevAndNumeric" />
    <AlternatingItemStyle BackColor="#E8F1FF" />
    <MasterTableView  CommandItemDisplay="TopAndBottom" DataKeyNames="U_ID"  
        DataSourceID="SqlDataSource1" HorizontalAlign="NotSet" AutoGenerateColumns="False">
        <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column"></RowIndicatorColumn>
        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"></ExpandCollapseColumn>
        <Columns>
            <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                <ItemStyle CssClass="MyImageButton" Width="20px" />
            </telerik:GridEditCommandColumn>
            <telerik:GridTemplateColumn HeaderText="ID" SortExpression="U_ID" UniqueName="U_ID" AllowFiltering="true"
                EditFormColumnIndex="0" FilterControlWidth="50%" DataField="U_ID" >
                <ItemTemplate>
                    <asp:Label runat="server" CssClass="Label2" Width="30px" ID="U_ID" Text='<%# Eval("U_ID") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="UserName" SortExpression="UserName" UniqueName="UserName" AllowFiltering="true"
                EditFormColumnIndex="0" FilterControlWidth="70%" DataField="UserName" >
                <EditItemTemplate>
                    <span style="font-size:13px;color:Red;">
                    <telerik:RadTextBox runat="server" Width="200px" Text='<%# Bind("UserName") %>' ID="UserName" Visible="true" MaxLength="12" >
                    </telerik:RadTextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator9" ControlToValidate="UserName"
                    ErrorMessage="*" runat="server">
                    </asp:RequiredFieldValidator>
                    </span>                            
                </EditItemTemplate>
                <InsertItemTemplate>
                    <span style="font-size:13px;color:Red;">
                    <telerik:RadTextBox runat="server" Width="200px" Text='<%# Bind("UserName") %>' ID="UserName" Visible="true" MaxLength="12" >
                    </telerik:RadTextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator10" ControlToValidate="UserName"
                    ErrorMessage="*" runat="server">
                    </asp:RequiredFieldValidator>
                    </span>       
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label runat="server" CssClass="Label2" Width="80px" ID="UserName" Text='<%# Eval("UserName") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Report Level" SortExpression="ReportLevel" UniqueName="ReportLevel" AllowFiltering="true"
                EditFormColumnIndex="0" FilterControlWidth="70%" DataField="ReportLevel" >
                <EditItemTemplate>
                    <span style="font-size:13px; color:Red;">
                    <telerik:RadComboBox runat="server" Width="205px" Height="100px"
                        EmptyMessage="Select a value"
                        HeaderText="ReportLevel"  SelectedValue='<%# Bind("ReportLevel") %>'
                        UniqueName="ReportLevel" ID="ReportLevel"  >
                        <Items>
                        <telerik:RadComboBoxItem Value="" Text="" />
                        <telerik:RadComboBoxItem Value="CC" Text="CC" />
                        <telerik:RadComboBoxItem Value="DIR" Text="DIR" />
                        </Items>
                    </telerik:RadComboBox
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" ControlToValidate="ReportLevel"
                    ErrorMessage="*" runat="server">
                    </asp:RequiredFieldValidator>
                    </span>                        
                </EditItemTemplate>
                <InsertItemTemplate>
                    <span style="font-size:13px; color:Red;">
                    <telerik:RadComboBox runat="server" Width="205px" Height="100px"
                        EmptyMessage="Select a value"
                        HeaderText="ReportLevel"  SelectedValue='<%# Bind("ReportLevel") %>'
                        UniqueName="ReportLevel" ID="ReportLevel"  >
                        <Items>
                        <telerik:RadComboBoxItem Value="" Text="" />
                        <telerik:RadComboBoxItem Value="CC" Text="CC" />
                        <telerik:RadComboBoxItem Value="DIR" Text="DIR" />
                        </Items>
                    </telerik:RadComboBox
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" ControlToValidate="ReportLevel"
                    ErrorMessage="*" runat="server">
                    </asp:RequiredFieldValidator>
                    </span>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label runat="server" CssClass="Label2" Width="80px" ID="lblReportLevel" Text='<%# Eval("ReportLevel") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Cost Centre Low" SortExpression="CostCentreLow" UniqueName="CostCentreLow" AllowFiltering="true"
                EditFormColumnIndex="0" FilterControlWidth="70%" DataField="CostCentreLow" >
                <EditItemTemplate>
                    <span style="font-size:13px; color:Red;" runat="server"  >
                        <telerik:RadComboBox ID="CostCentreLow" runat="server" Enabled="false" 
                            DataSourceID="DS_CostCentre" DataTextField="costcentre" 
                            DataValueField="costcentre" DropDownWidth="200px"
                            SelectedValue='<%# Bind("CostCentreLow") %>'  Width="205px" TabIndex="8" Font-Size="X-Small" Font-Names="Verdana" AppendDataBoundItems="True" >
                        </telerik:RadComboBox>  
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="CostCentreLow"
                    ErrorMessage="*" runat="server">
                    </asp:RequiredFieldValidator>
                    </span>                                                 
                </EditItemTemplate>
                <InsertItemTemplate>
                    <span style="font-size:13px; color:Red;">
                        <telerik:RadComboBox ID="CostCentreLow" runat="server" 
                            DataSourceID="DS_CostCentre" DataTextField="costcentre" 
                            DataValueField="costcentre" DropDownWidth="200px"
                            SelectedValue='<%# Bind("CostCentreLow") %>'  Width="205px" TabIndex="8" Font-Size="X-Small" Font-Names="Verdana" AppendDataBoundItems="True" >
                        </telerik:RadComboBox>  
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="CostCentreLow"
                    ErrorMessage="*" runat="server">
                    </asp:RequiredFieldValidator>
                    </span
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label runat="server" CssClass="Label2" Width="80px" ID="CostCentreLow" Text='<%# Eval("CostCentreLow") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Cost Centre High" SortExpression="CostCentreHigh" UniqueName="CostCentreHigh" AllowFiltering="true"
                EditFormColumnIndex="0" FilterControlWidth="70%" DataField="CostCentreHigh" >
                <EditItemTemplate>
                    <span style="font-size:13px; color:Red;">
                        <telerik:RadComboBox ID="CostCentreHigh" runat="server" Enabled="false" 
                            DataSourceID="DS_CostCentre" DataTextField="costcentre" 
                            DataValueField="costcentre" DropDownWidth="200px"
                            SelectedValue='<%# Bind("CostCentreHigh") %>'  Width="205px" TabIndex="8" Font-Size="X-Small" Font-Names="Verdana" AppendDataBoundItems="True" >
                        </telerik:RadComboBox>  
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" ControlToValidate="CostCentreHigh"
                    ErrorMessage="*" runat="server">
                    </asp:RequiredFieldValidator>
                    </span>                          
                </EditItemTemplate>
                <InsertItemTemplate>
                    <span style="font-size:13px; color:Red;">
                        <telerik:RadComboBox ID="CostCentreHigh" runat="server" 
                            DataSourceID="DS_CostCentre" DataTextField="costcentre" 
                            DataValueField="costcentre" DropDownWidth="200px"
                            SelectedValue='<%# Bind("CostCentreHigh") %>'  Width="205px" TabIndex="8" Font-Size="X-Small" Font-Names="Verdana" AppendDataBoundItems="True" >
                        </telerik:RadComboBox>  
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" ControlToValidate="CostCentreHigh"
                    ErrorMessage="*" runat="server">
                    </asp:RequiredFieldValidator>
                    </span
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label runat="server" CssClass="Label2" Width="80px" ID="CostCentreHigh" Text='<%# Eval("CostCentreHigh") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridButtonColumn ConfirmText="Delete this record?" ConfirmDialogType="RadWindow"
                ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
                UniqueName="DeleteColumn">
                <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
            </telerik:GridButtonColumn>
        </Columns>
        <EditFormSettings ColumnNumber="2" CaptionDataField="U_ID" CaptionFormatString="Edit properties of Record {0}" InsertCaption="New Record">
           <FormTableItemStyle Wrap="False"></FormTableItemStyle>
            <FormTableStyle CellPadding="2" CellSpacing="0" BackColor="#F2FFEC" Height="60px"></FormTableStyle>
            <FormMainTableStyle CellPadding="3" CellSpacing="0" BackColor="#F2FFEC" Width="100%"></FormMainTableStyle>
            <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
            <FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3" BackColor="#F2FFEC"  Width="100%" />
            <FormTableStyle CellSpacing="0" CellPadding="2" Height="60px" BackColor="#F2FFEC" />
            <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
            <EditColumn ButtonType="ImageButton" InsertText="Insert Record" UpdateText="Update Record"
                UniqueName="EditCommandColumn1" CancelText="Cancel edit">
            </EditColumn>
            <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
        </EditFormSettings>
    </MasterTableView>
    <ClientSettings>
        <Selecting AllowRowSelect="True" />
        <ClientEvents OnRowDblClick="RowDblClick" />
        <Selecting AllowRowSelect="True"></Selecting>
        <ClientEvents OnRowDblClick="RowDblClick"></ClientEvents>
    </ClientSettings>
    <EditItemStyle BackColor="#F2FFEC" />
    <ActiveItemStyle BackColor="#FFF8D9" BorderColor="#FFF8D9" BorderStyle="Solid" 
        BorderWidth="1px" />
    <ActiveItemStyle BackColor="#FFF8D9" BorderColor="#FFF8D9" BorderWidth="1px" 
        BorderStyle="Solid"></ActiveItemStyle>
    <SelectedItemStyle BackColor="#FFF8D9" BorderColor="#FFF8D9" 
        BorderStyle="Solid" BorderWidth="1px" />
    <FilterMenu EnableImageSprites="False"></FilterMenu>
    <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Web20"></HeaderContextMenu>
</telerik:RadGrid>

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            //the dropdown list will be the first control in the Controls collection of the corresponding cell  
            RadComboBox list = (e.Item as GridEditableItem)["ReportLevel"].Controls[1] as RadComboBox;
            //attach SelectedIndexChanged event for the drodown control this.combo_SelectedIndexChanged
            list.AutoPostBack = true;
            //list.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(this.list_SelectedIndexChanged);
            list.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(list_SelectedIndexChanged);
            
        }
}
void list_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    //first reference the edited grid item through the NamingContainer attribute
    GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
    //the dropdown list will be the first control in the Controls collection of the corresponding cell  
    //for custom edit forms (WebUserControl/FormTemplate) you can find the column editor with the FindControl(controlId) method  
    RadComboBox ddRepLev = editedItem["ReportLevel"].Controls[1] as RadComboBox;
    RadComboBox ddCostCen = editedItem["CostCentreLow"].Controls[1] as RadComboBox;
    if (ddRepLev.SelectedValue == "CC")
    {
        this.DS_CostCentre.SelectCommand = "SELECT '' AS costcentre UNION SELECT DISTINCT SEGMENT_CODE AS costcentre FROM ORACLE_DS_GL_CoA_Segments WHERE (SEGMENT_NUMBER = 2) "; //AND (EFFECTIVE_STATUS = 'Active')
        this.DS_CostCentre.DataBind();
    }
    else
    {
        this.DS_CostCentre.SelectCommand = "SELECT '' AS costcentre UNION SELECT DISTINCT PARENT_CODE_L2 AS costcentre FROM ORACLE_DS_GL_CoA_Segments WHERE (SEGMENT_NUMBER = 2)  "; //AND (EFFECTIVE_STATUS = 'Active')
        this.DS_CostCentre.DataBind();
    }
    //ddCostCen.DataSourceID = "DS_CostCentre";
    ddCostCen.Enabled = true;
      
}

As mentioned, because i need the datasource changed based on the reportlevel value selected, i need the datasource selectcommand being assigned a new value in the ReportLevel SelectedIndexChanged event but this gives me a databind error.

Can you please help me find a solution to this problem?

Regards,

Shuja 

2 Answers, 1 is accepted

Sort by
0
Shuja
Top achievements
Rank 1
answered on 26 May 2011, 02:32 PM
Hi,

As a follow up to my original post, i have tried tod databind the combobox rather than the datasource but i still get the same error:
 
System.InvalidOperationException was unhandled by user code
  Message=Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
  Source=System.Web
  StackTrace:
       at System.Web.UI.Page.GetDataItem()
       at ASP.financereports_usernamecostcentrelookup_aspx.__DataBinding__control59(Object sender, EventArgs e) in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\searcheditforms\c6c23503\c5968293\App_Web_aby0d2se.0.cs:line 0
       at System.Web.UI.Control.OnDataBinding(EventArgs e)
       at Telerik.Web.UI.RadComboBox.OnDataBinding(EventArgs e)
       at Telerik.Web.UI.RadComboBox.PerformSelect()
       at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
       at Telerik.Web.UI.RadComboBox.DataBind()
       at ExtOrgs.list_SelectedIndexChanged(Object sender, RadComboBoxSelectedIndexChangedEventArgs e) in c:\Documents and Settings\ahmadshu\My Documents\Visual Studio 2010\SearchEditForms\FinanceReports\UsernameCostCentreLookup.aspx.cs:line 212
       at Telerik.Web.UI.RadComboBox.OnSelectedIndexChanged()
       at Telerik.Web.UI.RadComboBox.RaisePostDataChangedEvent()
       at Telerik.Web.UI.RadDataBoundControl.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent()
       at System.Web.UI.Page.RaiseChangedEvents()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:

The code i changed now look like this:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            //the dropdown list will be the first control in the Controls collection of the corresponding cell  
            RadComboBox list = (e.Item as GridEditableItem)["ReportLevel"].Controls[1] as RadComboBox;
            //attach SelectedIndexChanged event for the drodown control this.combo_SelectedIndexChanged
            list.AutoPostBack = true;
            //list.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(this.list_SelectedIndexChanged);
            list.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(list_SelectedIndexChanged);
        }
}
void list_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    //first reference the edited grid item through the NamingContainer attribute
    GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
    //the dropdown list will be the first control in the Controls collection of the corresponding cell  
    //for custom edit forms (WebUserControl/FormTemplate) you can find the column editor with the FindControl(controlId) method  
    RadComboBox ddRepLev = editedItem["ReportLevel"].Controls[1] as RadComboBox;
    RadComboBox ddCostCen = editedItem["CostCentreLow"].Controls[1] as RadComboBox;
    if (ddRepLev.SelectedValue == "CC")
    {
        DS_CostCentre.SelectCommand = "SELECT '' AS costcentre UNION SELECT DISTINCT SEGMENT_CODE AS costcentre FROM ORACLE_DS_GL_CoA_Segments WHERE (SEGMENT_NUMBER = 2) "; //AND (EFFECTIVE_STATUS = 'Active')
        ddCostCen.DataBind();
    }
    else
    {
        DS_CostCentre.SelectCommand = "SELECT '' AS costcentre UNION SELECT DISTINCT PARENT_CODE_L2 AS costcentre FROM ORACLE_DS_GL_CoA_Segments WHERE (SEGMENT_NUMBER = 2)  "; //AND (EFFECTIVE_STATUS = 'Active')
        ddCostCen.DataBind();
    }
    //ddCostCen.DataSourceID = "DS_CostCentre";
    ddCostCen.Enabled = true;
}

It seems that i can't databind anything (comboBox or datasource) when i'm in the SelectedIndexChanged event. Is there a way around this or a method to data bind the combobox to the new datasource value?

Regards,

Shuja
0
Calleigh
Top achievements
Rank 1
answered on 26 May 2011, 02:41 PM
Tags
ComboBox
Asked by
Shuja
Top achievements
Rank 1
Answers by
Shuja
Top achievements
Rank 1
Calleigh
Top achievements
Rank 1
Share this question
or