Selected value for 3 ComboBox from databse

4 posts, 1 answers
  1. Ratul
    Ratul avatar
    3 posts
    Member since:
    Aug 2011

    Posted 15 Aug 2011 Link to this post

    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??
  2. Ratul
    Ratul avatar
    3 posts
    Member since:
    Aug 2011

    Posted 16 Aug 2011 Link to this post

    Can anyone please reply this post??
  3. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 17 Aug 2011 Link to this post

    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
  4. Ratul
    Ratul avatar
    3 posts
    Member since:
    Aug 2011

    Posted 17 Aug 2011 Link to this post

    Thank u very much Jayesh Goyani .. Ur code help me much & I can solve my problem .... It really works .. Thanx again ...
Back to Top