我有 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 会根据数据库中的记录重新加载。
先生/女士,您的回答会很有帮助。谢谢++