Call serverside method from html5 onclick event button

I made a test application in which the controls are html5 control.I have 2 text fields and an html5 button. I want to fire the onclick event on the html5 button, and the method that should be called via the onclick event is on the server side.

I tried, but it does not work. Can someone help me call the serveride method from the html5 button. I am posting my code.

//clientside code <form id="form1" runat="server"> <div> First name:<input type="search" name="searchfield" placeholder="enter your name" autofocus="on" required="required" pattern="[Az]" /> E-mail: <input type="email" name="emailfield" placeholder="enter emailid" /> <button onclick="btnSave_click" value="Click to Save" id="btnSave" runat="server" autofocus="autofocus" formtarget="_parent"> Click to Save</button> </div> </form> // server side code protected void btnSave_Click(object sender, EventArgs e) { string name = Request.Form["searchfield"]; string emailid = Request.Form["emailfield"]; string dob = Request.Form["bday"]; SqlConnection cn = new SqlConnection(); cn.ConnectionString = "Data Source=CP1106\\SQLEXPRESS;Initial Catalog=Testdb;User ID=sa;Password=pwd"; cn.Open(); string query = "INSERT INTO TestTable(name, email) VALUES(@name, @email)"; SqlCommand cmd = new SqlCommand(query,cn); cmd.Parameters.Add("@name", name); cmd.Parameters.Add("@email", emailid); // cmd.Parameters.Add("@bday", dob); cmd.ExecuteNonQuery(); cn.Close(); BindGrid(); } 
+4
source share
4 answers

Please use: onserverclick="btnSave_click"

+9
source

Use onserverclick="btnSave_click" instead of onclick="btnSave_click" .

+1
source

You can use the code below.

 <button onserverclick="btnSave_click" value="Click to Save" id="btnSave" runat="server" autofocus="autofocus" formtarget="_parent">Click to Save </button> 
0
source

I think you gave btnSave_Click on the server side and btnSave_click on the client side

0
source

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


All Articles