1

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

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

4

1 回答 1

1

You could have the following asp textbox:

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

Then, the following javascript function, using jQuery to do your Ajax call:

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;
            })
        });
}

And, on server-side, on Page Load event, do the following (my sample is in 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

Note: I didn't test all of this, but it should be very close to what you need.

于 2013-03-10T18:27:22.133 回答