Link to home
Start Free TrialLog in
Avatar of MikeMCSD
MikeMCSDFlag for United States of America

asked on

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
SOLUTION
Avatar of SThorogood
SThorogood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MikeMCSD

ASKER

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 . . . "
Avatar of SThorogood
SThorogood

sorry - i was thinking in C#

use & for VB strig concatenation i think..
If gives me the same error as the +.
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?
Yes. I even tried using "&" before you suggested it.
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?
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
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.
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>
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.
b1xml2  . . thanks .  .  I tried your code, I get the pop-up box, but the focus doesn't
go in the textbox.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
b1xml2 . . works great. thanks
Koala119, I like that setfocus function code and will probably use
it eventually on another page.