This is a migrated thread and some comments may be shown as answers.

[Solved] PopUp Edit Form Setting Focus Error

10 Answers 305 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 17 Sep 2009, 05:22 PM
I am trying to set the focus on a field in my PopUp edit form template using the below example from the documentation. The problem is that MyUserControl is equal to Nothing since it seems that "EditFormControl" is not being found in e.Item.FindControl(GridEditFormItem.EditFormUserControlID).

Anyone have an idea of why Dim MyUserControl As UserControl = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) is not being found?


Protected Sub RadGrid1_ItemCreated(sender As Object, e As Telerik.Web.UI.GridItemEventArgs)
   If TypeOf e.Item Is GridEditFormItem And e.Item.IsInEditMode Then
      Dim MyUserControl As UserControl = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) 
      RadGrid1.Controls.Add( New LiteralControl(String.Format( "<script type='text/javascript'>document.getElementById('{0}').focus();document.getElementById('{0}').select();</script>", MyUserControl.FindControl("txtFirstName").ClientID)))
   End If
End Sub

10 Answers, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 18 Sep 2009, 03:08 PM
Hi Robert,

I recommend that you use the following code to find a textbox in the edit form and set focus to it and select the text:
Protected Sub RadGrid1_ItemCreated(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) 
   If TypeOf e.Item Is GridEditFormItem And e.Item.IsInEditMode Then 
      Dim item As GridEditFormItem = DirectCast(e.Item, GridEditFormItem) 
      Dim txtBox As TextBox = (TryCast(item.FindControl("txtFirstName"), TextBox)) 
      txtBox.Attributes.Add("onfocus""this.select(); "
      txtBox.Focus() 
   End If 
End Sub 

Please let me know whether this works for you.

Sincerely yours,
Mira
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Robert
Top achievements
Rank 1
answered on 18 Sep 2009, 04:01 PM

Dim

 

item As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)

 


Is also equal to Nothing.  What else can I be missing? 

I have the following setup:

<

 

MasterTableView CommandItemDisplay="Top" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" DataKeyNames="PERSON_ID" EditMode="PopUp" > ..........

 


And:

 

<EditFormSettings EditFormType="Template" CaptionFormatString="Modify This Author / Analyst" >

 

 

<FormTemplate> .......

 

 

 

0
Mira
Telerik team
answered on 24 Sep 2009, 09:20 AM
Hi Robert,

I am attaching a sample project with a template edit form and a text box in it. The text box is focused and its text is selected when the edit template is shown.
Please take a look at it and tell me whether you find it useful.

Greetings,
Mira
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Wyatt
Top achievements
Rank 1
answered on 28 Oct 2009, 05:57 PM
This is exactly what I need and I appreciate your help.  Unfortunately, it does not work.  The focus is not set on the control.  I downloaded your example, which is not a popup edit.  I only changed the following.

<telerik:RadGrid ... <MasterTableView EditMode="PopUp" 
 
0
Robert
Top achievements
Rank 1
answered on 28 Oct 2009, 06:23 PM
Hey Wyatt,

Here is the solution for a Numeric Textbox:

    Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated

        If (TypeOf e.Item Is GridEditFormItem AndAlso _
            e.Item.IsInEditMode) Then

            Dim txtOrg As Telerik.Web.UI.RadNumericTextBox = CType(e.Item.FindControl("txtOrgId"), Telerik.Web.UI.RadNumericTextBox)
            txtOrg.SelectionOnFocus = SelectionOnFocus.SelectAll
            txtOrg.Focus()
        End If

    End Sub

Robert

0
Wyatt
Top achievements
Rank 1
answered on 28 Oct 2009, 06:42 PM
Robert,

I modfied the Telerik example to use
RadNumericTextBox.SelectionOnFocus = SelectionOnFocus.SelectAll 
, and it did not fix the focus problem when
EditMode="PopUp" 
0
Robert
Top achievements
Rank 1
answered on 28 Oct 2009, 06:54 PM

Remove this event from the sample:

 

 

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)

 

{

 

if (e.Item is GridEditFormItem && e.Item.IsInEditMode)

 

{

GridEditFormItem item = (GridEditFormItem)e.Item;

TextBox txtBox = item.FindControl(

"TextBox1") as TextBox;

 

txtBox.Attributes.Add(

"onfocus", "this.select(); ");

 

txtBox.Focus();

}

}

Add the RadGrid1_ItemCreated event as I suggested.

0
Wyatt
Top achievements
Rank 1
answered on 28 Oct 2009, 07:43 PM
Robert,
I modified the example in place.  Here are the blocks of code that changed.
<telerik:RadGrid ID="RadGrid1" runat="server" ShowGroupPanel="True" Width="99%" AllowPaging="True" AllowMultiRowSelection="false" PageSize="50" DataSourceID="SqlDataSource1" GridLines="None" PagerStyle-AlwaysVisible="true" AutoGenerateEditColumn="true" OnItemCreated="RadGrid1_ItemCreated"

<MasterTableView AutoGenerateColumns="False" Name="Requirements" EditMode="PopUp" GridLines="None" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" CommandItemDisplay="TopAndBottom"

<FormTemplate> 
   <telerik:RadNumericTextBox ID="TextBox1" Value="1" runat="server" /> 
</FormTemplate>

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
{  
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode)  
    {  
        GridEditFormItem item = (GridEditFormItem)e.Item;  
        RadNumericTextBox txtBox = item.FindControl("TextBox1") as RadNumericTextBox;  
        txtBox.SelectionOnFocus = SelectionOnFocus.SelectAll;  
        txtBox.Focus();  
    }  
0
Robert
Top achievements
Rank 1
answered on 28 Oct 2009, 07:52 PM
Attach your project to this thread.
0
Wyatt
Top achievements
Rank 1
answered on 28 Oct 2009, 08:00 PM
Robert,
I think only Telerik can attached code files.  I am limited to .gif, .jpg, .jpeg, and .png.  Download radgridformtemplatefocus.zip from the last Telerik post and replace the code with the following.

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
 
   </head> 
<body> 
    <form id="form1" runat="server">  
    <div> 
        <asp:ScriptManager ID="ScriptManager1" runat="server">  
        </asp:ScriptManager> 
          
        <telerik:RadGrid ID="RadGrid1" runat="server" ShowGroupPanel="True" Width="99%" AllowPaging="True" AllowMultiRowSelection="false" PageSize="50" DataSourceID="SqlDataSource1" GridLines="None" PagerStyle-AlwaysVisible="true" AutoGenerateEditColumn="true" OnItemCreated="RadGrid1_ItemCreated">  
            <ExportSettings> 
                <Pdf FontType="Subset" PaperSize="Letter" /> 
                <Csv ColumnDelimiter="Colon" RowDelimiter="NewLine" /> 
            </ExportSettings> 
            <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle> 
            <ItemStyle Font-Size="8pt" VerticalAlign="Middle" /> 
            <AlternatingItemStyle Font-Size="8pt" VerticalAlign="Middle" /> 
            <SelectedItemStyle /> 
            <GroupPanel> 
            </GroupPanel> 
            <FilterMenu EnableEmbeddedSkins="true" Skin="Vista">  
            </FilterMenu> 
            <MasterTableView AutoGenerateColumns="False" Name="Requirements" EditMode="PopUp" GridLines="None" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" CommandItemDisplay="TopAndBottom">  
                <RowIndicatorColumn Visible="True">  
                </RowIndicatorColumn> 
                <EditFormSettings EditFormType="Template">  
                    <FormTemplate> 
                       <telerik:RadNumericTextBox ID="TextBox1" Value="1" runat="server" /> 
                    </FormTemplate> 
                </EditFormSettings> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="ProductName" HeaderText="ProductName" UniqueName="ProductName" 
                        AllowFiltering="true">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="ProductID" HeaderText="ProductID" UniqueName="ProductID" 
                        AllowFiltering="true" DataType="System.Int32" ReadOnly="false">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="CategoryName" HeaderText="CategoryName" UniqueName="CategoryName">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="CategoryID" HeaderText="CategoryID" UniqueName="CategoryID" 
                        DataType="System.Int32">  
                    </telerik:GridBoundColumn> 
                </Columns> 
            </MasterTableView> 
        </telerik:RadGrid> 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
            SelectCommand="SELECT [ProductName], [ProductID], [CategoryName], [CategoryID] FROM [Alphabetical list of products]">  
        </asp:SqlDataSource> 
    </div> 
    </form> 
</body> 
</html> 

Default.aspx.cs
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using Telerik.Web.UI;  
 
public partial class _Default : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
         
    }  
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)  
        {  
            GridEditFormItem item = (GridEditFormItem)e.Item;  
            RadNumericTextBox txtBox = item.FindControl("TextBox1"as RadNumericTextBox;  
            txtBox.SelectionOnFocus = SelectionOnFocus.SelectAll;  
            txtBox.Focus();  
        }  
    }  
 
Tags
Grid
Asked by
Robert
Top achievements
Rank 1
Answers by
Mira
Telerik team
Robert
Top achievements
Rank 1
Wyatt
Top achievements
Rank 1
Share this question
or