How to make components disappear when I select something from the list?

What I’m trying to do: When I select “PRODUSE” from the dropdownlist the following happens. “nume1” and “salariu” disappear / become inaccessible. The same goes for the other components in the list. What’s wrong with my if? Why doesn’t it work.

    if (conectare.State == ConnectionState.Open) {
        conectare.Close();
    }
    conectare.Open();
}

protected void Button1_Click(object sender, EventArgs e)//butonul de insert
{
    if (DropDownList2.SelectedValue == "1")
    {
        denumire1.Enabled = false;
        salariu.Enabled = false;

        SqlCommand cmd = conectare.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "insert into PRODUSE values(' " + denumire.Text + " ',' " + anfabricatie.Text + " ')";
        cmd.ExecuteNonQuery();

        denumire.Text = "";
        anfabricatie.Text = "";

        display();
    }
    else if (DropDownList2.SelectedValue=="2") {
        denumire.Enabled = false;
        anfabricatie.Enabled = false;
            
        SqlCommand cmd = conectare.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "insert into FUNCTII values(' " + denumire1.Text + " ',' " + salariu.Text + " ')";
        cmd.ExecuteNonQuery();

        denumire1.Text = "";
        salariu.Text="";
       
        display2();
    }
}

public void display() {// AFISAM TABELA PRODUSE
    SqlCommand cmd = conectare.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from PRODUSE";
    cmd.ExecuteNonQuery();

    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

public void display2() // afisam tabela FUNCTII
{
    SqlCommand cmd = conectare.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from FUNCTII";
    cmd.ExecuteNonQuery();

    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

protected void Button2_Click(object sender, EventArgs e)
{
    SqlCommand cmd = conectare.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "delete from PRODUSE where Denumire=' " + denumire.Text + " '";
    cmd.ExecuteNonQuery();

    denumire.Text = "";
    display();
}

protected void Button3_Click(object sender, EventArgs e)
{
    SqlCommand cmd = conectare.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "update PRODUSE set Denumire='"+denumire.Text+"',AnFabricatie='"+anfabricatie.Text+"' where IdProdus=" +Convert.ToInt32(idvechi.Text) + " ";
    cmd.ExecuteNonQuery();

    denumire.Text = "";
    anfabricatie.Text = "";

    display();
}

protected void Button4_Click(object sender, EventArgs e)
{
    display();
}

-HTML-

<table>
            <tr>
                <td>Denumire</td>
                <td><asp:TextBox ID="denumire" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>AnFabricatie</td>
                <td><asp:TextBox ID="anfabricatie" runat="server"></asp:TextBox></td>
            </tr>
             <tr>
                <td>Denumire</td>
                <td><asp:TextBox ID="denumire1" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Salariu</td>
                <td><asp:TextBox ID="salariu" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="Button1" runat="server" Text="Insert" OnClick="Button1_Click" />
                    <asp:Button ID="Button2" runat="server" Text="Delete" OnClick="Button2_Click" />
                    <asp:Button ID="Button3" runat="server" Text="Update" OnClick="Button3_Click" />
                    <asp:Button ID="Button4" runat="server" Text="View" OnClick="Button4_Click" />
                                            
                </td>
            </tr>
            <tr>
                <td>Id-ul pentru update</td>
                <td>
                    <asp:TextBox ID="idvechi" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>

        <asp:DropDownList ID="DropDownList2" runat="server" Height="16px" Width="253px" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
            <asp:ListItem ID="produse" Value="1">PRODUSE</asp:ListItem>
            <asp:ListItem ID="functii" Value="2">FUNCTII</asp:ListItem>
            <asp:ListItem ID="angajati" Value="3">ANGAJATI</asp:ListItem>
            <asp:ListItem ID="categorii" Value="4">CATEGORII_PROD</asp:ListItem>
            <asp:ListItem ID="comenzi" Value="5">COMEZNI</asp:ListItem>
            <asp:ListItem ID="clienti" Value="6">CLIENTI</asp:ListItem>
            <asp:ListItem ID="vanzari" Value="7">VANZARI</asp:ListItem>
        </asp:DropDownList>

        <br />
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>

    </div>
</form>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to add the AutoPostback="true" to your DropDownList2 and also add the appropriate code into DropDownList2_SelectedIndexChanged

By default the asp:DropDownList does not postback when changed see https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listcontrol.autopostback?view=netframework-4.8


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x