Click here to Skip to main content
15,936,294 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
this Code works in aspx page but i made that to a usercontrol.
So In ctrlName i get null because
var ctrlName = Request.Params[Page.postEventSourceID];

& On The Above Code Page Refers To A Aspx Page But I Have A Usercotrol

What I have tried:

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
Updated 13-Apr-16 2:09am
Sergey Alexandrovich Kryukov 13-Apr-16 8:34am    
Add an new event in a user control. In its code, invoke it in a permanently subscribed event handler of the text box event.
ARYA JAGANNATH 13-Apr-16 10:05am    
See I Have A Webform Named "SiteFile.aspx"
In That Page One User Control I have Created In Which TextBox1 Is Present.
I Want To Handle key Press event to restrict only Numbers Should Be Allowed In That User Control or SiteFile.aspx Page
Like The Following Code

private void textBoxTag1_KeyPress(object sender, KeyPressEventArgs e)
if (char.IsNumber(e.KeyChar) != true)
e.Handled = true;

[no name] 14-Apr-16 17:17pm    
you may also set KeyPreView to TRUE, if the eventhandler is not reacting
[no name] 15-Apr-16 5:54am    
Just out of curiosity, how are you planning on handling when a user right clicks their mouse and pastes content into your textbox rather than typing anything?
Richard Deeming 15-Apr-16 15:01pm    
Causing a postback and reloading the page every time the user presses a key seems like a good way to annoy your users and ensure that nobody uses your site.

You should be using Javascript to handle keypress events, not a server-side method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900