Click here to Skip to main content
12,070,351 members (45,879 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
Dear Friends,
I was struck in between while selecting the ddlCountry under gridview control edit item template the ddlState Should pop up according to country selection it is not happening please fin it out.

My code is below..
RowDataBound Im binding.. protected void GrdView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView drv = e.Row.DataItem as DataRowView;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList ddlCountry = (DropDownList)e.Row.FindControl("ddlCountry");
                DropDownList ddlState = (DropDownList)e.Row.FindControl("ddlState");
                DataSet dsGrdValues = BindGridView();
                DataSet dsCountrys = BindCountrys();
                string SelCountry = drv[4].ToString();
                string SelState = drv[5].ToString();
                ddlCountry.DataSource = dsCountrys;
                ddlCountry.DataTextField = "Name";
                ddlCountry.DataValueField = "Id";
                ddlCountry.DataBind();
                for (int i = 0; i < ddlCountry.Items.Count; i++)
                {
                    if (ddlCountry.Items[i].Text.Trim().CompareTo(SelCountry.Trim())==0)
                    {
                        ddlCountry.SelectedIndex = i;
                        break;
                    }
                }
                DataSet dsStates = BindStates(ddlCountry.SelectedValue);
                ddlState.DataSource = dsStates;
                ddlState.DataTextField = "Name";
                ddlState.DataValueField = "Id";
                ddlState.DataBind();
                for (int i = 0; i < ddlState.Items.Count; i++)
                {
                    if (ddlState.Items[i].Text.Trim().CompareTo(SelState.Trim()) == 0)
                    {
                        ddlState.SelectedIndex = i;
                        break;
                    }
                }
            }
        }
    }

This is my GridView ddlCountry Selected change event where i am not getting the specific states on country changes..
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            DropDownList Country = (DropDownList)sender;
            //DropDownList State = Country.Parent as DropDownList;
            //State = new DropDownList();
            GridViewRow row = (GridViewRow)(Country.Parent.Parent);
            Country.DataTextField = "Name";
            string CCode = Country.SelectedItem.Value;
            DataSet dsGetStates = BindStates(CCode);
            ddlState1.DataSource = dsGetStates;
            ddlState1.DataTextField = "Name";
            if (dsGetStates.Tables[0].Rows.Count > 0)
            {
                //State.DataSource = dsGetStates;
                //State.DataTextField = "Name";
                //State.DataValueField = "Id";
                //State.DataBind();
            }
            else
            {
                //State.DataSource = null;
                //State.Items.Clear();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

Here is my Page Design .aspx Code
<pre lang="xml"><asp:TemplateField HeaderText="Country">
  <ItemTemplate>
  <asp:Label ID="lblCountry" runat="server" Text='<%#Eval("Country") %>'></asp:Label>
Posted 13-Feb-13 2:48am
sahmed4517
Comments
Sheikh Muhammad Haris 13-Feb-13 9:37am
   
Your code looks fine. What is the issue you are having ?
richcb 13-Feb-13 10:01am
   
Have you tried debugging and seeing where it stops performing as you expect?
sahmed4 13-Feb-13 19:21pm
   
The problem is i have an 4 dropdown list controls two of ddlCountry,ddlState in my form and rest of the two is inside edititem template of my gridview control...while editing the gridview control i wanted to populate the list of states based on country selected in edititemtemplate ddlCountry selectedindex change event...once afetr selecting the country when i try populating the states to ddlState of my ddlState inside edititemtemplate is not finding the control instead it's binding up to my ddlState control of my form...I hope my problem is clear please find me the solution i need to populate the staes of ddlState(edititemtemplate) controls on selecting country...

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

   DropDownList ddlState = (DropDownList)row.FindControl("ddlState");
if (dsGetStates.Tables[0].Rows.Count > 0)
          {
              ddlState.DataSource = dsGetStates;
              ddlState.DataTextField = "Name";
              ddlState.DataBind();
          }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100