当我们在文本框中键入文本时,如何调用c#中代码隐藏的一个事件 [英] how to invoke one event which is code behind in c# when we type text inside of the textbox

查看:184
本文介绍了当我们在文本框中键入文本时,如何调用c#中代码隐藏的一个事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have text box and gridview:

aspx page:

<asp:TextBox ID="txtsearch" runat="server" OnTextChanged="search" AutoPostBack="true"> </asp:TextBox>

aspx.cs:

Protected void search(object sender, eventargs e)

{

//here i am binding gridview stuff.

}

gridview:

name id

rau     1

rahul   2

mash   3

christ   4

everything works fine. but, when i typing name for example: mash. it is not retrieving or it is not calling that search function. when i give tab or leave from textbox, it is invoking search function.

but i need while typing 'm' itself , it should invoke the search function. i mean by automatically it shoud invoke when we type text inside the textbox not after leaving or tab from textbox. how to solve this problem. help needed.

推荐答案

键入的字符没有服务器端事件 - TextChanged仅在文本框焦点丢失时发出信号。



但是严肃地说,你可能不喜欢我想这样做。



原因是它意味着从非常密钥的客户端到服务器的回发,然后在服务器上搜索,一个页面加载回客户端 - 这将在它自己的时间响应。



如果用户乐于输入一个字符,请等待,输入一个字符,等一下,输入一个角色......然后你会没事的,但是对于大多数用户而言,这会让人感到烦恼。



试着看你的搜索在javascript中本地到客户端。
There is no server side event for character typed - TextChanged is only signaled when the textbox focus is lost.

But in all seriousness, you probably don't want to do that.

The reason is that it would mean a postback going from the client to the server on very key, followed by a search at the server, and a page load back to the client - which will respond in it's own time.

If the user is happy to type a character, wait, type a character, wait, type a character,... then you will be ok, but for most users that would be an annoying exercise in frustration.

Try looking at doing your search locally to the client in the javascript instead.


您正在观察此行为,因为搜索是在OnTextChanged上调用的当焦点移出控件时,文本框的值会发生变化。



尝试在文​​本框中使用Jquery注册事件(请检查哪一个适合KeyPress或KeyUp)
you are observing this behavior because the search is called on "OnTextChanged" event, the value of text box gets changed when you focus is moved out of the control.

Try registering the event with the use of Jquery on your text box (please check which one suits KeyPress or KeyUp)


TextBox服务器控件不会在每个键击中引发事件,而是在失去控制焦点后才触发事件。



尝试执行以下操作以解决您的问题

来自客户端,onKeyUP事件调用_postback()方法!!
TextBox server control doesn't raise event on each key stroke, rather the event gets fired only after losing the control focus.

Try doing the below in order to solve your problem
from the client side, onKeyUP event call _postback() method !!


这篇关于当我们在文本框中键入文本时,如何调用c#中代码隐藏的一个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆