Control postback events when enter key is pressed

J

jhoge123

This is one of those problems that should be easy but isn't turning out
that way: There are two textboxes on a page, each associated with a
button which has a Click event to do something different.

What happens when the user presses the Enter key?

I want one of the event handlers to be fired in this situation. The
only solution I have for this is not pretty: To use a flag to check if
the event has been fired from the Page_Load event handler.

bool calledByEnterKey= false;
private void Page_Load(object sender, System.EventArgs e)
{
if(IsPostBack)
{
btnSearch_Click(new Object(), new System.EventArgs());
calledByEnterKey==true;
}
}

This calls my click event for all postbacks. The event then must check
the boolean property calledByEnterKey to make sure it doesn't run
twice:

private void btnSearch_Click(object sender, System.EventArgs e)
{
if (!calledByEnterKey)
{
//do something
}
}

This works, but it's not the clear, elegant solution that I would like.
There must be a better way!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top