Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call serverside method from html5 button onclick event

I made a test application in which controls are html5 control.I have 2 textbox and a button of html5. I want to fire onclick event on html5 button and the method which is to be called through onclick event is on server side.

I tried but it is not working.Can anyone help me to call the serverside method from a 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="[A-z]" />
        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();
}
like image 709
Swati Avatar asked Dec 27 '22 07:12

Swati


1 Answers

Please use : onserverclick="btnSave_click"

like image 193
Saurabh Avatar answered Jan 30 '23 11:01

Saurabh