【问题标题】:key press event in asp.net textboxasp.net 文本框中的按键事件
【发布时间】:2013-03-11 02:36:32
【问题描述】:

我需要将文本框中的输入作为数值。 我需要从输入的文本框和从数据库值中检索的数据做产品。 如何在文本框上进行按键事件?

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 而不会闪烁

【问题讨论】:

  • 当然,AJAX 是你的出路
  • 如何在文本框上使用按键事件?
  • 你需要用谷歌搜索一下

标签: asp.net ajax


【解决方案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;
            })
        });
}

并且,在服务器端,在页面加载事件中,执行以下操作(我的示例在 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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 2021-11-03
    相关资源
    最近更新 更多