Swati
Swati

Reputation: 105

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();
}

Upvotes: 4

Views: 10478

Answers (4)

sindhu jampani
sindhu jampani

Reputation: 524

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

Upvotes: 0

Pankaj
Pankaj

Reputation: 10105

You can use below code.

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

Upvotes: 0

Bibhu
Bibhu

Reputation: 4081

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

Upvotes: 1

Saurabh
Saurabh

Reputation: 5727

Please use : onserverclick="btnSave_click"

Upvotes: 9

Related Questions