javascript - onserverclick event not working using html builder in C# -


i using anchor tag , want fire event on click anchor tag.but unfortunately not working . here code :

html.append("<a id='dlttag'  class='ca_quy_e' runat='server' onserverclick='delete_click'>");             html.append("<i class='fa'>");             html.append("</i>");             html.append("</a>");  protected void delete_click(object sender, eventargs e) {       //my code } 

every thing working perfect table forming , onserverclick not working.

you have make use of javascript or jquery i.e. clientside scripting calling serverside method client side

a. create method wemethod attribute

[webmethod]     public static string isexists(string value)     {         //code check uniqe value call database check         return "true";     } 

b. register client call element

    protected void page_load(object sender, eventargs e)      {           if (!ispostback)                txtdata.attributes.add("onblur", "focuslost()");      } 

c. make use of jquery ajax

function isexists(pagepath, datastring, textboxid, errorlableid) {  $.ajax({      type:"post",      url: pagepath,      data: datastring,      contenttype:"application/json; charset=utf-8",      datatype:"json",      error:           function(xmlhttprequest, textstatus, errorthrown) {                $(errorlableid).show();                $(errorlableid).html("error");           },      success:           function(result) {                var flg = true;                if (result != null) {                     flg = result.d;                     if (flg == "true") {                          $(errorlableid).show();                     }                     else {                          $(errorlableid).hide();                     }           }      }     }); } function focuslost() {      var pagepath = window.location.pathname + "/isexists";      var datastring = "{ 'value':'" + $("#<%= txtdata.clientid%>").val() + "' }";      var textboxid = "#<%= txtdata.clientid%>";      var errorlableid = "#<%= lblerror.clientid%>";      isexists(pagepath, datastring, textboxid, errorlableid); } 

if not getting here full article : calling server side function client side script

if using scriptmanager can use execute server side code javascript using script manager


you need use _dopostback psotbacking server or make use of ajax call server side method

read: usage of dopostback in real environment

example

html.append("<a id='dlttag'  class='ca_quy_e' runat='server' onclick='dopostback()'>");             html.append("<i class='fa'>");             html.append("</i>");             html.append("</a>");  function dopostback()  {   __dopostback('deletebutton','');      } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -