Thursday, March 22, 2012

Set Focus to TextBox

I would like to set the focus to a textbox when the page first openes. I have a couple of textboxes on the form and would like the cursor to appear in the first text box when the page opens.

This is the code that I am tring to work with


<td style="WIDTH: 81px; HEIGHT: 27px" align="left">Defect:</td
<td style="WIDTH: 141px; HEIGHT: 27px">
<asp:textbox id="Defect" Runat="server" Width="304px"></asp:textbox></td>

Can anyone help me set the focus to this textbox.

Thanks.Use this JavaScript:


function SetFocusToControl(id) {
document.getElementById(id).focus();
}

In you code, just use the following code:

protected TextBox Defect;

void Page_Load(object sender, EventArgs e) {
Page.RegisterStartUpScript("focusScript", String.Format("<script language=\"JavaScript\">SetFocusToControl('{0}');</script>", Defect.ClientID));
}

0 comments:

Post a Comment