Results 1 to 8 of 8

Thread: Datagrid Template Column

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    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.

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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!

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    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?

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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!

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Datagrid Template Column

    I translated this into VB as follows:
    VB Code:
    1. Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
    2.         Dim txtTemp As System.Web.UI.WebControls.TextBox
    3.         Dim sClientID As String
    4.  
    5.         If e.Item.ItemType = ListItemType.EditItem Then
    6.             txtTemp = e.Item.FindControl("TextBox1")
    7.             sClientID = txtTemp.ClientID
    8.  
    9.             If (Page.IsStartupScriptRegistered("EditFocusTextBox")) = True Then
    10.                 Page.RegisterStartupScript("EditFocusTextBox", "<script language=""javascript"" type=""text/javascript"">document.getElementById('" & sClientID & "').focus();<\u002Fscript>"")")
    11.             End If
    12.  
    13.      End If  
    14.  
    15. 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"

  6. #6
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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!

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Datagrid Template Column

    I used your code to get it to work like this:
    VB Code:
    1. Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
    2.         Dim sClientID As String
    3.         Dim sJavaScript As String
    4.  
    5.         sClientID = "DataGrid1__ctl" & ((e.Item.ItemIndex) + 2).ToString & "_TextBox1"
    6.         sJavaScript = "<script language=""javascript"">document.getElementById('" & sClientID & "').focus();</script>"
    7.         Page.RegisterStartupScript("EditFocusTextBox", sJavaScript)
    8.  
    9.         BindDataGrid(e.Item.ItemIndex)
    10.  
    11. 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"?

  8. #8
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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
  •  



Click Here to Expand Forum to Full Width