Javascript focus applied to a gridview cell textbox

Posted on 2007-10-15
Last Modified: 2010-05-18
Given the below javascript on a plain page:
"<script>document.getElementById('" + TextBox1.ClientID + "').focus();</script>"
how can it be applied in a gridview rowcreated event for TextBox1 in a cell?
Question by:ksfok
    LVL 26

    Accepted Solution

    try in rowcreated event
    if e.Row.RowType = DataControlRowType.DataRow then
    Dim mytextbox as textbox = ctype(e.Row.Cells(3).FindControl("txtTextBox"), textbox)
    page.clientscript.RegisterClientScriptBlock(me.gettype(), "setfocus", "document.getElementById('" + mytextbox.clientid + "').focus();")
    end if

    LVL 27

    Assisted Solution

          if (e.Row.RowType == DataControlRowType.DataRow)

    if (e.Row.RowIndex == 0)
    TextBox tempTextBox = (TextBox)e.Row.FindControl("Textbox1") as TextBox;
     System.Text.StringBuilder sb = new System.Text.StringBuilder(@"window.onload= function()
    {document.getElementById('" + tempTextBox.ClientID + "').focus();\n}\t\n");
     Page.ClientScript.RegisterStartupScript(this.GetType(), "CtrlFocus", sb.ToString(),true);

    this will see the focus on the first TextBox on the row

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now