MikeMCSD
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. DataGridCo mmandEvent Args) Handles grid.ItemCommand
If e.CommandName = "AddEngrav" Then
Dim productId As String = grid.DataKeys(e.Item.ItemI ndex)
Dim engrave As String = CType(e.Item.FindControl(" txtEng"), TextBox).Text.Trim
If engrave.Length = 0 Then
Page.RegisterClientScriptB lock("mess age", "<script language='javascript'>wind ow.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
<asp:TextBox ID="txtEng" Runat=server></asp:TextBox
Private Sub grid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.
If e.CommandName = "AddEngrav" Then
Dim productId As String = grid.DataKeys(e.Item.ItemI
Dim engrave As String = CType(e.Item.FindControl("
If engrave.Length = 0 Then
Page.RegisterClientScriptB
**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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry - i was thinking in C#
use & for VB strig concatenation i think..
use & for VB strig concatenation i think..
ASKER
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?
Dim myStr As String
myStr = "Hello" & " World" ' Returns "Hello World".
there are 2 instances of the operator - did you change both?
ASKER
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?
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>_<DataG ridItem.Cl ientID>_<T extBox.Cli entID> 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.AlternatingIt em, 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.ItemI ndex)
Dim engraveBox = CType(e.Item.FindControl(" txtEng"),T extBox)
Dim engrave As String = engraveBox.Text.Trim
If engrave.Length = 0 Then
Dim id As String = String.Format("{0}_{1}_{2} ",grid.Cli entID,e.It em.ClientI D,engraveB ox.ClientI D)
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.RegisterClientScriptB lock("mess age", "<script language='javascript'>wind ow.onload = function () {" & fragment & " };</script>")
End If
End If
End Sub
This because there are 2 INamingContainers along the way, the DataGrid and the DataGridItem.
Thus the ID of the TextBox will be <DataGrid.ClientID>_<DataG
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.AlternatingIt
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.ItemI
Dim engraveBox = CType(e.Item.FindControl("
Dim engrave As String = engraveBox.Text.Trim
If engrave.Length = 0 Then
Dim id As String = String.Format("{0}_{1}_{2}
Dim fragment As String = String.Format("alert('{0}'
Page.RegisterClientScriptB
End If
End If
End Sub
ASKER
Page.RegisterClientScriptB lock("mess age", "<script language='javascript'>wind ow.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.
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_t extbox>
your textbox inside the grid will have the following pattern
grid__ctl<number>_<id_of_t
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.
CType(e.Item.FindControl("
however, I think that b1xml2 has a point re: the ClientID stuff and his code is also a bit neater.
ASKER
b1xml2 . . thanks . . I tried your code, I get the pop-up box, but the focus doesn't
go in the textbox.
go in the textbox.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER
b1xml2 . . works great. thanks
Koala119, I like that setfocus function code and will probably use
it eventually on another page.
Koala119, I like that setfocus function code and will probably use
it eventually on another page.
ASKER
Page.RegisterClientScriptB
But I got this compiler warning:
"operator + is not defined for strings . . . "