Search code examples
c#asp.netcode-behinddynamic-controls

How do I wire up a client Javascript function call when creating a new CheckBox in my codebehind?


In my ASP.NET page's code behind I'm creating a checkbox on the fly:

CheckBox cb = new CheckBox();

But I want the new checkbox to call a client function instead of posting back. I would expect to be able to do something like this:

cb.OnClientClick = "Javascript:MyClientFunction();";

But that doesn't exist. How do I wire it up on the fly like that?


Solution

  • This will work:

    cb.Attributes.Add("onclick", "Javascript:MyClientFunction();");