TextBox根据输入字符的个数来触发事件

dt119 2008-09-26 11:12:11
现在做毕业设计,出现一个问题,我是做一个股票分析系统,要求在textbox里输入股票代码后,不进行任何操作,自动在其他的textbox里显示股票名称。我想就只能根据字符长度来触发了,因为股票代码都是6位的。就是一旦字符长度==6,他自动搜索数据库。不过不知道应该怎么写?
哪位高手指点下,因为javascript不怎么懂,最好有点具体代码。


...全文
456 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
a13951845000 2010-11-20
  • 打赏
  • 举报
回复
mark
  • 打赏
  • 举报
回复
两个TextBox关联到一个UpdataPanel,不论它们在界面上离得有多远,不论界面有多大,也只是刷新这两个TextBox控件这一点点html,并没有多少负担。
  • 打赏
  • 举报
回复
[Quote=引用楼主 dt119 的帖子:]
不进行任何操作,自动在其他的textbox里显示股票名称。[/Quote]

这个就是updatepanel的功能。在页面上上放一个Updatapanel,将变化部分关联到它,那么第一个TextBox经过上面的触发,后台查询出数据,可以局部更新updatepanel中的界面。
namhyuk 2008-09-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sp1234 的回复:]
写一行代码就够了。假设文本框的id为TextBox1,你可以在page_load中写:

C# codethis.TextBox1.Attributes["onkeyup"] = "if(this.value.length==6)" +
Page.ClientScript.GetPostBackEventReference(this.TextBox1, string.Empty);
[/Quote]
股票信息这样的应用postack也未免。。。
  • 打赏
  • 举报
回复
写一行代码就够了。假设文本框的id为TextBox1,你可以在page_load中写:
this.TextBox1.Attributes["onkeyup"] = "if(this.value.length==6)" +
Page.ClientScript.GetPostBackEventReference(this.TextBox1, string.Empty);
amandag 2008-09-27
  • 打赏
  • 举报
回复
如果要希望界面无刷新,必然要用类似Ajax之类的技术
namhyuk 2008-09-27
  • 打赏
  • 举报
回复
你这应用,异步和无刷新的必须的了。除了client-callback, 当然也可以用javascript访问Web Service或static类方式实现。
效果上差不多,代码量上也差不多。

namhyuk 2008-09-27
  • 打赏
  • 举报
回复
建议用client-callback.

举个例子, input用于接收输入, divResult用于显示异步查询结果。

<form id="form1" runat="server">
<input id="Text1" type="text" />
<div id="divResult"></div>
</form>


public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
{
private DataTable dt;
private string strReturn;
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "key", "function getResult(result) { document.getElementById('divResult').innerHTML = result;}", true);

string cbReference = Page.ClientScript.GetCallbackEventReference(this,"document.getElementById('Text1').value", "getResult", null);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "recallScript", "function Recall() { var newValue = document.getElementById('Text1').value; if(newValue != '' && oldValue != newValue && newValue.length==6) { oldValue = newValue;document.getElementById('divResult').innerText='请稍侯...';" + cbReference + "}}", true);
Page.ClientScript.RegisterStartupScript(this.GetType(), "startupScript", "var oldValue = ''; setInterval('Recall()', 1000);", true);
}

#region ICallbackEventHandler 成员

public string GetCallbackResult()
{
strReturn = "<table border=1>";
foreach(DataRow row in dt.Rows)
{
strReturn += "<tr>";
foreach (object item in row.ItemArray)
strReturn += "<td>" + item.ToString() + "</td>";
strReturn += "</tr>";
}
strReturn += "</table>";

return strReturn;
}

public void RaiseCallbackEvent(string eventArgument)
{
using (SqlConnection conn = new SqlConnection("server=localhost;database=GtKydj;uid=sa;pwd="))
{
SqlCommand cmd = new SqlCommand("select 列1, 列2,列3 from 表名 where 条件列 like @参数", conn);
cmd.Parameters.AddWithValue("参数", "%" + eventArgument + "%");
dt = new DataTable();
conn.Open();
dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
}
}

#endregion
}
uephee 2008-09-27
  • 打赏
  • 举报
回复
CSDN论坛下面这个框框允许输入8000个字符,看一下html源代码。
估计你这个是不是还得用ajax调用服务器获取相应数据。
JOSHUATSUEI 2008-09-27
  • 打赏
  • 举报
回复
onblur
benbenkui 2008-09-27
  • 打赏
  • 举报
回复
sp1234今天貌似很活跃啊
  • 打赏
  • 举报
回复
updatepanel是可以保持页面各控件状态的。如果你对于页面“状态”没有概念,并且更新的东西特别简单,你可以google关于asp.net的“System.Web.Script.Services.ScriptService”,你可以使用一个后台函数返回一个字符串,然后使用javascript来动态设置dhtml组件的innerText或innerHTML。不过,对于稍微复杂、修饰很多,并且经常需要变化调整的界面,使用javascript是限制了扩展能力。javascript代码要向那“一行代码”一样能省则省,只是用到刀刃上。
  • 打赏
  • 举报
回复
图片上的人长得不怎样,不过你可以看到图片并不刷新,实际上除了上下两个细条,别的地方都不刷新。从服务器上仅下载刷新区域的html,还是很精简的。(当然如果你的ViewState太大还应该参考我的那个关于把ViewState放在服务器端保持的帖子,否则ViewState的尺寸降低了updatepanel的一部分作用)

这里,TextBox和下面的Label通过Updatepanel关联起来,TextBox的后台TextChanged事件刷新了下面很远处的Label。
  • 打赏
  • 举报
回复
我写了一个简单的例子:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestTextbox.aspx.cs" Inherits="TestTextbox" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>演示输入文本触发自动刷新</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<hr />
<asp:Image ID="Image1" runat="server" ImageUrl="http://www.dameinv.com/ewebed/imgbd/2006/2/16/200621616361576736.jpg" /><hr />
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>

using System;

public partial class TestTextbox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.TextBox1.Attributes["onkeyup"] = "if(this.value.length>=3)" +
this.ClientScript.GetPostBackEventReference(this.TextBox1, string.Empty);
}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string res = string.Format("您在{0}点输入了 {1}", DateTime.Now.ToString("m:s.fff"), this.TextBox1.Text);
this.Label1.Text = res;
this.Label2.Text = res;
this.UpdatePanel2.Update();
}
}
mxftzjz45 2008-09-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sp1234 的回复:]
写一行代码就够了。假设文本框的id为TextBox1,你可以在page_load中写:

C# codethis.TextBox1.Attributes["onkeyup"] = "if(this.value.length==6)" +
Page.ClientScript.GetPostBackEventReference(this.TextBox1, string.Empty);
[/Quote]

学习了……
homepgdn 2008-09-26
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" enableEventValidation="true"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script>
function aa(t1)
{
if(t1.value.length==6)
document.form1.submit();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" onkeyup="aa(this)"></asp:TextBox>
</form>
</body>
</html>



using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<script>alert('" + TextBox1.Text + "');</script>");
}


}
ikelvin 2008-09-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ustbwuyi 的回复:]
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="ustb" onkeyup="SearchCode(this)" />
</div>
</form>
<script language="javascript">
function SearchCode(obj)
{
var value=obj.value;
if(value.length==6)
{
alert(value);
...........
...........
}
}
</script>
</body>
</html>
[/Quote]

就这么写,然后进数据库检索
ustbwuyi 2008-09-26
  • 打赏
  • 举报
回复
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="ustb" onkeyup="SearchCode(this)" />
</div>
</form>
<script language="javascript">
function SearchCode(obj)
{
var value=obj.value;
if(value.length==6)
{
alert(value);
...........
...........
}
}
</script>
</body>
</html>


62,074

社区成员

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

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

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

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