How to call a C # button click method from javascript?

I am trying to call a server side button click method from a javascript function, but it does not work, can I know where I made a mistake?

aspx code:

<asp:Button ID="ButtonFns" Name="ButtonFns" runat="server" Text="Finish" OnClientClick ="CountDownTick()" class="finish" onclick="ButtonFns_Click" /> 

Javascript Code:

 function CountDownTick() { if (_currentSeconds <= 0) { document.getElementById('<%= ButtonFns.ClientID %>').click(); return; } SetCountdownText(_currentSeconds-1); window.setTimeout("CountDownTick()", 1000); } 

C # Button:

 protected void ButtonFns_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Examination.mdf;Integrated Security=True;User Instance=True"); try { con.Open(); string snm; string Em; string Sx; string uname = Session["status"].ToString(); string qry = "SELECT SName,Email,Sex FROM Students WHERE Uname=@uname "; SqlCommand cm = new SqlCommand(qry, con); cm.Parameters.AddWithValue("@uname", uname); SqlDataReader reader = cm.ExecuteReader(); while (reader.Read()) { snm = reader["SName"].ToString(); Em = reader["Email"].ToString(); Sx = reader["Sex"].ToString(); if (snm != null && Em != null && Sx != null) { Session["snm"] = snm.ToString(); Session["em"] = Em.ToString(); Session["sx"] = Sx.ToString(); Session["uname"] = uname; Session["RAns"] = LabelRitAns.Text; Session["Tatt"] = LabelTotAtt.Text; Server.Transfer("YourScore.aspx"); break; } } } catch (Exception emsg) { LabelErr.Text= emsg.Message.ToString(); } finally { con.Close(); } } 

Any help would be appreciated.

+4
source share
5 answers

Use the __doPostBack method for JavaScript. If you pass the button identifier and the event name, it essentially performs the same postback on the server as if the user clicked the button.

ASP.NET postback using JavaScript (this uses VB code, but otherwise does what you need, I think)

EDIT: just to clarify, instead of calling the "click" method, use __doPostBack instead.

EDITx2: Also make sure that the return value of OnClientClick is true. If this is not the case, I think its equivalent says that it is invalid and should not do the postback.

+5
source
 ButtonFns.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(ButtonFns, "Your postback argument")); 

GetPostBackEventReference allows you to get this piece of JavaScript so that you can initiate this feedback from other sources.

This article will help you solve this problem.

How to call Postback from Javascript

+2
source

You can make a POST request from your client side, first make a click event using the method:

 [WebMethods] public static void ClickEvent() { //do you stuff } 

and now make an ajax call from your client side so that your webpage name is Default.aspx:

 $.ajax({ type: "POST", url: "Default.aspx/ClickEvent", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { // Do something interesting here. } }); 
+1
source

In your case, you can do it like this,

  <script type="text/javascript"> function ImitatePressButton() { __doPostBack('ButtonFns', ''); } </script> 

good luck.

+1
source

If you want to call a button from javascript, here is another way.

  var test = document.getElementbyId("YourButtonName"); test.onclick = function() { //your code 
0
source

Source: https://habr.com/ru/post/1411104/


All Articles