Results 1 to 11 of 11

Thread: [RESOLVED] [02/03] How to setfocus and highlight all text in datagrid textbox

  1. #1

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Resolved [RESOLVED] [02/03] How to setfocus and highlight all text in datagrid textbox

    Hi All,

    Can Anyone tell me how to setfocus and highlight entire text in datagrid editable textbox ?

    Thanks .
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  2. #2
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    Re: [02/03] How to setfocus and highlight all text in datagrid textbox

    Hi, Can You please Explain bit more elabrately.because you didn't explain where do you need to achieve this. onpageload? or when clicking any button? like this.
    Loving dotnet

  3. #3

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: [02/03] How to setfocus and highlight all text in datagrid textbox

    Thanks,

    When the user clicked the edit button (buttoncolumn) from datagrid. The textbox will add into the cell.So I want to setfocus and highlight  entire text inside the textbox in editcommand.
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  4. #4

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: [02/03] How to setfocus and highlight all text in datagrid textbox

    Harlow anyone here who are willing to assist me ?
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [02/03] How to setfocus and highlight all text in datagrid textbox

    If you have a ButtonColumn, handle the DataGrid's ItemCommand event. In there, look at the e.CommandName and e.CommandArgument to get the row number, then get that corresponding datagrid row.

    Then, in that row, perform a FindControl(), get your textbox, add text to it.

    As for setting focus, you'll need to do that using javascript:

    document.getElementById('yourTextBoxId').focus();

  6. #6

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: [02/03] How to setfocus and highlight all text in datagrid textbox

    Thanks Mendnak,

    Can you review my code ? because it was not working.

    CODE BEHIND
    ASP Code:
    1. Private Sub dgSchoolName_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgSchoolName.ItemCommand
    2.         If e.Item.ItemType = ListItemType.EditItem Then
    3.  
    4.             Dim TxtSchoolName As TextBox = CType(e.Item.Cells(2).FindControl("txtSchoolName"), TextBox)
    5.             e.Item.Cells(2).Attributes.Add("onclick", " document.getElementById('" & TxtSchoolName.ClientID & "').focus()")
    6.         End If
    7.     End Sub

    ---------------------------------

    HTML Code:
    1. <asp:TemplateColumn HeaderText="School Name">
    2.                         <ItemStyle Width="350px"></ItemStyle>
    3.                         <ItemTemplate>
    4.                             <%# DataBinder.Eval(Container.DataItem,"SchoolName") %>
    5.                         </ItemTemplate>
    6.                         <EditItemTemplate>
    7.                             <asp:TextBox id=txtSchoolName runat="server" Width="240px" Text='<%# DataBinder.Eval(Container.DataItem,"SchoolName") %>'>
    8.                             </asp:TextBox>
    9.                         </EditItemTemplate>
    10.                     </asp:TemplateColumn>
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [02/03] How to setfocus and highlight all text in datagrid textbox

    After you 'get' TxtSchoolName, set its text property

    TxtSchoolName .Text = "Hello World"

    On the next line, don't set focus in the onclick. use ClientScript.RegisterStartupScript() to send a

    document.getElementbyId....

    (as you're doing currently) instead, to set the focus.

  8. #8

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: [02/03] How to setfocus and highlight all text in datagrid textbox

    Quote Originally Posted by mendhak
    After you 'get' TxtSchoolName, set its text property

    TxtSchoolName .Text = "Hello World"

    On the next line, don't set focus in the onclick. use ClientScript.RegisterStartupScript() to send a

    document.getElementbyId....

    (as you're doing currently) instead, to set the focus.
    Thanks Sir,

    I had wrote a function as below.

    ASP Code:
    1. Private Const CrLf As String = _
    2.         Microsoft.VisualBasic.Constants.vbCrLf
    3.     Private Const Tab As String = _
    4.         Microsoft.VisualBasic.Constants.vbTab
    5.     Private Const Quote As String = """"
    6.  
    7. Private Sub SetTextBoxFocus(ByVal txtBox As Control)
    8.         Dim sb As New StringBuilder
    9.  
    10.         sb.Append("<script type=")
    11.         sb.Append(Quote)
    12.         sb.Append("text/javascript")
    13.         sb.Append(Quote)
    14.         sb.Append(">")
    15.         sb.Append(CrLf)
    16.         sb.Append("<!--")
    17.         sb.Append(CrLf)
    18.         sb.Append(Tab)
    19.         sb.Append("document.getElementById(")
    20.         sb.Append(Quote)
    21.         sb.Append(txtBox.ClientID)
    22.         sb.Append(Quote)
    23.         sb.Append(").focus();")
    24.         sb.Append(CrLf)
    25.         sb.Append("//-->")
    26.         sb.Append(CrLf)
    27.         sb.Append("</script>")
    28.         sb.Append(CrLf)
    29.  
    30.  
    31.         Page.RegisterClientScriptBlock("SetFocusScript", sb.ToString)
    32.  
    33.     End Sub
    34. Private Sub dgSchoolName_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgSchoolName.ItemCreated
    35.         If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.AlternatingItem Then
    36.             Dim TxtSchoolName As TextBox = CType(e.Item.Cells(2).FindControl("txtSchoolName"), TextBox)
    37.             SetTextBoxFocus(TxtSchoolName)
    38.         End If
    39.     End Sub

    But It could not work also. I'm sorry because of my stupidness.

    I need to do findcontrol in which event handle within datagrid because I guess the textbox control has not created after I executed the function of setfocus because I getting 'Object reference not set to an instance of an object' Error in this case.

    Thanks :-)
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  9. #9

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: [02/03] How to setfocus and highlight all text in datagrid textbox

    PROBLEM RESOLVED

    ASP Code:
    1. Private Sub dgSchoolName_EditCommand1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgSchoolName.EditCommand
    2.  
    3.         Me.dgSchoolName.EditItemIndex = e.Item.ItemIndex
    4.  
    5.         '    .DataSource = Nothing
    6.         dgSchoolName.DataSource = Session("Form1Ds").Tables("SchoolName")
    7.         dgSchoolName.DataBind()
    8.  
    9.         Dim descTB As TextBox
    10.        
    11.  
    12.         descTB = dgSchoolName.Items(e.Item.ItemIndex).Cells(2).FindControl("txtSchoolName")
    13.  
    14.  
    15.         Page.RegisterStartupScript("focus", "<script language=""JavaScript"">" & vbCrLf & _
    16.              vbTab & "Form1." & descTB.ClientID & ".focus();" & _
    17.              vbCrLf & vbTab & "Form1." & descTB.ClientID & ".select();" & _
    18.              vbCrLf & "<" & "/script>")
    19.  
    20.     End Sub

    Now I got one extra question. except rebind the data got any others method to solve my problem? Is it keep bind the data will implicating speed of page?

    Thanks .
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [02/03] How to setfocus and highlight all text in datagrid textbox

    Good work on solving it. What happens if you don't rebind each time? Yes, there will be speed implications here but it shouldn't be that bad.

  11. #11

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: [02/03] How to setfocus and highlight all text in datagrid textbox

    Thanks mendhak you are my great teacher :-)
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width