编译器错误消息:CS1061 ASPX

时间:2015-12-26 10:13:29

标签: c# asp.net

我正在尝试对DropDownListOnSelectedIndexChanged事件进行编码,但我无法使其发挥作用。

所以这是.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;可以找到(你是否错过了使用指令或   装配参考?)

1 个答案:

答案 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
}