[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 .
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.
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.
Re: [02/03] How to setfocus and highlight all text in datagrid textbox
Harlow anyone here who are willing to assist me ?
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();
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:
Private Sub dgSchoolName_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgSchoolName.ItemCommand
If e.Item.ItemType = ListItemType.EditItem Then
Dim TxtSchoolName As TextBox = CType(e.Item.Cells(2).FindControl("txtSchoolName"), TextBox)
e.Item.Cells(2).Attributes.Add("onclick", " document.getElementById('" & TxtSchoolName.ClientID & "').focus()")
End If
End Sub
---------------------------------
HTML Code:
<asp:TemplateColumn HeaderText="School Name">
<ItemStyle Width="350px"></ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"SchoolName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=txtSchoolName runat="server" Width="240px" Text='<%# DataBinder.Eval(Container.DataItem,"SchoolName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
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.
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:
Private Const CrLf As String = _
Microsoft.VisualBasic.Constants.vbCrLf
Private Const Tab As String = _
Microsoft.VisualBasic.Constants.vbTab
Private Const Quote As String = """"
Private Sub SetTextBoxFocus(ByVal txtBox As Control)
Dim sb As New StringBuilder
sb.Append("<script type=")
sb.Append(Quote)
sb.Append("text/javascript")
sb.Append(Quote)
sb.Append(">")
sb.Append(CrLf)
sb.Append("<!--")
sb.Append(CrLf)
sb.Append(Tab)
sb.Append("document.getElementById(")
sb.Append(Quote)
sb.Append(txtBox.ClientID)
sb.Append(Quote)
sb.Append(").focus();")
sb.Append(CrLf)
sb.Append("//-->")
sb.Append(CrLf)
sb.Append("</script>")
sb.Append(CrLf)
Page.RegisterClientScriptBlock("SetFocusScript", sb.ToString)
End Sub
Private Sub dgSchoolName_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgSchoolName.ItemCreated
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim TxtSchoolName As TextBox = CType(e.Item.Cells(2).FindControl("txtSchoolName"), TextBox)
SetTextBoxFocus(TxtSchoolName)
End If
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 :-)
Re: [02/03] How to setfocus and highlight all text in datagrid textbox
PROBLEM RESOLVED
ASP Code:
Private Sub dgSchoolName_EditCommand1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgSchoolName.EditCommand
Me.dgSchoolName.EditItemIndex = e.Item.ItemIndex
' .DataSource = Nothing
dgSchoolName.DataSource = Session("Form1Ds").Tables("SchoolName")
dgSchoolName.DataBind()
Dim descTB As TextBox
descTB = dgSchoolName.Items(e.Item.ItemIndex).Cells(2).FindControl("txtSchoolName")
Page.RegisterStartupScript("focus", "<script language=""JavaScript"">" & vbCrLf & _
vbTab & "Form1." & descTB.ClientID & ".focus();" & _
vbCrLf & vbTab & "Form1." & descTB.ClientID & ".select();" & _
vbCrLf & "<" & "/script>")
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 .
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.
Re: [02/03] How to setfocus and highlight all text in datagrid textbox
Thanks mendhak you are my great teacher :-)