Set focus to control in ListView InsertItemTemplate RRS feed

  • Question

  • Hi all,

    I tried to figured out where to post this, but eventually agreed with myself that this could be an ok place to ask. If not, please help me.

    Here's the deal:

    I am building a .NET 3.5 C# website.
    I use the ListView control and have a number of controls in the InsertItemTemplate. When the user adds a new entry, I want to specifically select what control has focus.

    Here's a snip of the InsertItemTemplate:

    1             <InsertItemTemplate> 
    2                 <tr > 
    3                     <td colspan="5">  
    4                         <div class="ActionMenuText">Insert New Computergroup Name</div> 
    5                         Name: <asp:TextBox ID="TextInsertGroupname" runat="server" Text='<%# Bind("GroupName") %>' /> 
    6                         <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />&nbsp  
    7                         <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" CausesValidation="false" OnClick="CancelButton_Click" Text="Cancel" /> 
    8                     </td> 
    9                 </tr> 
    10             </InsertItemTemplate> 

    In the beginning of my aspx file I have then created this to catch the event:

    1 <script runat="server">  
    2     protected void lvwComputerGroups_ItemDataBound(object sender, ListViewItemEventArgs e)  
    3     {  
    5         //This code should set focus to the groupname when inserting a new group, but it doesn't. Need to debug this :|  
    7         if (e.Item.ItemType == ListViewItemType.DataItem)  
    8         {  
    9             TextBox TempControl = (TextBox)e.Item.FindControl("TextInsertGroupname");  
    10             if ( TempControl != null )  
    11             {  
    12             TempControl.Focus();  
    13             }  
    14         }  
    15     }  
    16 </script> 

    Now, when I create a breakpoint in the above code I can see that the code is hit, but the TextInsertGroupname control does not get access. What am I missing here?

    Thank you.

    Monday, December 15, 2008 9:12 PM