Katherine
Katherine

Reputation: 319

Compiler Error Message: CS1061 ASPX

I am trying to code a DropDownList that will have an OnSelectedIndexChanged event but I can't make it work.

So here's the code on my .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> 

And a TextBox:

<asp:TextBox  ID="txtOthers" runat="server" Visible ="false" CssClass="form-control" ></asp:TextBox>

What I plan to do is that when Others... is selected from the DropDownList, it will show the Others field.

on my aspx.cs, I have this code

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;
}

But still, I keep getting this error:

CS1061: 'sample_aspx' does not contain a definition for 'DDLSample_SelectedIndexChanged' and no extension method 'DDLSample_SelectedIndexChanged' accepting a first argument of type 'sample_aspx' could be found (are you missing a using directive or an assembly reference?)

Upvotes: 0

Views: 2574

Answers (1)

Salah Akbari
Salah Akbari

Reputation: 39976

The DDLSample_SelectedIndexChanged is private and your aspx cannot access the private methods. You can either remove OnSelectedIndexChanged="DDLSample_SelectedIndexChanged" from your DropDownList because you already have:

DDLSample.SelectedIndexChanged += new EventHandler(DDLSample_SelectedIndexChanged);

Or make DDLSample_SelectedIndexChanged protected:

protected void DDLSample_SelectedIndexChanged(object sender, EventArgs e)
{
    //Your code
}

Upvotes: 1

Related Questions