2017-09-10 13 views
0

국가 드롭 다운 목록에서 값을 선택하면 다른 모든 드롭 다운 목록 상자의 값이 재설정되고 선택한 국가도 재설정됩니다.드롭 다운 목록에서 값을 선택할 때 다른 모든 드롭 다운 값이 재설정됩니다.

국가 드롭 다운 목록, 상태 드롭 다운 목록 및 지구 드롭 다운 목록은 종속적입니다.

private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText) 
{ 
string conString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString; 
SqlCommand cmd = new SqlCommand(query); 
using (SqlConnection con = new SqlConnection(conString)) 
{ 
using (SqlDataAdapter sda = new SqlDataAdapter()) 
{ 
      cmd.Connection = con; 
      con.Open(); 
      ddl.DataSource = cmd.ExecuteReader(); 
      ddl.DataTextField = text; 
      ddl.DataValueField = value; 
      ddl.DataBind(); 
      con.Close(); 
     } 
} 
ddl.Items.Insert(0, new ListItem(defaultText, "0")); 
} 

protected void gCountry2_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    gState2.Enabled = false; 
    gDistrict2.Enabled = false; 
    gState2.Items.Clear(); 
    gDistrict2.Items.Clear(); 
    //gState2.Items.Insert(0, new ListItem("Select State", "0")); 
    //gDistrict2.Items.Insert(0, new ListItem("Select City", "0")); 
    int countryId = int.Parse(gCountry2.SelectedItem.Value); 
    if (countryId > 0) 
    { 
     string query = string.Format("select StateId, StateName from States where CountryId = {0}", countryId); 
     BindDropDownList(gState2, query, "StateName", "StateId", "Select State"); 
     gState2.Enabled = true; 
     Page.SetFocus(f2.ClientID); 
    } 
} 
protected void gState2_SelectedIndexChanged1(object sender, EventArgs e) 
{ 
    gDistrict2.Enabled = false; 
    gDistrict2.Items.Clear(); 
    //gDistrict2.Items.Insert(0, new ListItem("Select City", "0")); 
    int stateId = int.Parse(gState2.SelectedItem.Value); 
    if (stateId > 0) 
    { 
     string query = string.Format("select CityId, CityName from Cities where StateId = {0}", stateId); 
     BindDropDownList(gDistrict2, query, "CityName", "CityId", "Select City"); 
     gDistrict2.Enabled = true; 
     Page.SetFocus(f2.ClientID);   
} 
+1

"선택한 국가도 재설정됩니다."- 국가 드롭 다운을 바인딩하는 방법을 표시하십시오. –

+0

감사합니다 알렉스 이것은 내가 페이지로드에 사용하는 코드입니다. ----------------------- if (! IsPostBack) { string query = "Select CountryId , CountryName 국가 이름 "; BindDropDownList (gCountry2, query, "CountryName", "CountryId", "Select Country"); gState2.Enabled = false; gDistrict2.Enabled = false; gState2.Items.Insert (0, 새 ListItem ("Select State", "0")); gDistrict2.Items.Insert (0, 새 ListItem ("Select City", "0")); \t} –

답변

0

원하는 작업을 훨씬 쉽게 수행 할 수 있습니다. 드롭 다운을 선언적으로 바인딩합니다. 이 같은.

<%-- .aspx file. --%> 
<%-- populate country DDL --%> 
<asp:DropDownList ID="ddCountry" runat="server" DataTextField="countryName" 
    DataValueField="countryId" 
    AppendDataBoundItems="true" DataSourceID="sqlCountry" AutoPostBack="true"> 
    <asp:ListItem Value="0" Text="- Select Country -"></asp:ListItem> 
</asp:DropDownList> 
<asp:SqlDataSource ID="sqlCountry" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>" 
    SelectCommand="select countryid,countryname from dbo.countries"> 
</asp:SqlDataSource> 

<%-- populate state DDL --%> 
<asp:DropDownList ID="ddState" runat="server" DataTextField="StateName" 
    DataValueField="stateId" 
    DataSourceID="sqlState" AutoPostBack="true" OnDataBound="ddState_DataBound"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="sqlState" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>" 
    SelectCommand="select stateId, stateName from dbo.states where [email protected]"> 
    <SelectParameters> 
     <%-- Bind parameter to the parent control --%> 
     <asp:ControlParameter DefaultValue="0" ControlID="ddCountry" PropertyName="SelectedValue" Name="countryId" Type="Int32" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

<%-- populate district DDL --%> 
<asp:DropDownList ID="ddDistrict" runat="server" DataTextField="CityName" 
    DataValueField="cityId" 
    DataSourceID="sqlDistrict" AutoPostBack="true" OnDataBound="ddDistrict_DataBound"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="sqlDistrict" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>" 
    SelectCommand="select cityId, cityName from dbo.cities where [email protected]"> 
    <SelectParameters> 
     <asp:ControlParameter DefaultValue="0" ControlID="ddState" PropertyName="SelectedValue" Name="stateId" Type="Int32" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

마지막 두 개의 드롭 다운에 "- 선택 -"항목을 추가하려면 코드가 필요합니다.

// .aspx.cs 
protected void ddState_DataBound(object sender, EventArgs e) 
{ 
    ddState.Items.Insert(0, new ListItem("--Select State--", "0")); 
} 
protected void ddDistrict_DataBound(object sender, EventArgs e) 
{ 
    ddDistrict.Items.Insert(0, new ListItem("--Select City--", "0")); 
} 
+0

감사합니다. @Alex Kudryashev 제가 이것을 시도 할 것입니다. –