Web - Pressing "Return" on a form

  • Thread starter Thread starter David P. Donahue
  • Start date Start date
D

David P. Donahue

Is there a way to specify on each control of a page which button on the
page should be "clicked" when the user presses "return" from within that
control? It seems like it's always just defaulting to the first button
it finds on the page, which isn't always correct.


Regards,
David P. Donahue
(e-mail address removed)
 
hi david,

Following is what you can do to set the focus to the desired button. It is
necessary to set the focus in the click event
of the button.


I have used RegisterStartupScript to register the javascript. Copy paste the
following code in the webform class and create the controls as necessary.

protected void setTheFocus( System.Web.UI.Control btnControl )
{
string s = "<SCRIPT language='javascript'>document.getElementById('" +
btnControl.ID + "').focus() </SCRIPT>";
this.RegisterStartupScript( "FocusEvent", s );
}



private void Button1_Click(object sender, System.EventArgs e)
{
this.setTheFocus(Button1);
}

private void Button2_Click(object sender, System.EventArgs e)
{
this.setTheFocus(Button2);
}


Happy programming!!!

pradeep_TP
 
Back
Top