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

Selected value for 3 ComboBox from databse

3 Answers 74 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Ratul
Top achievements
Rank 1
Ratul asked on 16 Aug 2011, 01:35 AM
Hi, I have three ComboBox in my webform which are used to for country, district and area for a user when they register in my site. 2nd ComboBox is populated when first one is selected and 3rd one when 2nd is selected. I have successfully databind these 3 from database. but after registering, if a user want to edit their data I want to pre-select all three ComboBoxes with the value which they select when registering at page load. I cannot find any idea how to do this... Can anyone please help me??

3 Answers, 1 is accepted

Sort by
0
Ratul
Top achievements
Rank 1
answered on 16 Aug 2011, 10:14 PM
Can anyone please reply this post??
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Aug 2011, 09:03 AM
Hello,

<telerik:RadComboBox ID="cmbCountry"  runat="server"  AutoPostBack="true" CausesValidation="false"
            onselectedindexchanged="cmbCountry_SelectedIndexChanged"></telerik:RadComboBox>
        <telerik:RadComboBox ID="cmbState"  runat="server" AutoPostBack="true" CausesValidation="false"
            onselectedindexchanged="cmbState_SelectedIndexChanged"></telerik:RadComboBox>
        <telerik:RadComboBox ID="cmbCity"  runat="server"></telerik:RadComboBox>
protected void Page_Load(object sender, EventArgs e)
{
 
 
    //for edit mode.
    if (!IsPostBack)
    {
        int countryId = 1; // set CoutryId
        int stateId = 1; // set StateId
        int cityId = 1; //set City Id
 
        BindCoutry();
        cmbCountry.SelectedValue = countryId.ToString();
        BindState(countryId.ToString());
        cmbState.SelectedValue = stateId.ToString();
        BindCity(stateId.ToString());
        cmbCity.SelectedValue = cityId.ToString();
 
         
    }
}
protected void BindCoutry()
{
    cmbCountry.DataSource = null; // assign your coutry list
    cmbCountry.DataTextField = "coutryName";
    cmbCountry.DataValueField = "coutryID";
    cmbCountry.DataBind();
}
protected void BindState(string coutryID)
{
    if (!string.IsNullOrEmpty(coutryID))
    {
        cmbState.DataSource = null; // assign state list by coutryID
        cmbState.DataTextField = "StateName";
        cmbState.DataValueField = "StateID";
        cmbState.DataBind();
    }
    else
    {
        cmbState.Items.Clear();
    }
}
protected void BindCity(string StateID)
{
    if (!string.IsNullOrEmpty(StateID))
    {
        cmbCity.DataSource = null; // assign your city list by stateID
        cmbCity.DataTextField = "CityName";
        cmbCity.DataValueField = "CityID";
        cmbCity.DataBind();
    }
    else
    {
        cmbCity.Items.Clear();
    }
}
protected void cmbCountry_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    if (!string.IsNullOrEmpty(cmbCountry.SelectedValue))
    {
        BindState(cmbCountry.SelectedValue);
    }
    else
    {
        cmbState.Items.Clear();
    }
    cmbCity.Items.Clear();
}
protected void cmbState_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    if (!string.IsNullOrEmpty(cmbState.SelectedValue))
    {
        BindCity(cmbState.SelectedValue);
    }
    else
    {
        cmbCity.Items.Clear();
    }
     
}

let me know if any concern.

Thanks,
Jayesh Goyani
0
Ratul
Top achievements
Rank 1
answered on 17 Aug 2011, 08:38 PM
Thank u very much Jayesh Goyani .. Ur code help me much & I can solve my problem .... It really works .. Thanx again ...
Tags
ComboBox
Asked by
Ratul
Top achievements
Rank 1
Answers by
Ratul
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or