locked
if not value is selected hide enter button RRS feed

  • 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();  
      
    }  

    I cant figure out how to hide the enter button only if a value isnt selected from droplist2?

     

    Monday, September 28, 2020 9:27 PM

Answers

  • User1535942433 posted

    Hi MOHIIMRAN,

    I cant figure out how to hide the enter button only if a value isn't selected from droplist2?

    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:

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 29, 2020 2:28 AM