2015-11-20 83 views
0

我製作了一個表格,其中一列從數據庫和其他列獲取值包含每個記錄的按鈕。無法獲得運行時響應

<table style="width: 44%;"> 
    <thead> 
     <tr> 
      <td> 
       <b>User Name</b> 
      </td> 
      <td> 
       <b>Approval</b> 
      </td> 
     </tr> 
    </thead> 
    <tbody> 
     <% for (i = 0; i < admin_dd.Rows.Count; i++) 
      {%> 
     <tr id="raw_<%=i %>"> 
      <td> 
       <%=admin_dd.Rows[i][0]%> 
      </td> 
      <td> 
       <% pos = Int32.Parse(admin_dd.Rows[i][1].ToString()); 

       %> 
       <input type="button" onclick="" value="button" onclick="approve_func(pos)"/> 

      </td> 
     </tr> 
     <% } %> 

    </tbody> 
</table> 

我想在這裏是什麼,我想送「POS」的按鈕處理程序方法的價值,並進行更改到數據庫,但我沒有這樣做

protected void approve_func(int id) 
    { 

      DatabseClass.SetData("update tblUser SET Status=1 where ID=" + id); 
      Response.Redirect(Request.RawUrl); 

     } 

好心幫我解決這個問題。

+0

你是否收到異常? –

+0

如果asp.net有一些叫grid的東西,那爲什麼這個亂碼呢? – Imad

+0

沒有得到任何異常,但數據庫沒有更新。 –

回答

0

您必須聲明你的方法作爲WebMethod在C#:

public static void approve_func(int id) 
{ 
    DatabseClass.SetData("update tblUser SET Status=1 where ID=" + id); 
} 

,並從客戶端Ajax調用:

function update(id) {  
     $.ajax({ 
     type: "POST", 
     url: 'yourpagename.aspx/approve_func',  //add your page name 
     data: "{id:id}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function() { 
      alert('success'); 
     }, 
     error: function (e) { 
      alert('an error occurred'); 
     } 
    }); 
} 

但我會建議你使用Gridview代替這種混亂的方法。你可以簡單地谷歌如何使用gridview,你會得到很多有用的鏈接。

+0

好的時候,這段代碼沒有任何改變謝謝...它工作正常:) –

+0

@shifafatima如果它對你有幫助,你可以接受它作爲答案。它也會告訴其他觀衆已經回答了這個問題。 – Shaharyar

+0

是的,它解決了我的問題,但我不得不做一個小小的改變。 –