When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML
Information:
Advertise
Feedback
Author an Article
Jobs
















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this Page!

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Monday, September 9, 2002

An Extensive Examination of the DataGrid Web Control: Part 9, Part 2
By Scott Mitchell


  • Read Part 1

  • In Part 1 we looked at what is needed to set the focus to an HTML input field - namely, some client-side code that references the input field and calls its focus() method. We also took a quick look at our DataGrid's declaration. In this part we'll look at how to dynamically add the needed client-side code to the ASP.NET Web page when a row is selected for editing!

    - continued -

    Dynamically Adding Client-Side Code to an ASP.NET Web Page
    When one of the rows of the DataGrid is selected for editing, we need some way to insert client-side code. Recall that we want to do this all within a client-side JavaScript function. There are two ways to do this. One way is to hard-code in the JavaScript function, having it's body be an ASP.NET Literal control. That is, we'd have something like:

    ...
    
    <html>
    <head>
      <script language="javascript">
        function setFocusIfNeeded()
        {
          <asp:literal id="ltlScript" runat="server" />
        }
      </script>
    </head>
    <body onload="setFocusIfNeeded();">
    
    ...
    

    Note that here the setFocusIfNeeded() JavaScript function will be called on every page load. For those times that we wish to have some sort of client-side action occur, we'd simply set the ltlScript control's Text property to the script we wished to have execute.

    Another way to accomplish this is to use the Page class's RegisterStartupScript method. This method takes two string parameters, a key and the client-side script to emit. It then adds a client-side script block at the end of the ASP.NET page and includes in it the client-side script provided. With this approach we would not use an onload event handler as we did above - rather, we'd only call the RegisterStartupScript method when the focus needed to be set (since the client-side JavaScript would only be emitted when this method call was made).

    I will use the latter approach - personally I find it to be cleaner and more readable.

    When the "Edit" Button is Clicked...
    When the "Edit" button is clicked we need to somehow emit client-side code that will set the focus to the TextBox in the EditItemTemplate. As we saw in Part 6, the DataGrid exposes a EditCommand event that fires whenever the "Edit" button is clicked. We can then provide an event handler for this EditCommand event.

    Inside of this event handler we want to, as always, set the DataGrid's EditItemIndex property and rebind the DataGrid. Furthermore, after we've performed these first two steps, we want to emit the necessary client-side code to set the focus to the EditItemTemplate's TextBox. In order to set the focus we need to grab the Web form's ID and the TextBox's ClientID. We can then emit JavaScript like: FormID.TextBoxID.focus();.

    Below you can see the code for the DataGrid's EditCommand event handler.

    Sub dgPopularFAQs_Edit(sender as Object, e as DataGridCommandEventArgs)
      dgPopularFAQs.EditItemIndex = e.Item.ItemIndex
      BindData()
        
      ' 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
    
    [View a Live Demo!]

    Notice that we need to retrieve the ClientID of the TextBox in the EditItemTemplate. Hence, immediately after setting the DataGrid's EditItemIndex property and calling the BindData() method (the first two lines of the event handler), we create a local variable named descTB and set it equal to the TextBox control in the 3rd column of the DataGrid row that is being edited. We use the FindControl method to find the control named txtDesc, which is the name of our TextBox control in the EditItemTemplate.

    You may be wondering why we are using dgPopularFAQs.Items(e.Item.ItemIndex).Cells(2).FindControl("txtDesc") and not just e.Item.Cells(2).FindControl("txtDesc"). This is because when the e.Item DataGridItem instance was passed in, the row was not in its editing form, meaning that the TextBox specified in the EditItemTemplate is not present. After we call the BindData() method (which rebinds the DataGrid, calling the DataBind() method) the e.Item DataGridItem instance still refers to the old, non-editable form. Hence, we have to work directly with the DataGrid's Items property, extracting the correct DataGridItem instance (which is given by e.Item.ItemIndex).

    Once we have a reference to this TextBox control, we can go ahead and emit the client-side JavaScript code using the RegisterStartupScript method. Note that we have to include the actual script tags, and that the closing script must be broken up into two strings (i.e., "<" & "/script>"). If it is not, the ASP.NET parsing engine will be confused when it reaches that closing script block and generate an error.

    In the RegisterStartupScript method you may have noticed that we are referencing the TextBox's ClientID property as opposed to its ID property. This is because the TextBox control is a child control of the DataGrid row and the DataGrid row is a child of the DataGrid. With each increasing child layer, ASP.NET augments the UniqueID of the controls, prefixing it with the parent's ID. For example, our TextBox's ID property is txtDesc (the value we gave it in our DataGrid's declaration), but it's UniqueID is something like: dgPopularFAQs:_ctrl4:txtDesc. The ClientID, which is the property we're interested in, is the actual value of the rendered HTML tag's id property, which is the UniqueID with colons replaced with underscores. That is, the ClientID for the TextBox would be something like: dgPopularFAQs__ctrl4_txtDesc.

    The actual JavaScript emitted by the RegisterStartupScript method looks something like:

    <script language="JavaScript">
    	frmEditFAQs.dgPopularFAQs__ctl12_txtDesc.focus();
    	frmEditFAQs.dgPopularFAQs__ctl12_txtDesc.select();
    </script>
    

    The select() call is optional - it simply highlights all of the text in the TextBox. Feel free to omit this call if so desired. Take a moment to try out the live demo and observe the behavior when a row's "Edit" button is clicked. Also, you are encouraged to do some View/Sources on the live demo so as to see the emitted JavaScript block as well as the id and name attributes of the HTML rendered for the TextBox in the EditItemTemplate.

    Conclusion
    In this article we examined how to programmatically add some client-side code to the DataGrid's EditCommand event in order to have a TextBox in the EditItemTemplate receive focus when the "Edit" button for a row is clicked.

    Happy Programming!

  • By Scott Mitchell


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES