我有GridView,文本框,一個HTML按鈕。它所做的是文本框包含一個值,點擊html按鈕後將保存在數據庫中。如何在jquery ajax調用後重新加載asp.net GridView?
Here is the code for the page:
<div>
<div id="Gridview-container">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input type="button" id="btn" value="insert" />
</div>
</form>
<script type="text/javascript">
$("#btn").click(function() {
var a = $("#TextBox1").val();
$.ajax({
url: 'WebService.asmx/insert',
data: "{ 'name': '" + a + "' }",
dataType: "json",
contentType: "application/json; charset=utf-8",
type: "POST",
success: function() {
//alert('insert was performed.');
$("#Gridview-container").empty();
}
});
});
</script>
下面是代碼後面的那一頁:
public partial class GridViewCourses : System.Web.UI.Page
{
Database db = new Database();
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = db.LoadCourses();
GridView1.DataBind();
}
}
下面是Web服務的代碼:我想要的東西
public class WebService : System.Web.Services.WebService {
public WebService() {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public void insert(string name)
{
Database db = new Database();
db.Add(name);
}
}
有作爲GridView.DataBind同樣的效果(),這樣當我執行刪除和更新時,GridView將根據數據庫中的記錄重新加載。
先生/女士,你的回答會有很大的幫助。謝謝++
感謝您的回覆,我只是想知道,如果它也有可能沒有更新面板做:d –
是的,但你將不得不做一個完整的回發(我推測你不想這樣做)。 – martinwnet