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.
As user user1090190 wrote in a comment,
onclick(generated fromOnClientClick) is executed in the web-browser as JavaScript.The
+=in this case is callingToString()on the newEventHandlerobject that is created which results in the string"System.EventHandler". (The+=expression expands tocmd1.OnClientClick = cmd1.OnClientClick + (new EventHandler(cmd1_Click));and the implicit conversion happens becauseOnClientClickis typed as a string.)It should likely be (note no
Client):Or, more simply (don’t ask me why the auto-complete is always “wrapped”):
Make sure this handler is setup each postback, as appropriate.
Happy coding.