asp.net - Code behind button only posting back, not calling javascript function -


i building page code behind. add button , set attributes this:

var button1 = new button(); button1.text = "purge course comments"; button1.attributes.add("onclientclick", "javascript: purge();"); pagebuttons.controls.add(button1); 

the button being added div called "pagebuttons". when test program, button appears on screen normally, when click it, nothing happens other browser posting back.

this page must built code behind, can't add button directly page, (which work):

<asp:button id="button1" runat="server" text="close window" onclientclick="purge;"/> 

basically, need able call function code behind, i'm not having luck. can tell me i'm doing wrong?

edit: i've looked @ similar questions on stackoverflow, can't find single 1 has answer works me in situation.

edit: clarity, here's purge function i've defined on page:

function purgecourse() {         strcourseid = getquerystring("id");         if (strcourseid == "") {             strcourseid = getquerystring("courseid");         }         window.open("purge.aspx?id=" + strcourseid);         parent.window.close();     } 

if add button manually page, in example above, works. however, can't seem call code behind.

edit: tried suggestion below , code looks this:

var button1 = new button(); button1.text = "purge course comments"; button1.onclientclick = "javascript: purge();"; pagebuttons.controls.add(button1); 

unfortunately, didn't work either. reiterate, though, function work if manually add button page , set onclientclick attribute call purge(). seems can't function code behind, frustrating because can't see why. small test page function , div , code behind. can't see problem is.

edit: tried thing, see if work. did following:

button1.onclientclick = "javascript: parent.window.close();"; 

and worked, button can @ least use inline javascript code. it's not "seeing" function that's on page code behind.

don't add onclientclick attribute output. property of button class, on server side, do

button1.onclientclick = "javascript: purgecourse();";  <asp:button runat="server" onclientclick="javascript: purgecourse();" /> 

on client side, rendered onclick attribute.

<button onclick="javascript: purgecourse();" /> 

usually stop processing of events, known event.preventdefault() (microsoft didn't like/get until recently), or return false. function either returns false,

button1.onclientclick = "javascript: purgecourse(); return false;"; 

or return value of function, can decide if submission terminated or continued:

button1.onclientclick = "javascript: return purgecourse();";  <script type="text/javascript"> function purgecourse () {    return false; // or return true; } </script> 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -