asp.net错误为“字符文字中的字符过多”

时间:2012-03-01 12:02:59

标签: javascript asp.net

我在DropDownList的索引更改上调用javascript,我按照以下方式执行

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
OnSelectedIndexChanged="toggleVisibility('Button1');">

我在编译期间遇到上述代码行

的错误
Too many characters in character literal

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

OnSelectedIndexChanged是服务器端事件,您需要设置OnClientClick并设置AutoPostBack =“false”如果您打算在客户端切换可见性

对于服务器端

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
OnSelectedIndexChanged="toggleVisibility">

protected object toggleVisibility(object sender, EventArgs e)
{
   Button1.Visible = !Button1.Visible;
}

因此,对于事件,您需要使用具有特定签名的方法名称,该名称由事件类型定义。这是object sender, EventArgs e

修改

DropDownList没有名为OnClientClick的属性,因此您需要在codebehind中添加它

DropDownList1.Attributes.Add("onchange","toggleVisibility('Button1');");