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

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
sbornstein2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Darth_helgeCommented:
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]);

str.Append(myTextBox.Text);

string a = myListBox.SelectedValue;

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

{

str.Append(myListBox.SelectedItem.Text);

}



str.Append(myList.SelectedItem.Text);

str.Append(myCheckBox.Checked);



}

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

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("<!--\r\n");
    sb.Append("function SetFocus()\r\n");
    sb.Append("{\r\n");
    sb.Append("\tdocument.");
 
    Control p = control.Parent;
    while (!(p is System.Web.UI.HtmlControls.HtmlForm)) p = p.Parent;
 
    sb.Append(p.ClientID);
    sb.Append("['");
    sb.Append(control.UniqueID);
    sb.Append("'].focus();\r\n");
    sb.Append("}\r\n");
    sb.Append("window.onload = SetFocus;\r\n");
    sb.Append("// -->\r\n");
    sb.Append("</script>");
 
    control.Page.RegisterClientScriptBlock("SetFocus", sb.ToString());
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sbornstein2Author Commented:
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
0
sbornstein2Author Commented:
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)
                              SetFocus(tb1);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.