2013-03-10 59 views
0

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

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

绝对,AJAX是你的出路 – Rab 2013-03-10 17:30:33

+0

如何在文本框上使用按键事件? – Anand 2013-03-10 17:37:10

+0

你需要谷歌有点为它 – Rab 2013-03-10 17:40:06

回答

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

而且,在服务器端,在页面加载前夕NT,请执行下列操作(我的样本是在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 

注:我没有测试了这一切,但它应该是非常接近你所需要的。

+0

谢谢你马特罗伊 – Anand 2013-07-26 10:43:28

相关问题