This Way:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCountries_OnSelectedIndexChanged">
<asp:ListItem Text="Please Selecte" />
<asp:ListItem Text="C1" Value="0" />
<asp:ListItem Text="C2" Value="1" />
<asp:ListItem Text="C3" Value="2" />
</asp:DropDownList>
<br />
<asp:DropDownList ID="ddlStates" runat="server">
<asp:ListItem Text="Please Selecte" />
<asp:ListItem Text="S1" Value="0" />
<asp:ListItem Text="S2" Value="1" />
<asp:ListItem Text=" S3 " Value="2" />
</asp:DropDownList>
<asp:Button ID="btnSave" Text="Save" runat="server" OnClick="Save" />
</div>
</form>
C#:
protected void Save(object sender, EventArgs e)
{
if (this.ddlStates.Enabled == true)
{
string constr = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO CountryState VALUES(@CountryId,@StateId)", con))
{
cmd.Parameters.AddWithValue("@CountryId", this.ddlCountries.SelectedItem.Value);
cmd.Parameters.AddWithValue("@StateId", this.ddlCountries.SelectedItem.Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
protected void ddlCountries_OnSelectedIndexChanged(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Count(*) FROM CountryState Where CountryId = @CountryId", con))
{
cmd.Parameters.AddWithValue("@CountryId", this.ddlCountries.SelectedItem.Value);
con.Open();
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count == 0)
{
this.ddlStates.Enabled = true;
}
else
{
this.ddlStates.Enabled = false;
}
}
}
}
SQL:
CREATE TABLE [dbo].[CountryState](
[CountryId] [int] NOT NULL,
[StateId] [int] NOT NULL
) ON [PRIMARY]