0

I'm using web forms

Right now my code runs when I leave the text box and the text has changed. I am running into issues. If I change the text but hit a button instead of enter, it resets via code. I need to be able to change the text and click a button which wont yet do anything, or change the text and hit enter which will trigger code.

thanks for the help

This is text changed event for the text box with notations of what im needing to do. really what I think I need is an event for clicking enter, not changing text

protected void txtboxPlate_TextChanged(object sender, EventArgs e)
        {
            if (txtboxPlate.Text == "plate number")
            {
                //will check database for "plate number" and do stuff on enter.
            }
            else
            {
                    resetforms();// on enter
            }
        else
        {
            the text has changed by user, but has clicked a button and needs nothing to happen because of this text change
            }       
        }
birddseedd
  • 65
  • 1
  • 1
  • 8

3 Answers3

0

You need to implement a method for the KeyDown event of your textbox.

private void txtboxPlate_KeyDown(object sender, KeyEventArgs e)
{
     if (e.KeyCode == Keys.Enter) {
          //Code
     }
}
Tim Schmidt
  • 1,297
  • 1
  • 15
  • 30
  • The type or namespace KeyEventArgs could not be found. I am using web forms I believe (website project) if that helps. – birddseedd Oct 24 '16 at 07:58
  • You should have stated that in your question then. I guess many assumed that you use WinForms. I updated the tags in your question. – jAC Oct 24 '16 at 08:00
  • Oh Sorry. Yea i did not recognize that you wanted a solution for web forms. Unfortunately i can´t haelp you then. – Tim Schmidt Oct 24 '16 at 08:02
0

If this is winform and you still want to use textchanged you can try catching your code first for example:

protected void txtboxPlate_TextChanged(object sender, EventArgs e)
    {
        //Catching code
        if (txtboxPlate.Text != "")         
        {
            if (txtboxPlate.Text == "plate number")
            {
                //will check database for "plate number" and do stuff on enter.
            }
            else
            {
                resetforms();// on enter
            }
        }

    else
    {
        the text has changed by user, but has clicked a button and needs nothing to happen because of this text change
    }

cause what textchanged is doing is unless txtboxPlate.Text equels "plate number" it will always do the else statement. Correct me if i'm wrong though but i had the same problem before which almost made me go insane.

Or try above 1 upvote code:

protected void txtboxPlate_TextChanged(object sender, EventArgs e)
{
    //Event only happens if you press enter
    if (e.KeyCode == Keys.Enter) 
    {
        if (txtboxPlate.Text == "plate number")
        {
            //will check database for "plate number" and do stuff on enter.
        }
        else
        {
            resetforms();// on enter
        }
    }            


else
{
    the text has changed by user, but has clicked a button and needs nothing to happen because of this text change
}
}
P. Pat
  • 488
  • 4
  • 13
  • I am not sure this will work. if (txtboxPlate.Text != "") will always be true before the other button is pressed (enter will not be pressed at this time). But because the value is not in the database ("plate number") it is going to want to go to the first else and reset forms – birddseedd Oct 24 '16 at 08:15
  • True but it doesn't really need to be != "" or null. Cause textchanged event will always happen whether you hit enter or press the button as long as txtboxPlate_TextChanged value changes and the very first if statement is true. – P. Pat Oct 24 '16 at 08:19
0

Keydown is a client-side event, as far as I know you're more than likely going to have to use JavaScript/Jquery.

Refer to the following link:

Textbox Keydown Event

I would of used a comment but... rep issues :/

edit: To anyone that hasn't realised yet, the question changed to webforms not winforms

Alternative:

Use a button.

You could place a button next to the textfield, it's simple and not a lot of work goes into it. You can set the TabIndex of the textbox to 1 and the TabIndex of the button to 2 so when you hit TAB it will focus the textbox and if pressed again it will focus the button. You could also look into adding the button to the textbox for design purposes.

edit: You also need to think about post back, when you hit a button the page get's post back meaning values are lost, to persist the data within the field you would have to use view state, session variables or a hidden field. For simplicity I'd set the value of the text box to a hidden field and then simply re-apply the value on page_load. If the value needs to persist across multiple postbacks/accessed from other pages use session variables.

So as a code example:

Remove the Text_changed event entirely.

protected void btnDoStuff(object sender, EventArgs e)
{
    if(txtPlateNumber.Text == "Plate Number")
    {
        //Do stuff
    }
    else
    {
        //Do other stuff
    }
}

-

If you notice the value disappearing after the post back do something like:

protected void btnDoStuff(object sender, EventArgs e)
{
    if(txtPlateNumber.Text == "Plate Number")
    {
        //Do stuff
        myHiddenField.Value == txtPlateNumber.Text;
    }
    else
    {
        //Do other stuff
    }
}

then reset the value on page_load.

Community
  • 1
  • 1
qqqqq
  • 23
  • 6
  • That makes sense. but for the simplicity of this demo (the fact that I have to do a bit of learning and need to have this done by noon lol) Im trying to stay server side. this may not be a server side capable thing. – birddseedd Oct 24 '16 at 08:17
  • You could use a button? I'll update my question to give you an example. – qqqqq Oct 24 '16 at 08:19
  • I may have to use the button for the demo. it seems client side javascript is going to be the cleanest simplest way to do it. I just don't want to code that right now. so may just use a button. in the end I want to avoid a button as most of the time you will just hit enter and im trying to keep this as quick and streamlined as possible. – birddseedd Oct 24 '16 at 08:44
  • Yeah, the Javascript shouldn't be too complicated. You could even look into calling a server-side method from Js that holds all the code for accessing the db etc. etc. – qqqqq Oct 24 '16 at 09:04