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

Programmatic two way databinding Combobox

5 Answers 125 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Trent
Top achievements
Rank 2
Trent asked on 30 Mar 2011, 07:49 AM
Hello,

I have a bit of an issue:

I have four ComboBoxes on the page as seen below:

<telerik:RadAjaxManagerProxy ID="RadAjaxManagerProxy1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="ddlCountry">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="ddlState" UpdatePanelHeight="" />
            </UpdatedControls>
        </telerik:AjaxSetting>
        <telerik:AjaxSetting AjaxControlID="ddlState">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="ddlLocation" UpdatePanelHeight="" />
            </UpdatedControls>
        </telerik:AjaxSetting>
        <telerik:AjaxSetting AjaxControlID="ddlLocation">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="ddlPostcode" UpdatePanelHeight="" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManagerProxy>
 
 
                    <table id="Table1" width="100%" class="style1">
                        <tr runat="server" width="150px" id="tr10">
                            <td>
                                Country 
                            </td>
                            <td>
                                <telerik:RadComboBox ID="ddlCountry" runat="server" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"
                                    Width="300px" EnableLoadOnDemand="True" AutoPostBack="True">
                                </telerik:RadComboBox>
                                <asp:RequiredFieldValidator ID="rfvCountry" runat="server" ErrorMessage="*" ControlToValidate="ddlCountry"
                                    ValidationGroup="Customer"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr runat="server" id="trState">
                            <td>
                                State 
                            </td>
                            <td>
                                <telerik:RadComboBox ID="ddlState" OnSelectedIndexChanged="ddlState_SelectedIndexChanged"
                                    Width="300px" runat="server" AutoPostBack="True">
                                </telerik:RadComboBox>
                                <asp:RequiredFieldValidator ID="rfvState" runat="server" ErrorMessage="*" ControlToValidate="ddlState"
                                    ValidationGroup="Customer"></asp:RequiredFieldValidator>
                            </td>
                            <td>
                                Location 
                            </td>
                            <td>
                                <telerik:RadComboBox ID="ddlLocation" OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged"
                                    Width="300px" runat="server" AutoPostBack="True">
                                </telerik:RadComboBox>
                                <asp:RequiredFieldValidator ID="rfvLocation" runat="server" ErrorMessage="*" ControlToValidate="ddlLocation"
                                    ValidationGroup="Customer"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr runat="server" id="trPostcode">
                            <td>
                                Postcode 
                            </td>
                            <td>
                                <telerik:RadComboBox ID="ddlPostcode" runat="server" Width="300px">
                                </telerik:RadComboBox>
                                <asp:RequiredFieldValidator ID="rfvPostcode" runat="server" ErrorMessage="*" ControlToValidate="ddlPostcode"
                                    ValidationGroup="Customer"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                    </table>

all data-bound from c# like so (including selected index changed);

#region Location ddls
private void BindCountry()
{
    string sql = "SELECT DISTINCT LookupID_LookupTable, LookupID, LookupParentID FROM " + XDEV.xd_Schema.ToString() + "xd_lookup_tables WHERE (LookupParentID = 41)";
    DataTable dt = data.GetDataTable(sql);
    if (dt.Rows.Count > 0)
    {
        ddlCountry.DataSource = dt;
        ddlCountry.DataTextField = "LookupID_LookupTable";
        ddlCountry.DataValueField = "LookupID";
        ddlCountry.DataBind();
        //RadAjaxManager1.FocusControl(ddlCountry);
    }
}
private void BindState(string country)
{
    string sql = string.Format("SELECT DISTINCT LookupID_LookupTable, LookupID, LookupParentID FROM " + XDEV.xd_Schema.ToString() + "xd_lookup_tables WHERE ( LookupID_ParentLookupID ={0})", country);
    DataTable dt = data.GetDataTable(sql);
    if (dt.Rows.Count > 0)
    {
        ddlState.DataSource = dt;
        ddlState.DataTextField = "LookupID_LookupTable";
        ddlState.DataValueField = "LookupID";
        ddlState.DataBind();
        //RadComboBoxItem StateItem = new RadComboBoxItem("- Select a State -", "0"); //Creates new item for RadCombobox 
        //ddlState.Items.Add(StateItem);
    }
}
private void BindLocation(string state)
{
    string sql = string.Format("SELECT DISTINCT LookupID_LookupTable, LookupID, LookupParentID FROM " + XDEV.xd_Schema.ToString() + "xd_lookup_tables WHERE ( LookupID_ParentLookupID = {0})", state);
    DataTable dt = data.GetDataTable(sql);
    if (dt.Rows.Count > 0)
    {
        ddlLocation.DataSource = dt;
        ddlLocation.DataTextField = "LookupID_LookupTable";
        ddlLocation.DataValueField = "LookupID";
        ddlLocation.DataBind();
        //RadComboBoxItem LocationItem = new RadComboBoxItem("- Select a Location -", "0"); //Creates new item for RadCombobox 
        //ddlLocation.Items.Add(LocationItem);
    }
}
private void BindPostcode(string location)
{
    string sql = string.Format("SELECT DISTINCT LookupID_LookupTable, LookupID, LookupParentID FROM " + XDEV.xd_Schema.ToString() + "xd_lookup_tables WHERE ( LookupID_ParentLookupID = {0})", location);
    DataTable dt = data.GetDataTable(sql);
    if (dt.Rows.Count > 0)
    {
        ddlPostcode.DataSource = dt;
        ddlPostcode.DataTextField = "LookupID_LookupTable";
        ddlPostcode.DataValueField = "LookupID";
        ddlPostcode.DataBind();
        //RadComboBoxItem PostcodeItem = new RadComboBoxItem("- Select a Postcode -", "0"); //Creates new item for RadCombobox 
        //ddlPostcode.Items.Add(PostcodeItem);
    }
}
 
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
    BindState(ddlCountry.SelectedValue);
}
 
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
    BindLocation(ddlState.SelectedValue);
}
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
    BindPostcode(ddlLocation.SelectedValue);
}
#endregion

and the existing records are bound like so:

#region cascading ddls
if (!String.IsNullOrEmpty(dt.Rows[0]["LookupID_Country"].ToString()))
{
    if (ddlCountry.Items.Contains(ddlCountry.Items.FindItemByValue(dt.Rows[0]["LookupID_Country"].ToString())))
    {
        ddlCountry.SelectedValue = dt.Rows[0]["LookupID_Country"].ToString();
        BindCountry();
    }
}
if (!String.IsNullOrEmpty(dt.Rows[0]["LookupID_State"].ToString()))
{
    if (ddlState.Items.Contains(ddlState.Items.FindItemByValue(dt.Rows[0]["LookupID_State"].ToString())))
    {
        ddlState.SelectedValue = dt.Rows[0]["LookupID_State"].ToString();
        BindState(ddlCountry.SelectedValue);
    }
    else
    {
         
    }
}
if (!String.IsNullOrEmpty(dt.Rows[0]["LookupID_Location"].ToString()))
{
    if (ddlLocation.Items.Contains(ddlLocation.Items.FindItemByValue(dt.Rows[0]["LookupID_Location"].ToString())))
    {
        ddlLocation.SelectedValue = dt.Rows[0]["LookupID_Location"].ToString();
        BindLocation(ddlState.SelectedValue);
    }
}
if (!string.IsNullOrEmpty(dt.Rows[0]["LookupID_Postcode"].ToString()))
{
    if (ddlPostcode.Items.Contains(ddlPostcode.Items.FindItemByValue(dt.Rows[0]["LookupID_Postcode"].ToString())))
    {
        ddlPostcode.SelectedValue = dt.Rows[0]["LookupID_Postcode"].ToString();
        BindPostcode(ddlLocation.SelectedValue);
    }
}
#endregion);
what is currently occurring is the first ComboBox is being bound with the record but the other 3  are blank where they should display the records from the db.

What needs to happen is when the user clicks 'Edit' button the user control becomes visible with ComboBoxes displaying the existing(non-edited data) and allow the user to change the ComboBox selected value.

5 Answers, 1 is accepted

Sort by
0
Trent
Top achievements
Rank 2
answered on 01 Apr 2011, 01:40 PM
Hello!!!!!,

is there any one answering the Telerik forums??? time is running out
0
Dimitar Terziev
Telerik team
answered on 06 Apr 2011, 01:20 PM
Hello Trent,

Your code seems to be correct and the only reason for this problem is because the condition is not fulfilled:

if (!String.IsNullOrEmpty(dt.Rows[0]["LookupID_State"].ToString()))
{
    if (ddlState.Items.Contains(ddlState.Items.FindItemByValue(dt.Rows[0]["LookupID_State"].ToString())))
    {
        ddlState.SelectedValue = dt.Rows[0]["LookupID_State"].ToString();
        BindState(ddlCountry.SelectedValue);
    }
    else

If the condition fails the related ComboBoxes are not bound and thus you end up with no items.
When this happens you can't use SelectedIndexChanged event of the first RadComboBox because of the RequiredFiledValidators.
The page could not postback because the other RadComboBoxes have no items.

Greetings,
Dimitar Terziev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Trent
Top achievements
Rank 2
answered on 08 Apr 2011, 01:26 PM
so how do I get the desired result?
0
Dimitar Terziev
Telerik team
answered on 19 Apr 2011, 05:06 PM
Hello Trent,

In order to troubleshoot this issues place a break point inside the "if"  statement clause. If the condition is not fulfilled then there is not such item:
ddlState.Items.Contains(ddlState.Items.FindItemByValue(dt.Rows[0]["LookupID_State"].ToString()))

On other hand, if the condition is fulfilled and the "State" RadComboBox is data bound, then we would try to find another way to troubleshoot the problem

Regards,
Dimitar Terziev
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Trent
Top achievements
Rank 2
answered on 16 Oct 2011, 02:53 AM

Hello Dimitar Terziev,

Sorry for the delayed response... I was called away to work on another project that grew to be a bigger pain than foreseen.. I will black the breakpoint  and report back with the results.

Tags
ComboBox
Asked by
Trent
Top achievements
Rank 2
Answers by
Trent
Top achievements
Rank 2
Dimitar Terziev
Telerik team
Share this question
or