asp.net文本框中的按键事件

时间:2013-03-10 17:28:15

标签: asp.net ajax

我需要从文本框输入数字值。 我需要从输入的文本框中生成产品,并从数据库值中检索数据。 如何在文本框上执行按键事件?

SqlConnection objcon = new SqlConnection(conn);
objcon.Open();
string SQL = "select * from Price";
SqlCommand objCommand = new SqlCommand(SQL, objcon);
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet DS = new DataSet();
adapter.SelectCommand = objCommand;
adapter.Fill(DS);
DataTable dt = DS.Tables[0];
DataView dvWindows = new DataView(dt);
dvWindows.RowFilter = "Description = 'Windows'";
foreach (DataRowView rowView in dvWindows)
{
    DataRow row = rowView.Row;
    decimal d =Convert.ToInt32(txtncores.Text) * (decimal)row["CoreCost"];// txtncores is textbox
    txtWindows.Text = d.ToString();// result value to populate in text box
}

帮助我,我是否需要在同一页面上使用Ajax进行部分更新而不会出现闪烁

1 个答案:

答案 0 :(得分:1)

您可以使用以下asp文本框:

<asp:textbox id="txtncores" onkeyup="MyFunction(this);" />

然后,以下javascript函数,使用jQuery进行Ajax调用:

function MyFunction(elem)
{
    $.ajax({
        cache: false,
        type: "POST",
        url: "MyPage.aspx",
        data: "UpdatetxtWindows=" + elem.value ,
        dataType: "html",
        success: (function(data){
                document.getElementByID("txtWindows").value = data;
            })
        });
}

并且,在服务器端,在Page Load事件上,执行以下操作(我的示例在VB.net中):

If Request.Params("UpdatetxtWindows") <> "" Then
    'Use Request.Params("UpdatetxtWindows") to do your product as in:
    d = Convert.ToInt32(Request.Params("UpdatetxtWindows")) * (decimal)row["CoreCost"]
    'Return the result to the ajax success event
    Response.Write(d.ToString())  ' d is the decimal result as in your question.
End If

注意:我没有测试所有这些,但它应该非常接近你所需要的。

相关问题