ASP.Net/C# - Datagrid Textbox controls - want to set focus to first control

Posted on 2006-04-23
Last Modified: 2012-05-05
Hello all.  On my page load after the page rendes I have a datagrid that has rows going across with 4 textbox columns.  I want to set focus to the first control in the 1st datagrid row.  How can I do this after the page loads?  Thanks all
Question by:sbornstein2
    LVL 5

    Accepted Solution

    you can reach controls inside your datagrid like this:
    foreach(DataGridItem dgi in myDataGrid.Items)

    TextBox myTextBox = (TextBox) (dgi.Cells[0].Controls[1]);

    ListBox myListBox = (ListBox) (dgi.Cells[1].Controls[1]);

    DropDownList myList = (DropDownList) (dgi.Cells[2].Controls[1]);

    CheckBox myCheckBox = (CheckBox) (dgi.Cells[3].Controls[1]);


    string a = myListBox.SelectedValue;

    if(a != null && a != "")







    then you can find the right control/textbox you want to focus and then execute this method:

    this is the method:
    public static void SetFocus(Control control)
        StringBuilder sb = new StringBuilder();
        sb.Append("\r\n<script language='JavaScript'>\r\n");
        sb.Append("function SetFocus()\r\n");
        Control p = control.Parent;
        while (!(p is System.Web.UI.HtmlControls.HtmlForm)) p = p.Parent;
        sb.Append("window.onload = SetFocus;\r\n");
        sb.Append("// -->\r\n");
        control.Page.RegisterClientScriptBlock("SetFocus", sb.ToString());

    Author Comment

    yes I am getting the textbox control in actually my itemdatabound can I just do it there but I guess I dont want it to do it for each row everytime

    Author Comment

    thats perfect I have it now.  One more question is there a way not only the focus goes into the box but there are default 0's in the box can I highlight the whole entry in the box if that makes sense?  I am now in my itemdatabound just saying:

          //Set Focus to first Textbox Column
                            if(e.Item.ItemIndex == 0)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 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