Setting Focus to a TextBox in an EditItemTemplate

This demo illustrates how to set the cursor's focus to a TextBox in the EditItemTemplate.

 FAQ IDQuestion
Edit2 How do you convert a string to all uppercase or all lowercase?
Edit12 How can I display line breaks in an email message?
Edit14 Can I send emails without using CDONTS?
Edit15 How can I determine the length of a string (how many characters are in a string)?
Edit17 How can I find the position of a certain character (or substring) within a string?
Edit19 How can I return the current date/time?
Edit20 What is recursion?
Edit23 What, exactly, is the FileSystemObject?
Edit25 How can I send an email using CDONTS?
Edit8 How do you create a database table through a SQL statement (via an ASP page, perhaps)?
Edit16 Using VBScript, how can I determine if a variable is a valid date or not?
Edit18 How can I easily display a nicely formatted date variable?
Edit24 How can I read the contents of a text file through an ASP page?
Edit7 How do I dynamically resize an array?
Edit11 How can I have my ASP page send HTML-formatted email?
Edit10 How can I tell what version of ADO I am using?
Edit13 How do you send email attachments through an ASP page?
Edit21 How can I determine the upper or lower bounds of an array?
Edit22 How can I validate an email adress using Jscript?

Source Code
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
  'Create a connection
  Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
  Dim ddlDataSet as DataSet = New DataSet()

  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
    End If
  End Sub

  Sub BindData()
    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "SELECT FAQID, F.Description FROM tblFAQ F WHERE FAQID <= 25"

    'Set the datagrid's datasource to the datareader and databind
    Dim resultsDataSet as New DataSet()
    Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection) 

    dgPopularFAQs.DataSource = resultsDataSet
  End Sub

  Sub dgPopularFAQs_Edit(sender as Object, e as DataGridCommandEventArgs)
    dgPopularFAQs.EditItemIndex = e.Item.ItemIndex
    ' Create a reference to the TextBox
    Dim descTB as TextBox
    descTB = dgPopularFAQs.Items(e.Item.ItemIndex).Cells(2).FindControl("txtDesc")

    'Set the script to focus and select the TextBox
    RegisterStartupScript("focus", "<script language=""JavaScript"">" & vbCrLf & _
         vbTab & "frmEditFAQs." & descTB.ClientID & ".focus();" & _
         vbCrLf & vbTab & "frmEditFAQs." & descTB.ClientID & ".select();" & _
         vbCrLf & "<" & "/script>")
  End Sub
  Sub dgPopularFAQs_Cancel(sender as Object, e as DataGridCommandEventArgs)
    dgPopularFAQs.EditItemIndex = -1
  End Sub
  Sub dgPopularFAQs_Update(sender as Object, e as DataGridCommandEventArgs)
    ' Create a reference to the TextBox
    Dim descTB as TextBox
    descTB = dgPopularFAQs.Items(e.Item.ItemIndex).Cells(2).FindControl("txtDesc")

    'Display what would have happened in the lblResults label
    lblResults.Text = "You opted to update the Description field to: " & descTB.Text    

    dgPopularFAQs.EditItemIndex = -1
  End Sub

<form runat="server" id="frmEditFAQs">
  <asp:label id="lblResults" Font-Bold="True" runat="server"
  	Font-Italic="True" ForeColor="#aa0000" />
  <asp:datagrid id="dgPopularFAQs" runat="server"

  	<asp:EditCommandColumn EditText="Edit" UpdateText="Update" CancelText="Cancel" />
  	<asp:BoundColumn DataField="FAQID" HeaderText="FAQ ID"
  	     ItemStyle-HorizontalAlign="Center" ReadOnly="True" />
  	<asp:TemplateColumn HeaderText="Question">
  	    <%# DataBinder.Eval(Container.DataItem, "Description") %>
  	    <asp:TextBox id="txtDesc" runat="server"
  	           Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>'
  	           Width="95%" />

[Return to the article]