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;
}
Result:
Best regards,
Yijing Sun
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
26 Points
49 Posts
if not value is selected hide enter button
Sep 28, 2020 09:27 PM|MOHIIMRAN|LINK
I cant figure out how to hide the enter button only if a value isnt selected from droplist2?
Contributor
3940 Points
1550 Posts
Re: if not value is selected hide enter button
Sep 29, 2020 02:28 AM|yij sun|LINK
Hi MOHIIMRAN,
Accroding to your description and codes,could you tell us what is the enter button?Your codes isn't including about the enter button.
Then I think,your logic is wrong.Only when you select the dropdownlist,it will excute the selectIndexchanged event.
I suggest you could hidden the enter button on the page load.And then,when you select the dropdownlist2,the enter button will be displayed.
More details,you could refer to below codes:
<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="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> <div> </div>
Code-Behind:
Result:
Best regards,
Yijing Sun