如何用JavaScript让页面主动回发?

meidengyin 2003-08-29 10:12:06
比如:有一个TextBox和Button(保存),
我如何做到:用户在TextBox中输入字符7个字符的时候,WebForm主动触发(Button(保存))事件?
...全文
108 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2003-08-30
  • 打赏
  • 举报
回复
I cannot reproduce your problem with the same aspx page and this codebehind:

Public Class chaiban
Inherits System.Web.UI.Page
Protected WithEvents txtPlateNo As System.Web.UI.WebControls.TextBox
Protected WithEvents txtProductSN As System.Web.UI.WebControls.TextBox
Protected WithEvents lblMsg As System.Web.UI.WebControls.Label
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents lblProductSN As System.Web.UI.WebControls.Label
Protected WithEvents lblModel As System.Web.UI.WebControls.Label
Protected WithEvents lblSpec As System.Web.UI.WebControls.Label
Protected WithEvents lblWeight As System.Web.UI.WebControls.Label
Protected WithEvents lblCnt As System.Web.UI.WebControls.Label
Protected WithEvents BUTTON2 As System.Web.UI.HtmlControls.HtmlButton
Protected WithEvents btnSave As System.Web.UI.HtmlControls.HtmlButton

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Response.Write("Page_Load is called<BR>")
End Sub

Private Sub btnSave_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.ServerClick
Response.Write("btnSave_ServerClick is called<BR>")
End Sub
End Class
panyee 2003-08-30
  • 打赏
  • 举报
回复
Page_Load里加入

TextBox1.Attributes.Add("onkeydown", "if(event.keyCode!=8&&this.value.length==7) this.form.Button1.click();");

用退格键(8)允许它删除,但要输入第8个字符才能触发事件(?)

取长度用this.value.length
meidengyin 2003-08-30
  • 打赏
  • 举报
回复
Imports System.Data.SqlClient

Public Class chaiban
Inherits System.Web.UI.Page
Protected WithEvents lblMsg As System.Web.UI.WebControls.Label
Protected WithEvents lblModel As System.Web.UI.WebControls.Label
Protected WithEvents lblSpec As System.Web.UI.WebControls.Label
Protected WithEvents lblWeight As System.Web.UI.WebControls.Label
Protected WithEvents lblCnt As System.Web.UI.WebControls.Label
Protected WithEvents txtPlateNo As System.Web.UI.WebControls.TextBox
Protected WithEvents txtProductSN As System.Web.UI.WebControls.TextBox
Protected WithEvents BUTTON2 As System.Web.UI.HtmlControls.HtmlButton
Protected WithEvents btnSave As System.Web.UI.HtmlControls.HtmlButton
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents lblProductSN As System.Web.UI.WebControls.Label

#Region " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub

#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
End Sub

Private Sub btnSave_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.ServerClick
'Session("cnt") = 1 + CInt(Session("cnt"))
'Label1.Text = Session("cnt")
'If Session("LastValue") = Me.txtPlateNo.Text.Trim & "|" & Me.txtProductSN.Text.Trim Then
' Exit Sub
'End If
'Session("LastValue") = Me.txtPlateNo.Text.Trim & "|" & Me.txtProductSN.Text.Trim
Dim Cnn As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("Cnn")) '"Data Source=Dbserver;User Id=sa;Password=;Initial Catalog=YsMis") )
Dim Cmd As New SqlCommand()
Dim ResultReader As SqlDataReader
Try
Cnn.Open()
With Cmd
.Connection = Cnn
.CommandType = CommandType.StoredProcedure
.CommandText = "ChaiBan"
.Parameters.Add("@fPlateNo", Me.txtPlateNo.Text.Trim)
.Parameters.Add("@fProductSN", Me.txtProductSN.Text.Trim)
.Parameters.Add("@fUserNo", Session("User").ToString)
ResultReader = .ExecuteReader
While ResultReader.Read()
Select Case ResultReader.GetString(0).Trim.ToUpper
Case "OK"
Me.lblMsg.Text = "拆版完成,请继续..."
Me.lblProductSN.Text = ResultReader.GetString(1)
Me.lblModel.Text = ResultReader.GetString(2)
Me.lblSpec.Text = ResultReader.GetString(3)
Me.lblCnt.Text = ResultReader.GetInt32(4)
Me.lblWeight.Text = ResultReader.GetInt32(5)
'Me.txtPlateNo.Text = ""
Me.txtProductSN.Text = ""
Case "NoPlate".ToUpper
Me.lblMsg.Text = "查无此板号!"
Me.lblProductSN.Text = ""
Me.lblModel.Text = ""
Me.lblSpec.Text = ""
Me.lblCnt.Text = ""
Me.lblWeight.Text = ""
Case "NotPlateProduct".ToUpper
Me.lblMsg.Text = "此塑盘和托板不匹配!"
Me.lblProductSN.Text = ""
Me.lblModel.Text = ""
Me.lblSpec.Text = ""
Me.lblCnt.Text = ""
Me.lblWeight.Text = ""
Case "NoProductSn".ToUpper
Me.lblMsg.Text = "查无此塑盘条码!"
Me.lblProductSN.Text = ""
Me.lblModel.Text = ""
Me.lblSpec.Text = ""
Me.lblCnt.Text = ""
Me.lblWeight.Text = ""
End Select

End While
ResultReader.Close()
End With
Catch Ec As Exception
lblMsg.Text = Ec.Message
Finally
' Cmd.Dispose()
Cnn.Close()
End Try

End Sub

End Class
meidengyin 2003-08-30
  • 打赏
  • 举报
回复
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="chaiban.aspx.vb" Inherits="YsMis.chaiban"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>chaiban</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function GetPlateInfo(){
window.location = "plateinfo.aspx?PlateNo="+window.Form1.txtPlateNo.value;
}
function GetPsInfo(){
window.location = "PsInfo.aspx?ProductSN="+window.Form1.txtProductSN.value;
}

function ChkPlateNo(){
var strPlateNo=window.Form1.txtPlateNo.value;
if (strPlateNo.length == 12) {
var strProductSN=window.Form1.txtProductSN.value;
if (strProductSN.length==15) {
//__doPostBack("btnSave","");
//alert("__doPostBack--ChkPlateNo");
document.Form1.btnSave.click();
}else{
lblMsg.innerHTML="请输入塑盘条码!";
window.Form1.txtProductSN.select();
}
}
}
function ChkProductSN(){
var strProductSN=window.Form1.txtProductSN.value;
if (strProductSN.length==15) {
var strPlateNo=window.Form1.txtPlateNo.value;
if (strPlateNo.length == 12) {
//alert("__doPostBack--ChkProductSN");
//__doPostBack("btnSave","");
document.Form1.btnSave.click();
}else{
lblMsg.innerHTML="请输入托板条码!";
window.Form1.txtPlateNo.select();
};
}
}
function Init(){
window.Form1.txtProductSN.focus();
}
</script>
</HEAD>
<body onload="Init()" MS_POSITIONING="GridLayout" bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0" bgColor="#c0c0c0">
<form id="Form1" method="post" runat="server">
<table style="LEFT: 0px; POSITION: absolute; TOP: 0px" height="320" cellSpacing="0" cellPadding="0" width="240" border="1">
<tr>
<td align="middle">
<table borderColor="#339966" cellSpacing="0" cellPadding="0" width="80%" bgColor="#339966" border="1">
<tr>
<td align="middle" width="100%"><b><font color="#ffffff"> 拆  板</font></b>
</td>
</tr>
</table>
</td>
<tr>
<td vAlign="center">
<table borderColor="#111111" cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td align="right">托板编号:</td>
<td><asp:textbox id="txtPlateNo" runat="server" onkeyup="ChkPlateNo()" Width="100" MaxLength="20"></asp:textbox><button onclick="GetPlateInfo()" name="B5" type="button">查询</button></td>
</tr>
<tr>
<td align="right">塑盘条码:</td>
<td><asp:textbox id="txtProductSN" runat="server" onkeyup="ChkProductSN()" Width="100" MaxLength="20"></asp:textbox><BUTTON onclick="GetPsInfo()" name="B5" type="button">查询</BUTTON></td>
</tr>
<tr>
<td colSpan="2" height="15"></td>
</tr>
<tr>
<td align="right" colSpan="2"><button id="BUTTON2" style="FLOAT: right; WIDTH: 77px; HEIGHT: 21px; BACKGROUND-COLOR: #ffff00" onclick="window.location.href='index.htm'" name="btnReturn" type="button" runat="server">返回</button>
<button id="btnSave" style="FLOAT: right; WIDTH: 77px; HEIGHT: 21px; BACKGROUND-COLOR: #ffff00" name="B3" type="button" runat="server">
拆板</button></td>
</tr>
</table>
</td>
</tr>
<tr>
<td vAlign="center"><asp:label id="lblMsg" runat="server" ForeColor="Red" Font-Bold="True">请扫描...</asp:label>
<asp:Label id="Label1" runat="server">Label</asp:Label>
<hr>
塑盘编号:
<asp:label id="lblProductSN" runat="server" ForeColor="Blue" Font-Bold="True"></asp:label><br>
型号:
<asp:label id="lblModel" runat="server" ForeColor="Blue" Font-Bold="True"></asp:label><br>
规格:
<asp:label id="lblSpec" runat="server" ForeColor="Blue" Font-Bold="True"></asp:label><br>
此板总重量:
<asp:label id="lblWeight" runat="server" ForeColor="Blue" Font-Bold="True"></asp:label>Kg
<br>
此板总盘数:
<asp:label id="lblCnt" runat="server" ForeColor="Blue" Font-Bold="True"></asp:label>盘
</td>
</tr>
</table>
</form>
</body>
</HTML>
saucer 2003-08-30
  • 打赏
  • 举报
回复
show your code
meidengyin 2003-08-30
  • 打赏
  • 举报
回复
<button id="btnSave" name="B3" type="button" runat="server">拆板</button>
也没有 OnServerClick="....."

而且又是会执行三次
saucer 2003-08-30
  • 打赏
  • 举报
回复
if you have
...Handles ...

somewhere, make sure you don't set the event handler again, for example, remove
OnServerClick="....." from your button declaration
meidengyin 2003-08-30
  • 打赏
  • 举报
回复
谢谢楼上两位。

方法有效,可是 用这种方法为什么 会回发两次呢?

btnSave.ServerClick 和 Handles MyBase.Load 分别执行了两次!

(我已经有 AutoEventWireup="false" 语句了)
saucer 2003-08-29
  • 打赏
  • 举报
回复
<asp:TextBox id=txt1 runat=server onchange="if (this.length ==7) this.form.YourButtonID.click()">

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧