Control Focus and DataList

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerry Camel

    Control Focus and DataList

    Once the datalist is in edit mode and the text boxes and all happily appear,
    how can I force the textbox to be the control with focus, so the user can
    start editing without having to click the textbox first? Thanks.

    Jerry


  • Jay Douglas

    #2
    Re: Control Focus and DataList

    In the past, I have put the following code in the ItemDataBound event of the
    text box.

    Where TextBoxToFocusI d in the text box id in the datagrid

    private void DataGrid1_ItemD ataBound(object sender,
    System.Web.UI.W ebControls.Data GridItemEventAr gs e)
    {
    if (e.Item.ItemTyp e == ListItemType.Ed itItem)
    {
    TextBox txt = e.Item.FindCont rol("TextBoxToF ocusId");
    RegisterClientS criptBlock("Dat aGridFocus",
    string.Format(" <script
    language=\"java script\">docume nt.getElementBy Id('{0}').focus ();</script>",
    txt.ClientID));
    }
    }

    --
    Jay Douglas



    "Jerry Camel" <rlrcstr@msn.co m> wrote in message
    news:O6QxFLg6FH A.2176@TK2MSFTN GP14.phx.gbl...[color=blue]
    > Once the datalist is in edit mode and the text boxes and all happily
    > appear,
    > how can I force the textbox to be the control with focus, so the user can
    > start editing without having to click the textbox first? Thanks.
    >
    > Jerry
    >
    >[/color]


    Comment

    • Jerry Camel

      #3
      Re: Control Focus and DataList

      Here's the problem... The Datalist control renders the code and assigns IDs
      and names to all the controls. So you don't know the name of the control
      that needs the focus until the html is rendered. And the name will be
      different for every line in the datalist.

      For instance, I named the textbox "txtName", but the ID when rendered is
      something like "dlEntities__ct l4_txtName" and if I click a different record
      to edit, the name would be different. How can you tell what control to set
      the focus to?

      Thanks.


      "Jay Douglas" <jayREMOVEIFNOT SPAM@jaydouglas .com> wrote in message
      news:u$IKNgg6FH A.3636@TK2MSFTN GP09.phx.gbl...[color=blue]
      > In the past, I have put the following code in the ItemDataBound event of[/color]
      the[color=blue]
      > text box.
      >
      > Where TextBoxToFocusI d in the text box id in the datagrid
      >
      > private void DataGrid1_ItemD ataBound(object sender,
      > System.Web.UI.W ebControls.Data GridItemEventAr gs e)
      > {
      > if (e.Item.ItemTyp e == ListItemType.Ed itItem)
      > {
      > TextBox txt = e.Item.FindCont rol("TextBoxToF ocusId");
      > RegisterClientS criptBlock("Dat aGridFocus",
      > string.Format(" <script
      > language=\"java script\">docume nt.getElementBy Id('{0}').focus ();</script>",
      > txt.ClientID));
      > }
      > }
      >
      > --
      > Jay Douglas
      > http://www.jaydouglas.com
      >
      >
      > "Jerry Camel" <rlrcstr@msn.co m> wrote in message
      > news:O6QxFLg6FH A.2176@TK2MSFTN GP14.phx.gbl...[color=green]
      > > Once the datalist is in edit mode and the text boxes and all happily
      > > appear,
      > > how can I force the textbox to be the control with focus, so the user[/color][/color]
      can[color=blue][color=green]
      > > start editing without having to click the textbox first? Thanks.
      > >
      > > Jerry
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Jay Douglas

        #4
        Re: Control Focus and DataList

        Per the example below, you on the ItemDataBound event, you can use:

        TextBox txt = (TextBox) e.Item.FindCont rol("txtName");

        then txt.ClientID will return the full control name, ie:
        dlEntities__ctl 4_txtName

        --
        Jay Douglas



        "Jerry Camel" <rlrcstr@msn.co m> wrote in message
        news:ekqF$Ei6FH A.3876@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Here's the problem... The Datalist control renders the code and assigns
        > IDs
        > and names to all the controls. So you don't know the name of the control
        > that needs the focus until the html is rendered. And the name will be
        > different for every line in the datalist.
        >
        > For instance, I named the textbox "txtName", but the ID when rendered is
        > something like "dlEntities__ct l4_txtName" and if I click a different
        > record
        > to edit, the name would be different. How can you tell what control to
        > set
        > the focus to?
        >
        > Thanks.
        >
        >
        > "Jay Douglas" <jayREMOVEIFNOT SPAM@jaydouglas .com> wrote in message
        > news:u$IKNgg6FH A.3636@TK2MSFTN GP09.phx.gbl...[color=green]
        >> In the past, I have put the following code in the ItemDataBound event of[/color]
        > the[color=green]
        >> text box.
        >>
        >> Where TextBoxToFocusI d in the text box id in the datagrid
        >>
        >> private void DataGrid1_ItemD ataBound(object sender,
        >> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
        >> {
        >> if (e.Item.ItemTyp e == ListItemType.Ed itItem)
        >> {
        >> TextBox txt = e.Item.FindCont rol("TextBoxToF ocusId");
        >> RegisterClientS criptBlock("Dat aGridFocus",
        >> string.Format(" <script
        >> language=\"java script\">docume nt.getElementBy Id('{0}').focus ();</script>",
        >> txt.ClientID));
        >> }
        >> }
        >>
        >> --
        >> Jay Douglas
        >> http://www.jaydouglas.com
        >>
        >>
        >> "Jerry Camel" <rlrcstr@msn.co m> wrote in message
        >> news:O6QxFLg6FH A.2176@TK2MSFTN GP14.phx.gbl...[color=darkred]
        >> > Once the datalist is in edit mode and the text boxes and all happily
        >> > appear,
        >> > how can I force the textbox to be the control with focus, so the user[/color][/color]
        > can[color=green][color=darkred]
        >> > start editing without having to click the textbox first? Thanks.
        >> >
        >> > Jerry
        >> >
        >> >[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Jerry Camel

          #5
          Re: Control Focus and DataList

          Figured it out right after I sent that note. Thanks for the info - much
          appreciated.



          "Jay Douglas" <jayREMOVEIFNOT SPAM@jaydouglas .com> wrote in message
          news:eBt6jVs6FH A.2888@tk2msftn gp13.phx.gbl...[color=blue]
          > Per the example below, you on the ItemDataBound event, you can use:
          >
          > TextBox txt = (TextBox) e.Item.FindCont rol("txtName");
          >
          > then txt.ClientID will return the full control name, ie:
          > dlEntities__ctl 4_txtName
          >
          > --
          > Jay Douglas
          > http://www.jaydouglas.com
          >
          >
          > "Jerry Camel" <rlrcstr@msn.co m> wrote in message
          > news:ekqF$Ei6FH A.3876@TK2MSFTN GP09.phx.gbl...[color=green]
          > > Here's the problem... The Datalist control renders the code and assigns
          > > IDs
          > > and names to all the controls. So you don't know the name of the[/color][/color]
          control[color=blue][color=green]
          > > that needs the focus until the html is rendered. And the name will be
          > > different for every line in the datalist.
          > >
          > > For instance, I named the textbox "txtName", but the ID when rendered is
          > > something like "dlEntities__ct l4_txtName" and if I click a different
          > > record
          > > to edit, the name would be different. How can you tell what control to
          > > set
          > > the focus to?
          > >
          > > Thanks.
          > >
          > >
          > > "Jay Douglas" <jayREMOVEIFNOT SPAM@jaydouglas .com> wrote in message
          > > news:u$IKNgg6FH A.3636@TK2MSFTN GP09.phx.gbl...[color=darkred]
          > >> In the past, I have put the following code in the ItemDataBound event[/color][/color][/color]
          of[color=blue][color=green]
          > > the[color=darkred]
          > >> text box.
          > >>
          > >> Where TextBoxToFocusI d in the text box id in the datagrid
          > >>
          > >> private void DataGrid1_ItemD ataBound(object sender,
          > >> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
          > >> {
          > >> if (e.Item.ItemTyp e == ListItemType.Ed itItem)
          > >> {
          > >> TextBox txt = e.Item.FindCont rol("TextBoxToF ocusId");
          > >> RegisterClientS criptBlock("Dat aGridFocus",
          > >> string.Format(" <script
          > >>[/color][/color][/color]
          language=\"java script\">docume nt.getElementBy Id('{0}').focus ();</script>",[color=blue][color=green][color=darkred]
          > >> txt.ClientID));
          > >> }
          > >> }
          > >>
          > >> --
          > >> Jay Douglas
          > >> http://www.jaydouglas.com
          > >>
          > >>
          > >> "Jerry Camel" <rlrcstr@msn.co m> wrote in message
          > >> news:O6QxFLg6FH A.2176@TK2MSFTN GP14.phx.gbl...
          > >> > Once the datalist is in edit mode and the text boxes and all happily
          > >> > appear,
          > >> > how can I force the textbox to be the control with focus, so the user[/color]
          > > can[color=darkred]
          > >> > start editing without having to click the textbox first? Thanks.
          > >> >
          > >> > Jerry
          > >> >
          > >> >
          > >>
          > >>[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...