c# - 当我从列表中选择某些内容时,如何使组件消失?
问题描述
我正在尝试做的事情:当我从下拉列表中选择“PRODUSE”时,会发生以下情况。“nume1”和“salariu”消失/变得无法访问。列表中的其他组件也是如此。我的if怎么了?为什么它不起作用。
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>
解决方案
您需要添加AutoPostback="true"
到您的DropDownList2
并将适当的代码添加到DropDownList2_SelectedIndexChanged
默认情况下asp:DropDownList
,更改时不回发,请参阅https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listcontrol.autopostback?view=netframework-4.8
推荐阅读
- python - Pandas:在 CSV 中查找每下 10 个数据点的平均值
- flutter - Flutter 包 widgets.dart、material.dart 和 cupertino.dart 有什么区别,使用哪一个?
- javascript - 等待设备并响应网页
- java - 如何将条目添加到 CalendarFX MonthPage?
- javascript - 更新 mobx 存储时重新渲染组件
- python - 将 Rawsql 查询数组转换为格式化的 html 表
- javascript - 练习反应式编程
- php - 从正则表达式中得到错误“在偏移量 1 处没有重复的内容”
- ios - 异常没有被捕获
- batch-file - 在 echo 命令的末尾批量附加一些东西