Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..
C#
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..
C#
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
C#
<pre lang="xml"><asp:TemplateField HeaderText="Country">
  <ItemTemplate>
  <asp:Label ID="lblCountry" runat="server" Text='<%#Eval("Country") %>'></asp:Label>
  </ItemTemplate>
  <EditItemTemplate>
  <asp:DropDownList ID="ddlCountry" onselectedindexchanged="ddlCountry_SelectedIndexChanged" runat="server" AutoPostBack="True"></asp:DropDownList>
  </EditItemTemplate>
  <FooterTemplate>
  <asp:DropDownList ID="ddlNewCountry" runat="server"></asp:DropDownList>
  </FooterTemplate>
  </asp:TemplateField>

  <asp:TemplateField HeaderText="State">
  <ItemTemplate>
  <asp:Label ID="lblState" runat="server" Text='<%#Eval("State") %>'></asp:Label>
  </ItemTemplate>
  <EditItemTemplate>
  <asp:DropDownList ID="ddlState" runat="server"></asp:DropDownList>
  </EditItemTemplate>
  <FooterTemplate>
  <asp:DropDownList ID="ddlNewState" runat="server"></asp:DropDownList>
  </FooterTemplate>
  </asp:TemplateField>
Posted
Comments
[no name] 13-Feb-13 9:37am    
Your code looks fine. What is the issue you are having ?
Richard C Bishop 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

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900