Saturday, 15 June 2013

c# - How Do I Add Event Handlers to .NET Buttons Programmatically? -



c# - How Do I Add Event Handlers to .NET Buttons Programmatically? -

i'm trying employ strongest oop can muster in developing web application, i'm having issues adding event handlers objects create them using code. i'm sure it's simple solution maintain passing up, i'm @ loss seek next. below test code i've been playing with, trying button press go something.

(imagine there's break point on line "int i;")

button b = new button(); b.text = "do something"; b.attributes.add("runat", "server"); b.attributes.add("onclick", "click"); form1.controls.add(b); private void click(object sender, eventargs e) { int i; }

since new button created page_load, can't hardcode xhtml. debugging never hits breakpoint. haven't had more success checkboxes either.

you have subscribe click event:

button b = new button(); b.text = "do something"; b.click += click; form1.controls.add(b); private void click(object sender, eventargs e) { int i; }

by adding onclick attribute button's attributes collection, rendered attribute on html input tag. in case utilize execute javascript code on client side.

b.attributes.add("onclick", "alert('hey')"); //will render button <input type="submit" name="x" value="do something" onclick="alert('hey')">

c# asp.net .net event-handling

No comments:

Post a Comment