我正在尝试对DropDownList
个OnSelectedIndexChanged
事件进行编码,但我无法使其发挥作用。
所以这是.aspx
上的代码:
<asp:DropDownList ID="DDLSample" OnSelectedIndexChanged="DDLSample_SelectedIndexChanged" runat="server">
<asp:ListItem Text="Sample1" Value="0"></asp:ListItem>
<asp:ListItem Text="Sample2" Value="1"></asp:ListItem>
<asp:ListItem Text="Others..." Value="2"></asp:ListItem>
</asp:DropDownList>
和TextBox
:
<asp:TextBox ID="txtOthers" runat="server" Visible ="false" CssClass="form-control" ></asp:TextBox>
我打算做的是,当从Others...
中选择DropDownList
时,它会显示Others
字段。
在我的aspx.cs
上,我有这段代码
protected void Page_Load(object sender, EventArgs e)
{
DDLSample.SelectedIndexChanged += new EventHandler(DDLSample_SelectedIndexChanged);
DDLSample.AutoPostBack = true;
}
void DDLSample_SelectedIndexChanged(object sender, EventArgs e)
{
if (DDLFindings.SelectedValue.ToString() == "2")
txtOthers.Visible = true;
else
txtOthers.Visible = false;
}
但是,我仍然遇到这个错误:
CS1061:&#39; sample_aspx&#39;不包含的定义 &#39; DDLSample_SelectedIndexChanged&#39;没有扩展方法 &#39; DDLSample_SelectedIndexChanged&#39;接受第一个类型的参数 &#39; sample_aspx&#39;可以找到(你是否错过了使用指令或 装配参考?)
答案 0 :(得分:1)
DDLSample_SelectedIndexChanged
是私有的,aspx
无法访问私有方法。您可以从OnSelectedIndexChanged="DDLSample_SelectedIndexChanged"
删除DropDownList
,因为您已经拥有:{/ p>
DDLSample.SelectedIndexChanged += new EventHandler(DDLSample_SelectedIndexChanged);
或者DDLSample_SelectedIndexChanged
受保护:
protected void DDLSample_SelectedIndexChanged(object sender, EventArgs e)
{
//Your code
}