Textbox的Textchanged事件不要顺序
我正在尝试根据用户类型在字符中输入的数字动态过滤数据。我发现,文本值似乎是出于秩序,我不知道为什么。例如,如果文本框中已经有“ 1379”,然后我在那之后输入“ 2019年”(因此以“ 13792019”结束),我会得到以下内容:
TxtFilter_TextChanged; text 13792; Time: 637865675822596714
TxtFilter_TextChanged; text 1379201; Time: 637865675846017202
TxtFilter_TextChanged; text 13792019; Time: 637865675847734984
TxtFilter_TextChanged; text 137920; Time: 637865675849383373
我正在tick中记录时间。我肯定可以看到这些值正在增加,但是我不知道为什么/“ 137920”如何在“ 13792019”之后出现?我可以始终如一地再现这个。
现在,我导致回发在钥匙UP上,以便可以将焦点放在文本框上。在onfocus上,我将光标设置为最后一个位置(否则它将返回到开始)。我开放的是更好的方法...
<script type="text/javascript">
function RefreshUpdatePanel(e) {
__doPostBack('<%= txtFilter.ClientID %>', '');
document.all.txtFilter.focus();
};
function SetEnd(txt) {
if (txt.createTextRange) {
//IE
var FieldRange = txt.createTextRange();
FieldRange.moveStart('character', txt.value.length);
FieldRange.collapse();
FieldRange.select();
}
else {
//Firefox and Opera
txt.focus();
var length = txt.value.length;
txt.setSelectionRange(length, length);
}
}
CodeBehind:LoadorderList方法根据过滤后的文本绑定数据,然后为所选第一个项目(如果有)加载订单信息。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
txtFilter.Attributes.Add("OnFocus", "SetEnd(this);");
txtFilter.Attributes.Add("OnKeyUp", "RefreshUpdatePanel();");
LoadOrderList();
}
}
protected void TxtFilter_TextChanged(object sender, EventArgs e)
{
DateTime dtNow = DateTime.Now;
System.Diagnostics.Trace.WriteLine(string.Format("TxtFilter_TextChanged; text {0}; Time: {1}", ((TextBox)sender).Text, dtNow.Ticks));
LoadOrderList();
}
I am trying to dynamically filter data based on a number entered into a TextBox as the user types in characters. I am finding though that the text values seem to come out of order and I have no idea why. For example, if the textbox already has "1379" in it and I then type in "2019" after that (so finishing with "13792019"), I am getting the following:
TxtFilter_TextChanged; text 13792; Time: 637865675822596714
TxtFilter_TextChanged; text 1379201; Time: 637865675846017202
TxtFilter_TextChanged; text 13792019; Time: 637865675847734984
TxtFilter_TextChanged; text 137920; Time: 637865675849383373
I am logging the Time in Ticks. I can definitely see that the values are increasing, but I have no idea why/how "137920" is coming in after "13792019"? I can consistently reproduce this.
Right now, I cause the postback to happen on keyup so that I can set the focus back on the textbox. On OnFocus, I am setting the cursor to the last position (otherwise it goes back to the start). I'm open to better ways of doing this...
<script type="text/javascript">
function RefreshUpdatePanel(e) {
__doPostBack('<%= txtFilter.ClientID %>', '');
document.all.txtFilter.focus();
};
function SetEnd(txt) {
if (txt.createTextRange) {
//IE
var FieldRange = txt.createTextRange();
FieldRange.moveStart('character', txt.value.length);
FieldRange.collapse();
FieldRange.select();
}
else {
//Firefox and Opera
txt.focus();
var length = txt.value.length;
txt.setSelectionRange(length, length);
}
}
Codebehind: The LoadOrderList method binds the data based on the filtered text, and then loads the order info for the first item selected, if any.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
txtFilter.Attributes.Add("OnFocus", "SetEnd(this);");
txtFilter.Attributes.Add("OnKeyUp", "RefreshUpdatePanel();");
LoadOrderList();
}
}
protected void TxtFilter_TextChanged(object sender, EventArgs e)
{
DateTime dtNow = DateTime.Now;
System.Diagnostics.Trace.WriteLine(string.Format("TxtFilter_TextChanged; text {0}; Time: {1}", ((TextBox)sender).Text, dtNow.Ticks));
LoadOrderList();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会在这里完成全页。您需要进行Web API调用,传递文本框,恢复响应。您有使用AjaxtoolKit吗?它具有自动完整的预构建,可让您设置时机/延迟,甚至要输入的字符数量。
现在,我可能不想为此功能采用整个ajaxtoolkit,但是我当然会考虑使用工具包 - 因为自动完成套件确实可以很好地工作,并且自动魔术会为您提供ajax的启动 - 没有页面帖子。
而且易于使用。
您还可以获得多选择的组合框,一个真正光滑的对话框(更容易bootstratp或jquery.ui使用)。而且您可以找到非常好的上装。
因此,考虑AjaxtoolKit。它当然是有些过时的,但是如果您的项目仍然是Web表单,那么我将考虑采用工具套件,从那以后,您可以使用自动完成的示例和工具 - 战斗硬化的一个您知道的一个刚刚起作用。出色地。
实际上,请注意这里房间里的一头大象吗?
这是什么?
当然,为什么没有人愿意发布他们的工作示例,并建议/展示他们在这项非常常见的任务和功能中使用的用途。
而且由于没有人发布示例,因此非常意味着设置此类和示例的时间和精力比发布So所需的时间更多。
而且,如果显示出这样的Exmaple的时间是更多,那么将其发布为简单答案所需的时间?
你无与伦比失败了!!! - 换句话说,您不仅想要这种功能,而且要再次执行此操作的接下来的10个示例呢?再一次???
那么真正的目标,真正的答案?
无论您走过哪条路和哪条路,您最认识地沿着一条可以一遍又一遍地使用工作解决方案的道路。
因此,说我想搜索一些酒店名称。所以,掉落;在文本框中,然后单击Ajax工具套件扩展器。
因此,我放在文本框中,然后去:
现在,选择自动完成
那么,现在我的标记是这个
和背后的代码?此:
现在我们已经有了:
因此,您具有以下设置:
因此,有“现有工具为此。
但是我想更多,然后出去挑选任何旧工具?
真正的交易,真正的问题?
您是否有一个工具可以让您花时间在这里回答问题,以显示一个有效的示例???
没有其他人愿意这样做的事实?
这并不意味着他们是卑鄙的人,坏人,甚至是懒惰的人。
但这确实意味着他们没有选择一个解决方案,它为您提供了巫师,助手和在UI设计人员中构建的解决方案,从而使我能够完成所有这些工作 - 因为我使用了非常简单的解决方案。
I would not do a full page post-back here. You need to do a web API call, pass the text box, get the response back. Do you have use of the ajaxtoolkit? It has a auto complete pre-built, and lets you set the timing/delay, and even number of characters you have to type.
Now, I might not want to adopt the whole ajaxtoolkit for this ONE feature, but I certainly would consider the tool kit - as the autocomplete does work well, and auto magic wires up the ajax calls for you - without a page post back.
And it easy to use.
You also get mult-select combo boxes, a really slick dialog box (easier then bootstratp, or jquery.ui ones to use). And you get to VERY nice up-loaders.
So, consider the ajaxtoolkit. It is certainly and somewhat dated, but if your project is still web forms, then I would consider adopting the tool kit, since then you can a working auto-complete example and tool - one that been battle hardened an one that you know just works well.
In fact, note ONE BIG ELEPHANT in the room here?
What is it?
Why of course no one was willing to post THEIR working example, and suggest/have show what they use for this VERY common task and feature.
And since no one is posting examples, then it VERY much means the time and effort to setup such and example is MORE TIME then what it takes to post on SO.
And if the time taken to show such an exmaple is MORE then what it takes to post on SO as simple answer?
You ALEADY FAILED!!! - in other words, you want this kind of feature not only now but what about the next 10 examples you want to do this again? and again???
So the REAL goal, the REAL answer?
Regardless of WHAT and WHICH road you take, you MOST CERTIANLY want to go down a road in which a working solution can be used over and over.
So, say I want to search for some hotel names. So, drop; in a text box, then click on the ajax tool kit extender.
So, I dropped in a text box, and then go:
Now, select auto complete
So, now my markup is this
And the code behind? This:
And now we have this:
And you get a whole property sheet - you can customize the dealy, prefix length - all the setting and setup is done for you.
so, you have these settings:
So, there "are" existing tools for this.
But I think MUCH MORE then just going out and picking any old tool?
The real deal, the real issue?
Do you have a tool that lets YOU SPEND THE TIME here on SO to answer a question, to show a working example???
The fact that no one else is willing to do this?
It does not mean they are mean people, bad people, or even lazy people.
But it DOES mean they not picked a solution that gives you wizards, helpers, and built in UI designers that ALLOWED me to do all this work - in near record time, since I used a VERY easy solution.