0

I am new to asp.net. Here I am trying to create a chat application. In my project there is one label and one TextBox. whenever the user enters anything in TextBox and press enter the text will be shown in Label. I tried the below code:

OnEnterKeyPress of TextBox (.aspx.cs file)

        HttpRequest request = base.Request;           
        Ping p = new Ping();
        PingReply r;
        String s = request.UserHostAddress;  //using own IP address for understanding purpose.
        r = p.Send(s);
        Response.Buffer = false;
        if (r.Status == IPStatus.Success)
        {
            Label1.Text = TextBox1.Text;
        }

I dont know how to keep the enter event. (There are no events in TextBox properties as it were in C#)

Before asking here I checked many links like Link1 Link2 Link3 but no use. I could have used simple Javascript but the main code is in C# file.

Community
  • 1
  • 1
Mr_Green
  • 40,727
  • 45
  • 159
  • 271

3 Answers3

1

If there is only one button in the form that you want to enable 'enter' on, you can set it to be the default

protected void Page_Load(object sender, EventArgs e)
{
    this.Form.DefaultButton = myTextBox.UniqueID;
}

For any button, you will need to use Javascript. You can still use C# to add the attributes. (from here)

myTextBox.Attributes.Add("onkeydown", "if(event.which || event.keyCode)
                  {if ((event.which == 13) || (event.keyCode == 13)) 
                      {document.getElementById('"+Button1.UniqueID+"').click();
                                         return false;}} else {return true};");
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • It is not the only button in the form. Actually I even dont know how the site is because my work will be integrated in to the main site later. – Mr_Green Nov 10 '12 at 12:47
  • This. And if you want this to be interactive, you probably don't want to submit the page every time. Check out SignalR. – MikeSmithDev Nov 10 '12 at 12:49
  • @MikeSmithDev ya I heard SignalR from one of my previous questions before but I have no idea about it. Can you please provide me a sample code on it.. so that I can refer it.. – Mr_Green Nov 10 '12 at 12:52
  • @Mr_Green - Then you need to add some Javascript. I've updated my post. – keyboardP Nov 10 '12 at 12:54
  • @MikeSmithDev - Isn't adding an async framework to handle the enter button a bit overkill? It might be useful for the whole site, but OP seems be working on part of it. – keyboardP Nov 10 '12 at 12:55
  • To handle a button? Indeed. To handle a chat feature for a website? No. – MikeSmithDev Nov 10 '12 at 12:59
1

You'll need to put this in its own form and use javascript, since it will be in a page with other controls. You don't want to "submit" the page every time they type something.

<form action="javascript:chat()">
put label and textbox here
</form>

This will make "enter" work for the textbox. For the chat feature, to make it more interactive, you can implement with http://signalr.net/

MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
  • your explanation is not clear to me. I cant understand how to download signalR. can you please explain it. – Mr_Green Nov 12 '12 at 09:48
  • Sorry for the confusion... my answer addresses your question of how to capture the enter key event for a TextBox. I mentioned SignalR just for something for you to consider when you start trying to make the chat work in real-time. To install SignalR, from your project in Visual Studio, go to Tools > Library Package Manager > Package Manager Console. In the console type Install-Package SignalR. If you run into a new problem with your project, you should post a new question. – MikeSmithDev Nov 12 '12 at 14:09
0

The problem you might be facing here is the event will be processed server side ( after post ). You'll probably still need a javascript to do the post when the enter key has been pressed..

I'm also not certain if the OnEnterKeyPressed is a valid event.