2012-01-19 141 views
4

我做了一個測試應用程序,其中控件是html5 control.I有2個文本框和一個html5按鈕。我想在html5按鈕上觸發onclick事件,並且要通過onclick事件調用的方法在服務器端。從html5按鈕onclick事件調用服務器端方法

我試過,但它不工作。任何人都可以幫助我從html5 button調用serverside方法。我發佈我的代碼。

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

請註明的答案,如果它解決了這個問題。 – Pankaj

回答

9

請使用:onserverclick="btnSave_click"

1

使用onserverclick="btnSave_click"而不是onclick="btnSave_click"

0

您可以使用下面的代碼。

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

我想你在服務器端給btnSave_Click和btnSave_click在客戶端