Javascript 在不使用任何单击按钮更改文本框值时调用c#方法
我想调用从数据库加载数据的c#方法“test()”。我有两个文本框,当我写东西的时候,应该会出现一个gridview(这就是test()应该做的)…我正在使用jQuery在文本框的每次更改中调用test(),但没有使用任何按钮,但无法运行..我感谢任何帮助:) 试试这个Javascript 在不使用任何单击按钮更改文本框值时调用c#方法,javascript,c#,jquery,Javascript,C#,Jquery,我想调用从数据库加载数据的c#方法“test()”。我有两个文本框,当我写东西的时候,应该会出现一个gridview(这就是test()应该做的)…我正在使用jQuery在文本框的每次更改中调用test(),但没有使用任何按钮,但无法运行..我感谢任何帮助:) 试试这个 $("input").keyup(function () { alert("dsfd"); $.ajax({ type: "POST", url:
$("input").keyup(function () {
alert("dsfd");
$.ajax({
type: "POST",
url: "FirstPage.aspx/test",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#Content").text(response.d);
},
failure: function (response) {
alert(response.d);
}
});
});
test()是否运行?(如果你设置一个断点)@ParkashKumar上次我使用它时,每次按键都会导致回发。有什么变化吗?会的,因为每次按键都会调用listener。@nAviD您很好地描述了您的目标和尝试。但你根本没有描述失败。如果我们不知道问题是什么,我们将无法帮助您。它不会运行我的函数测试()…它只显示警报窗口
<asp:TextBox ID="TextBox1" runat="server" Width="140px" Height="16px" CssClass="calculate" ></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Width="140px" Height="16px" CssClass="calculate" ></asp:TextBox>
protected void test(object sender, EventArgs e)
{
load();
}
protected string load()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=ic-webdev;Initial Catalog=intracall;User ID=sa;Password=xs4intracall";
con.Open();
SqlDataAdapter ada = new SqlDataAdapter("SELECT COUNT(*) as Count where Firstname='"+TextBox1+"' and Lastname='"+TextBox2+"' FROM contact", con);
DataTable dt = new DataTable();
ada.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
$("input").keyup(function () {
alert("dsfd");
$.ajax({
type: "POST",
url: "FirstPage.aspx/test",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#Content").text(response.d);
},
failure: function (response) {
alert(response.d);
}
});
});