我有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