Setting Focus in DataGrid - Solution

Discussion in 'ASP .Net Datagrid Control' started by Kubuli John, Apr 30, 2004.

  1. Kubuli John

    Kubuli John Guest

    I googled for a solution to this and didn't find a clear one, so I
    thought I'd pass along what I ended up with.

    In the PreRender event of the DataGrid, I execute the following code:

    private void myGrid_PreRender(object sender, System.EventArgs e)
    {
    foreach (DataGridItem item in myGrid.Items)
    {
    foreach (Control ctl in item.Cells[0].Controls)
    {
    if (ctl.GetType() == typeof(System.Web.UI.WebControls.TextBox))
    {
    controlToFocus = ctl.UniqueID;
    return;
    }
    }
    }
    }

    In this method, controlToFocus is a page-level protected string. You
    can customize the innermost foreach and if statements to find any
    particular control you want to set the focus to - this example sets
    the focus on the first TextBox it finds in the first column. Since I
    only have TextBoxes when I'm ready to add or edit, this is fine.

    Then, in the html, I have the following bits:

    function setFocus()
    {
    ctl = "<%= controlToFocus %>";
    if (ctl != "")
    {
    document.forms("my_form").elements(ctl).focus();
    }
    }

    And:

    <body onload="setFocus();">

    Hope you find this useful...

    John H.
    Kubuli John, Apr 30, 2004
    #1
    1. Advertising

  2. Excellent, John.

    I've done alot of dirty workarounds to achive similar functionality.
    Additionally, I would consider to emit the clientside-script using
    Page.RegisterClientScriptBlock and Page.RegisterStartupScript:

    Page.RegisterClientScriptBlock("SetFocus","<script
    language=JavaScript>function setFocus(ctl,formname){if (ctl !=
    ""){document.forms("my_form").elements(ctl).focus();}}</script>");
    Page.RegisterStartupScript("SetFocusStartup","<script
    language=JavaScript>SetFocus('"+controlToFocus+"','my_form');</script>");

    Or, maybe even better, encapsulate the entire thing in a custom control :)

    rgds
    Øystein Fallo


    "Kubuli John" <> wrote in message
    news:...
    > I googled for a solution to this and didn't find a clear one, so I
    > thought I'd pass along what I ended up with.
    >
    > In the PreRender event of the DataGrid, I execute the following code:
    >
    > private void myGrid_PreRender(object sender, System.EventArgs e)
    > {
    > foreach (DataGridItem item in myGrid.Items)
    > {
    > foreach (Control ctl in item.Cells[0].Controls)
    > {
    > if (ctl.GetType() == typeof(System.Web.UI.WebControls.TextBox))
    > {
    > controlToFocus = ctl.UniqueID;
    > return;
    > }
    > }
    > }
    > }
    >
    > In this method, controlToFocus is a page-level protected string. You
    > can customize the innermost foreach and if statements to find any
    > particular control you want to set the focus to - this example sets
    > the focus on the first TextBox it finds in the first column. Since I
    > only have TextBoxes when I'm ready to add or edit, this is fine.
    >
    > Then, in the html, I have the following bits:
    >
    > function setFocus()
    > {
    > ctl = "<%= controlToFocus %>";
    > if (ctl != "")
    > {
    > document.forms("my_form").elements(ctl).focus();
    > }
    > }
    >
    > And:
    >
    > <body onload="setFocus();">
    >
    > Hope you find this useful...
    >
    > John H.
    Øystein Fallo, Apr 30, 2004
    #2
    1. Advertising

  3. Check out this articles,
    http://aspalliance.com/aldotnet/examples/dgautoscroll.aspx
    http://www.dotnetjunkies.com/howto/default.aspx?id=38


    --
    Saravana
    Microsoft MVP - ASP.NET
    www.extremeexperts.com



    "Kubuli John" <> wrote in message
    news:...
    > I googled for a solution to this and didn't find a clear one, so I
    > thought I'd pass along what I ended up with.
    >
    > In the PreRender event of the DataGrid, I execute the following code:
    >
    > private void myGrid_PreRender(object sender, System.EventArgs e)
    > {
    > foreach (DataGridItem item in myGrid.Items)
    > {
    > foreach (Control ctl in item.Cells[0].Controls)
    > {
    > if (ctl.GetType() == typeof(System.Web.UI.WebControls.TextBox))
    > {
    > controlToFocus = ctl.UniqueID;
    > return;
    > }
    > }
    > }
    > }
    >
    > In this method, controlToFocus is a page-level protected string. You
    > can customize the innermost foreach and if statements to find any
    > particular control you want to set the focus to - this example sets
    > the focus on the first TextBox it finds in the first column. Since I
    > only have TextBoxes when I'm ready to add or edit, this is fine.
    >
    > Then, in the html, I have the following bits:
    >
    > function setFocus()
    > {
    > ctl = "<%= controlToFocus %>";
    > if (ctl != "")
    > {
    > document.forms("my_form").elements(ctl).focus();
    > }
    > }
    >
    > And:
    >
    > <body onload="setFocus();">
    >
    > Hope you find this useful...
    >
    > John H.
    Saravana [MVP], May 5, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bonj
    Replies:
    1
    Views:
    2,056
    John M Deal
    Nov 13, 2004
  2. Craig G
    Replies:
    2
    Views:
    385
    Saravana
    Dec 15, 2004
  3. Replies:
    1
    Views:
    4,838
    =?Utf-8?B?c29jaWV0b3BpYS5uZXQ=?=
    Jul 15, 2005
  4. Jason
    Replies:
    4
    Views:
    1,618
    Jason
    May 7, 2007
  5. Roger
    Replies:
    3
    Views:
    322
Loading...

Share This Page