|
-
Feb 3rd, 2009, 04:39 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Feb 3rd, 2009, 07:06 AM
#2
Fanatic Member
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.
-
Feb 3rd, 2009, 07:16 AM
#3
Thread Starter
Hyperactive Member
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.
-
Feb 3rd, 2009, 07:03 PM
#4
Thread Starter
Hyperactive Member
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.
-
Feb 4th, 2009, 04:26 PM
#5
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();
-
Feb 6th, 2009, 04:22 AM
#6
Thread Starter
Hyperactive Member
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>
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.
-
Feb 6th, 2009, 05:51 PM
#7
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.
-
Feb 8th, 2009, 09:03 PM
#8
Thread Starter
Hyperactive Member
Re: [02/03] How to setfocus and highlight all text in datagrid textbox
 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 :-)
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.
-
Feb 8th, 2009, 11:15 PM
#9
Thread Starter
Hyperactive Member
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 .
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.
-
Feb 10th, 2009, 06:50 AM
#10
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.
-
Feb 10th, 2009, 07:01 PM
#11
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|