Joe Tyman
Joe Tyman

Reputation: 1417

"System is undefined" JavaScript error when using click handler?

I have a gridview with a button in one of the columns. I add columns to the gridview with the code behind. I used this line to attach my click event:

cmd1.OnClientClick += new EventHandler(cmd1_Click);

Now every time I click that button, it gives me a run time error and breaks at System.EventHandler of <input type="submit" name="grvList$ctl02$ctl05" value="Add" onclick="System.EventHandler;" />

Has anyone ran into this problem.

Upvotes: 1

Views: 1184

Answers (1)

user166390
user166390

Reputation:

As user user1090190 wrote in a comment, onclick (generated from OnClientClick) is executed in the web-browser as JavaScript.

The += in this case is calling ToString() on the new EventHandler object that is created which results in the string "System.EventHandler". (The += expression expands to cmd1.OnClientClick = cmd1.OnClientClick + (new EventHandler(cmd1_Click)); and the implicit conversion happens because OnClientClick is typed as a string.)

It should likely be (note no Client):

cmd1.Click += new EventHandler(cmd1_Click)

Or, more simply (don't ask me why the auto-complete is always "wrapped"):

cmd1.Click += cmd1_Click;

Make sure this handler is setup each postback, as appropriate.

Happy coding.

Upvotes: 4

Related Questions