我有 GridView、文本框、一个 html 按钮。它的作用是文本框包含一个值,单击 html 按钮后该值将保存在数据库中。
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();
}
}
这是网络服务的代码:
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 会根据数据库中的记录重新加载。
先生/女士,您的回答将会很有帮助。谢谢++
最佳答案
您可以使用 UpdatePanel
并将 GridView
放入其中。然后,为了从 javascript 引发回发,您必须使用 __doPostBack
并定位您的 UpdatePanel。
这里的一些信息应该可以帮助您实现这一点:http://encosia.com/easily-refresh-an-updatepanel-using-javascript/
关于asp.net - jquery ajax调用后如何重新加载asp.net GridView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12781581/