Vivek Thakur

Chaotically Complex

  Home  |   Contact  |   Syndication    |   Login
  102 Posts | 1 Stories | 317 Comments | 66 Trackbacks

News



Archives

ASP.NET Ventures

In ASP.NET, when we try to add client side script on events such as onclick and onload to controls with runat="server", we will get this error:

Error 1 Too many characters in character literal 

For example, see this code:

<form id="form1" runat="server" onload="alert('Will not work!')" >

Compiling it will result in the error above. As another example, see the HTML code below:

<asp:Button ID="Button1" runat="server" Text="Button" onclick="myClientSideMethod()" />

If we try to compile it, we will get this error:

'ASP.new_aspx' does not contain a definition for 'myClientSideMethod' 

Now why are we getting such errors? Does this mean we can't add client side code to these server controls? Answer is somewhat simple: notice that this error will only come if the control has a runat="server" attribute, which implies that events such as onclick and onload will correspond to server events instead of any client side code. That's why in ASP.NET 2.0 we have a OnClientClick method for the Button control. That means if we have a control with no runat="server" like:

<input type="button" ID="Button1" value="Button" onclick="myClientSideMethod()" />

then we will not get any such error since onclick event this time would correspond to the client side script only.

Another way to wire client side script to server controls is to add attributes via code, for example:

protected void Page_Load(object sender, EventArgs e)
{
         Button1.Attributes.Add(
"onclick", "alert('Hi');");
}

This would work as expected. As for the <form> tag's onload event, its better to remove it and place onload in the <body> tag instead.

 

posted on Thursday, November 23, 2006 11:54 PM

Feedback

# re: Too many characters in character literal error while adding client side events 11/25/2006 11:44 AM Mohamed Ahmed Meligy
One small note:
Once the event name is not used by the server. you can use it in the markup, otherwise modify the "Attributes" as you show.
For any html server controls (the ones that don't start with "asp:" or such prefix), if there's click event it's called ServerClick, so, you can use onclick, clearly you show that this doesn't apply to other events like load.
BTW, the "input" server html control, there's no "Text" property. User "Value" instead (and type it lowercased in the markup for XHTML compliance).

# re: Too many characters in character literal error while adding client side events 8/22/2007 8:43 PM Willliam
I was working on web page when I received the compile error "Too many characters in character literal". Thanks for your explanation.

I changed my parameter from "OnClick" to "OnClientClick" leaving runat="server" in place and everything compiled and worked.

# re: Too many characters in character literal error while adding client side events 5/22/2008 9:26 PM Rick
Thank you very much! Every tutorial I saw was telling me to put this in the OnClick attribute. I never even noticed the OnClientClick attribute below it.

# re: Too many characters in character literal error while adding client side events 8/9/2008 8:03 PM The E
I believe you have one double quote too many. If you are using multiple quotes, you may have too many.

+ "'LITERAL'" +

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 6 and 4 and type the answer here: