此表单上有50个文本框。它们都是用于在我们的网站上输入每件商品的数量。像这样:
我的意图是在VB CodeBehind中使用TextChanged
事件或OnBlur
事件来运行并制表值,并保持成本的总计。
我使用的是<asp:textbox...>
格式,而不是<input type="text"...>
格式。
我希望它能够添加一个指向运行计算的子的事件处理程序,但似乎我无法确定正确的代码。
到目前为止,具体过程是这样的:
*我只是暂时这样做了。最终它会在数据库表中找到这些价格,但我还没有设置它。
我想使用相同的循环来识别文本框并为每个文本框创建一个事件处理程序,所有这些都指向同一个子文件。它比仅仅向一个事件处理程序添加“句柄”子句列表更容易。到目前为止,我在搜索中看到的是动态创建文本框并添加处理程序。但是文本框已经存在,我只需要添加处理程序。
这可行吗?或者有更好的方法吗?
答案 0 :(得分:1)
这应该是一个很好的小模板供您查看。
我们实际上是使用XmlHttpRequest对象将数据发布到generichandler页面。 XHR对象以状态和responseText响应。
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Form.ProductPage.aspx.vb" Inherits="TestWebApplication.Form_ProductPage" %>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
var ProductManager = {
'post': function(data, fnSuccessCallback, fnFailCallback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200){
fnSuccessCallback(xmlhttp.responseText);
}
else {
if (!(fnFailCallback == null)) {
fnFailCallback();
}
}
}
xmlhttp.open("POST","Handler.Products.ashx", true);
xmlhttp.send(data);
},
'label': null,
'textbox': null,
'onBlurHandler':function(e) {
e.preventDefault();
//update the static datado an ajax post, and update total cost
var data = {
'product' : e.target.parentElement.querySelector('span').innerText,
'quantity' : e.target.value
};
ProductManager.post(data, function(result){
var elm = document.getElementById('debugWindow');
elm.innerHTML += '<br />' + result;
});
},
'onChangeHandler':function(e) {
e.preventDefault();
},
'onKeyPressHandler':function(e) {
//e.preventDefault();
},
'init': function() {
ProductManager.label = document.querySelectorAll('.product-wrapper>span');
ProductManager.textbox = document.querySelectorAll('.product-wrapper>input');
for (i = 0; i < ProductManager.textbox.length; i++) {
ProductManager.textbox[i].addEventListener('blur', ProductManager.onBlurHandler, false);
ProductManager.textbox[i].addEventListener('change', ProductManager.onChangeHandler, false);
ProductManager.textbox[i].addEventListener('keypress', ProductManager.onKeyPressHandler, false);
}
}
}
</script>
</head>
<body>
<div id="content-wrapper">
<div class="product-wrapper">
<span runat="server" id="lblProduct1Label"></span>
<input runat="server" type="text" id="tbProduct1Quantity" value="0" /></div>
<div class="product-wrapper">
<span runat="server" id="lblProduct2Label"></span>
<input runat="server" type="text" id="tbProduct2Quantity" value="0" /></div>
<div class="product-wrapper">
<span runat="server" id="lblProduct3Label"></span>
<input runat="server" type="text" id="tbProduct3Quantity" value="0" /></div>
<div class="product-wrapper">
<span runat="server" id="lblProduct4Label"></span>
<input runat="server" type="text" id="tbProduct4Quantity" value="0" /></div>
<div class="product-wrapper">
<span runat="server" id="lblProduct5Label"></span>
<input runat="server" type="text" id="tbProduct5Quantity" value="0" /></div>
<div class="product-wrapper">
<span runat="server" id="lblProduct6Label"></span>
<input runat="server" type="text" id="tbProduct6Quantity" value="0" /></div>
<div class="product-wrapper">
<span runat="server" id="lblProduct7Label"></span>
<input runat="server" type="text" id="tbProduct7Quantity" value="0" /></div>
<div class="product-wrapper">
<span runat="server" id="lblProduct8Label"></span>
<input runat="server" type="text" id="tbProduct8Quantity" value="0" /></div>
<div class="product-wrapper">
<span runat="server" id="lblProduct9Label"></span>
<input runat="server" type="text" id="tbProduct9Quantity" value="0" /></div>
<div class="product-wrapper">
<span runat="server" id="lblProduct10Label"></span>
<input runat="server" type="text" id="tbProduct10Quantity" value="0" /></div>
<div class="product-wrapper">
<span runat="server" id="lblProduct11Label"></span>
<input runat="server" type="text" id="tbProduct11Quantity" value="0" /></div>
<div class="product-wrapper">
<span runat="server" id="lblProduct12Label"></span>
<input runat="server" type="text" id="tbProduct12Quantity" value="0" /></div>
</div>
<div id="debugWindow">
</div>
<script>
ProductManager.init();
</script>
</body>
</html>
Public Class Form_ProductPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
For Each InputControl As HtmlInputText In Me.Controls.OfType(Of HtmlInputText).ToList
InputControl.Value = "0"
Next
Dim I As Integer = 0
For Each LabelControl As HtmlGenericControl In Me.Controls.OfType(Of HtmlControls.HtmlGenericControl).ToList
LabelControl.InnerHtml = "Product " & I
I += 1
Next
End If
End Sub
End Class
Imports System.Web
Imports System.Web.Services
Public Class Handler_Products
Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/plain"
context.Response.Write("Hello World!")
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
注意:上下文公开了HttpRequest对象和HttpResponse对象。您将使用这些对象来确定
其他可能的验证和处理方法。
这也是查询数据库的好地方。记录/更新用户会话/活动详细信息。
答案 1 :(得分:0)
我不编写或维护任何Web应用程序。
根据您的问题,如果我必须为桌面应用编写类似内容,我就会想到这一点。
希望它会给你一些想法,祝你好运!
Option Strict On
Public Class MyItem
Public Property ItemID As Integer
Public Property Value As Decimal
End Class
Public Class Form1
Private _MyItemList As New List(Of MyItem)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
'1, Update the tag property for each text box you need to identify on the form
'2, Instead of using an array. Populate the _MyItemList when you load the file
'3, Loop through the controls list
Me.Controls.OfType(Of TextBox).ToList.ForEach(Sub(_textBox)
If _textBox.Tag Is Nothing OrElse Not IsNumeric(_textBox.Tag) Then Exit Sub 'stop and loop to the next textbox
Dim _textboxID As Integer = CInt(_textBox.Tag)
'I like to get a list so I can check for duplicates
'You could use Dim _item As MyItem = _MyItemList.SingleOrDefault(Function(item) item.ItemID = _textboxID)
Dim _item As List(Of MyItem) = _MyItemList.Where(Function(item) item.ItemID = _textboxID).ToList
'Validate item
If _item Is Nothing Then Exit Sub 'Or throw an "unable to locate item" exception?
If _item.Count > 1 Then Throw New Exception("Duplicate items found for ID: " & _item(0).ItemID.ToString)
'You could add event handlers here
AddHandler _textBox.TextChanged, AddressOf TextValueChanged
AddHandler _textBox.KeyUp, AddressOf TextKeyPress
_textBox.Text = _item(0).Value.ToString 'NOTE: The text changed event will fire. Move this line above the AddHandler if this is not desired
End Sub)
End Sub
Private Sub TextValueChanged(sender As Object, e As EventArgs)
'do something
End Sub
Private Sub TextKeyPress(sender As Object, e As KeyEventArgs)
'do something
End Sub
End Class