Too many characters in character literal?

2019-05-03 09:58发布

问题:

Can you tell me please what is wrong with this code??? About to getting crazy!!!

<asp:LinkButton ID="LinkButton1" OnClick="DivAc('griddiv')" Font-Size="Smaller"  runat="server" CommandName='<%# Eval("harf").ToString().ToUpper()%>'><%# Eval("harf").ToString().ToUpper() %></asp:LinkButton>

Error: Too many characters in character literal... :(

回答1:

Is DivAc('griddiv') a javascript function? Then you have to use OnClientClick instead of OnClick.

OnClick is reserved for .NET functions. With OnClientClick you generates the OnClick-attribute in HTML.

This is probably a bit confusing.

So this is what you have to do:

<asp:LinkButton ID="LinkButton1" OnClientClick="DivAc('griddiv')" Font-Size="Smaller"  runat="server" CommandName='<%# Eval("harf").ToString().ToUpper()%>'><%# Eval("harf").ToString().ToUpper() %></asp:LinkButton>


回答2:

The immediate issue is that you placed a string (griddiv) in character quotes (a single quote, in C#, is for a single character only). You would need to write something like OnClick="DivAc(\"griddiv\")"

BUT

OnClick is a server-side event handler that takes the name of a public or protected function that takes (object,EventArgs) and returns void. So this won't compile anyway.

Where is DivAc? In JavaScript? If so, you want OnClientClick, in which case you can leave the single and double quotes as they are.



回答3:

I think that your error is here:

CommandName='<%# Eval("harf").ToString().ToUpper()%>'><%# Eval("harf").ToString().ToUpper() %></asp:LinkButton>

I think that its should be:

CommandName='<%# Eval("harf").ToString().ToUpper()%'></asp:LinkButton>