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
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.
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> .......
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.
| <telerik:RadGrid ... <MasterTableView EditMode="PopUp" |
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
I modfied the Telerik example to use
| RadNumericTextBox.SelectionOnFocus = SelectionOnFocus.SelectAll |
| EditMode="PopUp" |
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.
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(); |
| } |
| } |
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(); |
| } |
| } |
| } |