Answered by:
a check box change dropdown1 default value if a check box has been

Question
-
User1298215938 posted
<form id="form1" runat="server"> <table style="width: 100%; height: 86px;"> <tr> <td class="style1"> <asp:Label ID="Label1" runat="server" Text="Choose Your Country :"></asp:Label> </td> <td> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataTextField="country_name" DataValueField="country_id" AppendDataBoundItems="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem Value="0">--Select Country--</asp:ListItem> </asp:DropDownList> </td> <td> </td> </tr> <tr> <td class="style1"> <asp:Label ID="Label2" runat="server" Text="Choose Your State :"></asp:Label> </td> <td> <asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="true" DataTextField="state_name" DataValueField="state_id" AutoPostBack="True" onselectedindexchanged="DropDownList2_SelectedIndexChanged"> <asp:ListItem Value="0">-- Select State--</asp:ListItem> </asp:DropDownList> </td> <td> </td> </tr> <tr> <td class="style1"> <asp:Label ID="Label3" runat="server" Text="Choose Your City :"></asp:Label> </td> <td> <asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems="true" DataTextField="city_name" DataValueField="city_id"> <asp:ListItem Value="0">-- Select City--</asp:ListItem> </asp:DropDownList> </td> <td> </td> </tr> </table> <div> </div> </form>
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"); SqlCommand cmd = new SqlCommand("select * from tbl_country", con); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); DropDownList1.DataSource = dt; DropDownList1.DataBind(); } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList2.Items.Clear(); DropDownList2.Items.Add("Select State"); SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"); SqlCommand cmd = new SqlCommand("select * from tbl_state where country_id=" + DropDownList1.SelectedItem.Value, con); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); DropDownList2.DataSource= dt; DropDownList2.DataBind(); } protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { DropDownList3.Items.Clear(); DropDownList3.Items.Add("Select State"); SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"); SqlCommand cmd = new SqlCommand("select * from tbl_city where state_id=" + DropDownList2.SelectedItem.Value, con); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); DropDownList3.DataSource = dt; DropDownList3.DataBind(); }
if Checkbox checked how can i disable a item from dropdownlist one even tho its connected to SQL server?
Thursday, October 1, 2020 8:00 PM
Answers
-
User1535942433 posted
Hi MOHIIMRAN,
Accroding to your description and codes,could you tell us where is your checkbox?
As far as I think,when you check your checkbox,you could select again and not equal the checkbox's text.And then ,you could rebind the dropdown again.
More details,you could refer to below codes:
<table style="width: 100%; height: 86px;"> <tr> <td> <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" Text="China" AutoPostBack="true" /></td> <td></td> <td></td> </tr> <tr> <td class="style1"> <asp:Label ID="Label1" runat="server" Text="Choose Your Country :"></asp:Label> </td> <td> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataTextField="CountryName" DataValueField="CountryID" AppendDataBoundItems="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem Value="0">--Select Country--</asp:ListItem> </asp:DropDownList> </td> <td></td> </tr> <tr> <td class="style1"> <asp:Label ID="Label2" runat="server" Text="Choose Your State :"></asp:Label> </td> <td> <asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="true" DataTextField="StateName" DataValueField="State_Id" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"> <asp:ListItem Value="0">-- Select State--</asp:ListItem> </asp:DropDownList> </td> <td></td> </tr> <tr> <td class="style1"> <asp:Label ID="Label3" runat="server" Text="Choose Your City :"></asp:Label> </td> <td> <asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems="true" DataTextField="CityName" DataValueField="CityID"> <asp:ListItem Value="0">-- Select City--</asp:ListItem> </asp:DropDownList> </td> <td></td> </tr> <tr> <td></td> <td><asp:Button ID="Enter" runat="server" Text="Enter" /></td> <td></td> </tr> </table>
Code-behind:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string str; str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection conn = new SqlConnection(str); SqlCommand cmd = new SqlCommand("select * from Country", conn); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); DropDownList1.DataSource = dt; DropDownList1.DataBind(); conn.Close(); Enter.Visible = false; } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList2.Items.Clear(); DropDownList2.Items.Add("Select State"); string str; str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection conn = new SqlConnection(str); SqlCommand cmd = new SqlCommand("select * from State where country_id=" + DropDownList1.SelectedItem.Value, conn); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); DropDownList2.DataSource = dt; DropDownList2.DataBind(); } protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { DropDownList3.Items.Clear(); DropDownList3.Items.Add("Select State"); string str; str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection conn = new SqlConnection(str); SqlCommand cmd = new SqlCommand("select * from City where state_id=" + DropDownList2.SelectedItem.Value, conn); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); DropDownList3.DataSource = dt; DropDownList3.DataBind(); Enter.Visible = true; } protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { if (CheckBox1.Checked == true) { DropDownList1.Items.Clear(); DropDownList1.Items.Add("Select Country"); string str; str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection conn = new SqlConnection(str); string sql= "select * from Country where CountryName <>'" + CheckBox1.Text +"'"; SqlCommand cmd = new SqlCommand(sql , conn); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); DropDownList1.DataSource = dt; DropDownList1.DataBind(); } }
Result:
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 2, 2020 3:31 AM