我正在尝试根据Dropdownlist中的选定值显示某些文本框。出于某种原因,它不起作用 - 当在下拉列表中选择特定项目时,页面上没有任何输入框可见。希望得到反馈。提前谢谢。
以下是相关代码:
aspx页面:
<asp:DropDownList ID="AreaDropDownList" runat="server" OnSelectedIndexChanged="AreaDropDownList_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Rectangle</asp:ListItem>
<asp:ListItem>Triangle</asp:ListItem>
<asp:ListItem>Trapezoid</asp:ListItem>
<asp:ListItem>Circle</asp:ListItem>
<asp:ListItem>Sector Of Circle</asp:ListItem>
<asp:ListItem>Ellipse</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="InputTextBox1" runat="server" AutoPostBack="True" Visible="TRUE"></asp:TextBox>
<asp:TextBox ID="InputTextBox2" runat="server" AutoPostBack="True" Visible="TRUE"></asp:TextBox>
<asp:TextBox ID="InputTextBox3" runat="server" AutoPostBack="True" Visible="TRUE"></asp:TextBox>
aspx.cs页面:
protected void AreaDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
if (AreaDropDownList.SelectedValue == "")
{
InputTextBox1.Visible = false;
InputTextBox2.Visible = false;
InputTextBox3.Visible = false;
}
else if (AreaDropDownList.SelectedValue == "Rectangle")
{
InputTextBox1.Visible = true;
InputTextBox2.Visible = true;
InputTextBox3.Visible = false;
}
else if (AreaDropDownList.SelectedValue == "Triangle")
{
InputTextBox1.Visible = true;
InputTextBox2.Visible = true;
InputTextBox3.Visible = false;
}
else if (AreaDropDownList.SelectedValue == "Trapezoid")
{
InputTextBox1.Visible = true;
InputTextBox2.Visible = true;
InputTextBox3.Visible = true;
}
else if (AreaDropDownList.SelectedValue == "Circle")
{
InputTextBox1.Visible = true;
InputTextBox2.Visible = false;
InputTextBox3.Visible = false;
}
else if (AreaDropDownList.SelectedValue == "Sector")
{
InputTextBox1.Visible = true;
InputTextBox2.Visible = true;
InputTextBox3.Visible = false;
}
else if (AreaDropDownList.SelectedValue == "Ellipse")
{
InputTextBox1.Visible = true;
InputTextBox2.Visible = true;
InputTextBox3.Visible = false;
}
}
答案 0 :(得分:1)
也许您需要使用.Text
属性
而不是.SelectedValue
在If语句中。
if (AreaDropDownList.Text == "")
{
InputTextBox1.Visible = false;
InputTextBox2.Visible = false;
InputTextBox3.Visible = false;
}
答案 1 :(得分:1)
在M. Ayoub的帮助下,我能够通过稍微改变代码逻辑来解决问题。以下代码放在Page_Load事件函数中,现在我可以控制哪个文本框可见:
if ((RadioButtonList1.SelectedValue != "Area Calculator") && (RadioButtonList1.SelectedValue != "Trigonometric Calculator")
&& (RadioButtonList1.SelectedValue != "Area Calculator"))
{
arithmetic.Visible = false;
trig.Visible = false;
CalcArea.Visible = false;
}
基于M. Ayoub的反馈做出了另外两个更改:第一个是使用SelectedItem.Text而不是SelectedValue作为下拉列表。第二个是在AreaDropDownList_SelectedIndexChanged中有一个else块。代码如下:
protected void AreaDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
if (AreaDropDownList.SelectedItem.Text == "")
{
inputbox1.Visible = false;
inputbox2.Visible = false;
inputbox3.Visible = false;
}
else if (AreaDropDownList.SelectedItem.Text == "Rectangle")
{
inputbox1.Visible = true;
inputbox2.Visible = true;
inputbox3.Visible = false;
}
else if (AreaDropDownList.SelectedItem.Text == "Triangle")
{
inputbox1.Visible = true;
inputbox2.Visible = true;
inputbox3.Visible = false;
}
else if (AreaDropDownList.SelectedItem.Text == "Trapezoid")
{
inputbox1.Visible = true;
inputbox2.Visible = true;
inputbox3.Visible = true;
}
else if (AreaDropDownList.SelectedItem.Text == "Circle")
{
inputbox1.Visible = true;
inputbox2.Visible = false;
inputbox3.Visible = false;
}
else if (AreaDropDownList.SelectedItem.Text == "Sector")
{
inputbox1.Visible = true;
inputbox2.Visible = true;
inputbox3.Visible = false;
}
else if (AreaDropDownList.SelectedItem.Text == "Ellipse")
{
inputbox1.Visible = true;
inputbox2.Visible = true;
inputbox3.Visible = false;
}
else
{
inputbox1.Visible = true;
inputbox2.Visible = true;
inputbox3.Visible = true;
}