如何在TextBox Asp.net应用程序的KeyPress上调用C#方法。 [英] How to call a C# Method on KeyPress of the TextBox Asp.net Application.

查看:65
本文介绍了如何在TextBox Asp.net应用程序的KeyPress上调用C#方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何在Asp.net应用程序中调用TextBox的KeyPress上的C#方法。



请提供相同的一个样本

Hi,

How to call a C# Method on KeyPress of the TextBox in Asp.net Application.

Please provide the one sample for the same

推荐答案

在ASP.NET页面

In ASP.NET Page
<asp:textbox id="MyTextBox" runat="server" onkeypress="__doPostBack(this.name,'OnKeyPress');" ></asp:textbox>





In CodeBehined文件



In CodeBehined file

protected void Page_Load(object sender, EventArgs e){
    var ctrlName = Request.Params[Page.postEventSourceID];
    var args = Request.Params[Page.postEventArgumentID];

    if(ctrlName == MyTextBox.UniqueID && args == "OnKeyPress"){
        MyTextBox_OnKeyPress(ctrlName, args);
    }
}

private void MyTextBox_OnKeyPress(string ctrlName, string args){
    //your code goes here
}





每次按下提交的关键页面都会导致页面完全刷新。

使用更新的面板来避免整页提交。



希望这会对你有所帮助



Every time you press a key page get submitted which causes the page to be refreshed totally.
Use updated panels to avoid full page submissions.

Hope this will help you


你可以使用Javascript。

You can use Javascript.
OnPageLoad()
 {
  TextboxId.Attributes.Add("onclick") = "myfunction(this.Id)";
 }





aspx文件上的Javascript:



Javascript on aspx file:

myfunction(textboxid){
//do your stuff
}


是的,在asp.net服务器端控制文本框中没有任何按键事件是正确的。 OnTextChange的代码就像

服务器端代码



Yes it is correct in asp.net server side control text box has no any keypress event. OnTextChange is there the code smample like
Server side code

protected  void My_OnTextChanged(object sender, EventArgs e)
       {
           var txt = sender as TextBox;
           string value = txt.Text;
       }





在标记声明中





In markup declaration



< asp:textbox id =txtOnerunat =serverontextchanged =My_OnTextChangedautopostback =truexmlns:asp =#unknown>


<asp:textbox id="txtOne" runat="server" ontextchanged="My_OnTextChanged" autopostback="true" xmlns:asp="#unknown">





请记住,AutoPostBack属性非常重要,没有文本框控件不会回发。另一种是当您完成将数据写入文本框之后,当您按下回车或标签时触发事件。它类似于Windows应用程序的丢失焦点事件。

如果你真的想去实际的按键事件那么你必须去按键事件并使用javasript / jquery来订阅那个事件。



// DOM准备活动



Just remember that AutoPostBack property is very important there without that the textbox control will not be postback. Another is when you finish to write your data to the textbox after that when you press enter or tab that time the event is triggered. It is similar to lost focus event of windows application.
If you really want to go actual keypress event then you must go dom key press event and use javasript/jquery for subscribe that event.

//DOM ready event


这篇关于如何在TextBox Asp.net应用程序的KeyPress上调用C#方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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