By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 1,904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

Control postback events when enter key is pressed

P: n/a
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!

Nov 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.