Asp.Net ve Focus

11.Temmuz.2007 Javascript Asp.net

Webform larında bulunan kontrollere odaklanmak için bir sürü yöntem varmış inceledik;

Asp.Net 2.0 ile gelen 2 yeni form özelliği

//odaklanacak bir TextBox ise
<form id="form1" runat="server" defaultfocus
="txtSname">
//Button ise
<form id="form1" runat="server" defaultbutton
="btnSend">

//Asp.Net 1.1 olsaydı
<body onload
="document.form1.txtSname.focus()">
//yada
<body onload
="document.form1.txtSname.select()">

Programatik yapmak istersek

protected void Page_Load(object sender, EventArgs e)
{
   if
(!IsPostBack)
   {
      xtSname.Focus();
      //yada
      
SetFocus(txtSname);
      //yada
      RegisterStartupScript("sFocus", "<scrip>document.getElementById('" + txtSname.ClientID + "').focus();</script>");

   }
}