• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 703
  • Last Modified:

Set Focus to a textbox in a DataGrid

This textbox is in a DataGrid (id="grid"):
<asp:TextBox ID="txtEng" Runat=server></asp:TextBox>

Private Sub grid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grid.ItemCommand
      If e.CommandName = "AddEngrav" Then
         Dim productId As String = grid.DataKeys(e.Item.ItemIndex)
         Dim engrave As String = CType(e.Item.FindControl("txtEng"), TextBox).Text.Trim
         If engrave.Length = 0 Then
          Page.RegisterClientScriptBlock("message", "<script language='javascript'>window.onload = function () { alert('Add a message in the box first, then click Add Engraving.'); };</script>")
         
**I want to Set Focus to that textbox here . .  right after the alert box closes.

Is there a way to do this? Thank you
0
MikeMCSD
Asked:
MikeMCSD
  • 6
  • 5
  • 4
  • +1
3 Solutions
 
SThorogoodCommented:
When you construct the client script block, you should be able to add code to set the focus using the Control.UniqueID property of the TextBox.

Something like:

"<script language='javascript'>window.onload = function () { alert('Add a message in the box first, then click Add Engraving.'); document.getElementById('" + CType(e.Item.FindControl("txtEng"), TextBox)).UniqueID + "').focus(); };</script>"
0
 
MikeMCSDAuthor Commented:
Thanks SThorogood  . . I tried this:

Page.RegisterClientScriptBlock("message", "<script language='javascript'>window.onload = function () { alert('Add a message in the box first, then click Add Engraving.'); document.getElementById('" + CType(e.Item.FindControl("txtEng"), TextBox)).UniqueID + "').focus(); };</script>")

But I got this compiler warning:

"operator + is not defined for strings . . . "
0
 
SThorogoodCommented:
sorry - i was thinking in C#

use & for VB strig concatenation i think..
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
MikeMCSDAuthor Commented:
If gives me the same error as the +.
0
 
SThorogoodCommented:
Are you sure? from MSDN:

Dim myStr As String
myStr = "Hello" & " World"   ' Returns "Hello World".

there are 2 instances of the operator - did you change both?
0
 
MikeMCSDAuthor Commented:
Yes. I even tried using "&" before you suggested it.
0
 
SThorogoodCommented:
Mike - this doesn't make any sense. Are you sure it's the exact same error? Are you sure that the file has been saved?

Are you using VS.NET - if so, which line does the editor hilite with regard to the compile error?
0
 
b1xml2Commented:
Control.UniqueID does not work here and relates to the name attribute of the control which differs from browser to browser. Control.ClientID is the id attribute.

This because there are 2 INamingContainers along the way, the DataGrid and the DataGridItem.

Thus the ID of the TextBox will be <DataGrid.ClientID>_<DataGridItem.ClientID>_<TextBox.ClientID> such as grid__ctl2_txtUsers

If you do not explicitly set the ID of the DataGridItem, it will take the pattern _ctl<number>

Thus for consistency across postbacks:

Private Sub grid_ItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles grid.ItemCreated
      Select Case e.Item.ItemType
      Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem, ListItemType.SelectedItem
            e.Item.ID = "Item" & e.Item.ItemIndex.ToString
      Case Else
            e.Item.ID = e.Item.ItemType.ToString
      End Select
End Sub

Private Sub grid_ItemCommand(ByVal sender As Object,ByVal e As DataGridCommandEventArgs) Handles grid.ItemCommand
      If e.CommandName = "AddEngrav" Then
            Dim productId As String = grid.DataKeys(e.Item.ItemIndex)
            Dim engraveBox = CType(e.Item.FindControl("txtEng"),TextBox)
            Dim engrave As String = engraveBox.Text.Trim
            If engrave.Length = 0 Then
                  Dim id As String = String.Format("{0}_{1}_{2}",grid.ClientID,e.Item.ClientID,engraveBox.ClientID)
                  Dim fragment As String = String.Format("alert('{0}'); document.getElementById('{1}').focus();","Add a message in the box first, then click Add Engraving",id)
                  Page.RegisterClientScriptBlock("message", "<script language='javascript'>window.onload = function () {" & fragment & " };</script>")
            End If
            
      End If

End Sub
0
 
MikeMCSDAuthor Commented:
Page.RegisterClientScriptBlock("message", "<script language='javascript'>window.onload = function () { alert('Add a message in the box first, then click Add Engraving.'); document.getElementById('" & CType(e.Item.FindControl("txtEng"), TextBox)).UniqueID & "').focus(); };</script>")

I get a build error "expression expected" and
"Method arguments must be enclosed in parentheses" at the second "&".

The line is underlined in blue, and the tooltip box in yellow says:

"operator & is not defined for 'strings' and System......Texboxes.
0
 
b1xml2Commented:
The UniqueID relates to the name attribute, and the ClientID relates to the id attribute. Now, in the DataGridItem, you cannot get the proper fully qualified id as you will find out by trying out the UniqueID property.

your textbox inside the grid will have the following pattern
grid__ctl<number>_<id_of_textbox>
0
 
SThorogoodCommented:
I think there is an additional parentheses  - it should be:

CType(e.Item.FindControl("txtEng"), TextBox).UniqueID

however, I think that b1xml2 has a point re: the ClientID stuff and his code is also a bit neater.
0
 
MikeMCSDAuthor Commented:
b1xml2  . . thanks .  .  I tried your code, I get the pop-up box, but the focus doesn't
go in the textbox.
0
 
Koala119Commented:
Well, how about create a reusable setfocus function and call it after showing the message? e.g:

...        
 Page.RegisterClientScriptBlock("message", "<script language='javascript'>window.onload = function () { alert('Add a message in the box first, then click Add Engraving.'); };</script>")
         
'**I want to Set Focus to that textbox here . .  right after the alert box closes.
' Put here
Dim ctrl As Textbox= CType(e.Item.FindControl("txtEng"), TextBox)
SetFocus(ctrl)
...

The setfocus function:
==============
Private Sub SetFocus(ByVal ctrl As Control)
    ' Define the JavaScript function for the specified control.
    Dim focusScript As String = "<script language='javascript'>" & _
      "document.getElementById('" + ctrl.ClientID & _
      "').focus();</script>"

    ' Add the JavaScript code to the page.
    Page.RegisterStartupScript("FocusScript", focusScript)
End Sub
0
 
b1xml2Commented:
amendment
========
Private Sub grid_ItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles grid.ItemCreated
     Select Case e.Item.ItemType
     Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem, ListItemType.SelectedItem
          e.Item.ID = "Item" & e.Item.ItemIndex.ToString
     Case Else
          e.Item.ID = e.Item.ItemType.ToString
     End Select
End Sub

Private Sub grid_ItemCommand(ByVal sender As Object,ByVal e As DataGridCommandEventArgs) Handles grid.ItemCommand
     If e.CommandName = "AddEngrav" Then
          Dim productId As String = grid.DataKeys(e.Item.ItemIndex)
          Dim engraveBox = CType(e.Item.FindControl("txtEng"),TextBox)
          Dim engrave As String = engraveBox.Text.Trim
          If engrave.Length = 0 Then
               Dim fragment As String = String.Format("alert('{0}'); document.getElementById('{1}').focus();","Add a message in the box first, then click Add Engraving",engraveBox.ClientID)
               Page.RegisterClientScriptBlock("message", "<script language='javascript'>window.onload = function () {" & fragment & " };</script>")
          End If
         
     End If

End Sub
0
 
b1xml2Commented:
i retract the statement about the INamingContainers, the ClientID property is sufficient to get the fully qualified ID. To prevent problems with dynamic Grid names, we use the method as seen in grid_ItemCreated
0
 
MikeMCSDAuthor Commented:
b1xml2 . . works great. thanks
Koala119, I like that setfocus function code and will probably use
it eventually on another page.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now