locked
two dropDownList in gridview country and city, Change city value when selected value from Country (RowCommand), Please help me fast RRS feed

  • Question

  • User-1952516322 posted

    Hello,

    I Have two DropDownList in gridView, first one is Country and second one is City, I fill the country DropDownList in (OnRowDataBound), then what I want, when the user selected value from country dropDownList, the City DorpDownList must fill with value of Country Selected,

    I tried the command name and command arguments, but the command name Or command argument not show in attributes of dropdownlist?

    how can I do this please?

    Here is My Code

     protected void grvList_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DataTable dtCountry = new DataTable();
                    dtCountry.Columns.Add("ID");
                    dtCountry.Columns.Add("NAME");
                    dtCountry.Rows.Add(1, "JORDAN");
                    dtCountry.Rows.Add(1, "UAE");
                   
                    DropDownList ddlCountry = (DropDownList)e.Row.FindControl("ddlCountry") as DropDownList;
                    ddlCountry.DataSource = dtCountry;
                    ddlCountry.DataValueField = "ID";
                    ddlCountry.DataTextField = "NAME";
                    ddlCountry.DataBind();
     }
            }
    
    

     

     <asp:GridView ID="grvList" runat="server" AutoGenerateColumns="false" OnRowDataBound="grvList_RowDataBound" OnRowCommand="grvList_RowCommand">
                <Columns>
                <asp:BoundField HeaderText="NAME" DataField="NAME"/>
                <asp:BoundField HeaderText="GENDER" DataField="GENDER" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlCountry" runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlCity" runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>

     

    Saturday, March 4, 2017 8:44 AM

Answers

  • User-1716253493 posted
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlCountry=(DropDownList)sender;
            DropDownList ddlCity = (DropDownList)ddl1.NamingContainer.FindControl("ddlCity");
            //populate ddl2 items based ddl1.selectedvalue
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 4, 2017 9:44 AM
  • User-1716253493 posted

    I have simplify the codes, the dropdown namingcontainer is the gridviewrow

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlCountry=(DropDownList)sender;
            GridViewRow row = (GridViewRow)ddlCountry.NamingContainer;
            DropDownList ddlCity = (DropDownList)row.FindControl("ddlCity");
            //populate ddl2 items based ddl1.selectedvalue
        }

    the dropdown is inside gridviewrow

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 4, 2017 11:54 AM

All replies

  • User-1716253493 posted
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlCountry=(DropDownList)sender;
            DropDownList ddlCity = (DropDownList)ddl1.NamingContainer.FindControl("ddlCity");
            //populate ddl2 items based ddl1.selectedvalue
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 4, 2017 9:44 AM
  • User-1952516322 posted

    oned_gk Thank you very much.

    I tried it and it works successfully ,, but please, if you can explain what is the NamingContainer do? what is the benefits? when should be used?

    Saturday, March 4, 2017 10:14 AM
  • User-1716253493 posted

    I have simplify the codes, the dropdown namingcontainer is the gridviewrow

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlCountry=(DropDownList)sender;
            GridViewRow row = (GridViewRow)ddlCountry.NamingContainer;
            DropDownList ddlCity = (DropDownList)row.FindControl("ddlCity");
            //populate ddl2 items based ddl1.selectedvalue
        }

    the dropdown is inside gridviewrow

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 4, 2017 11:54 AM