|
-
Jun 23rd, 2005, 09:33 PM
#1
Thread Starter
Frenzied Member
Datagrid Template Column
I am using a datagrid with a "Bound" column, a "Template" column and an "EditCommand" column.
The "EditCommand" column has an "Edit" button which when clicked shows a text box in the "Template" column which the user can enter text into.
How do you set the focus to the text box when it appears?
I have used Javascript like this:
Code:
<script language="javascript">document.getElementById('TextBox1').focus();</script>
but it does not work in this case.
-
Jun 24th, 2005, 03:35 AM
#2
Frenzied Member
Re: Datagrid Template Column
Check that the client-side ID is "TextBox1". As it is inside a DataGrid I'm sure its client-side ID is different to the server-side one. You can use the ClientID property of the control server-side to get what the client-side ID will be.
HTH
DJ
If I have been helpful please rate my post. If I haven't tell me!
-
Jun 24th, 2005, 04:06 AM
#3
Thread Starter
Frenzied Member
Re: Datagrid Template Column
That worked except that it allocates a different ClientID to each index.
eg. It was called DataGrid1__ctl2_txtQuantity at index 0 and
DataGrid1__ctl3_txtQuantity at index 1
how can I determine this on the fly?
-
Jun 24th, 2005, 04:36 AM
#4
Frenzied Member
Re: Datagrid Template Column
OK what you need to do is this...
When the DataGrid is databound you need to grab the ClientID for the TextBox and generate the Client-Side code using this ID. You can do this using the ItemDataBound event of the DataGrid. Page.RegisterStartupScript will generate the JavaScript code.
Code:
void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e) {
if (e.Item.ItemType == ListItemType.EditItem) {
TextBox txtTemp = (TextBox) e.Item.FindControl("TextBox1");
string clientSideID = txtTemp.ClientID;
if (!Page.IsStartupScriptRegistered("EditFocusTextBox")) {
Page.RegisterStartupScript("EditFocusTextBox", "<script language=\"javascript\" type=\"text/javascript\">document.getElementById('" + clientSideID + "').focus();<\u002Fscript>");
}
}
}
Let me know if you have any problems.
DJ
If I have been helpful please rate my post. If I haven't tell me!
-
Jun 24th, 2005, 05:35 AM
#5
Thread Starter
Frenzied Member
Re: Datagrid Template Column
I translated this into VB as follows:
VB Code:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
Dim txtTemp As System.Web.UI.WebControls.TextBox
Dim sClientID As String
If e.Item.ItemType = ListItemType.EditItem Then
txtTemp = e.Item.FindControl("TextBox1")
sClientID = txtTemp.ClientID
If (Page.IsStartupScriptRegistered("EditFocusTextBox")) = True Then
Page.RegisterStartupScript("EditFocusTextBox", "<script language=""javascript"" type=""text/javascript"">document.getElementById('" & sClientID & "').focus();<\u002Fscript>"")")
End If
End If
End Sub
This did not work and I think it's because the template column is not databound. It is an empty column to receive data entry from the user.
"e.Item.ItemType = ListItemType.EditItem" never equated to True so the script was never generated. When I removed the first "If" statement, I received an object not set exception at the line:
"sClientID = txtQuantity.ClientID"
-
Jun 24th, 2005, 05:56 AM
#6
Frenzied Member
Re: Datagrid Template Column
Could you give a bit more detail on how the DataGrid is built - perhaps the asp.net code?
Cheers
DJ
If I have been helpful please rate my post. If I haven't tell me!
-
Jun 24th, 2005, 06:23 AM
#7
Thread Starter
Frenzied Member
Re: Datagrid Template Column
I used your code to get it to work like this:
VB Code:
Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
Dim sClientID As String
Dim sJavaScript As String
sClientID = "DataGrid1__ctl" & ((e.Item.ItemIndex) + 2).ToString & "_TextBox1"
sJavaScript = "<script language=""javascript"">document.getElementById('" & sClientID & "').focus();</script>"
Page.RegisterStartupScript("EditFocusTextBox", sJavaScript)
BindDataGrid(e.Item.ItemIndex)
End Sub
I noticed that it allocated the ID number of the text box as 2 greater than the selected index of the grid.
However, this has created another problem - I now don't get focus to the search text box when the page is loaded. I'll try and use the same technique to set the focus to that text box when I am not updating.
What is the significance of the first parameter of Page.RegisterStartupScript ie. "EditFocusTextBox"?
-
Jun 24th, 2005, 06:31 AM
#8
Frenzied Member
Re: Datagrid Template Column
Okey dokey sounds like you've got it sorted.
The first parameter is a unique key for the block of script so it can be referenced elsewhere -> full explanation.
DJ
If I have been helpful please rate my post. If I haven't tell me!
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
|