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

RadGrid PrePopulate TextBox in EditFormSettings FormTemplate

3 Answers 205 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 11 Jun 2013, 04:39 PM
I have been following your examples and created a RadGrid very similar to http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/usercontroleditform/defaultcs.aspx

in EditFormSettings I use PopUpSettings-Modal="true" and then <FormTemplate> I list a number of text boxes.

One of these text boxes is for a datetime field. I would like it so that when the user clicks "add new record" and the pop up shows up that in the form in this text box is prepopulated with the current date. I can't seem to get this working. This is my code:

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            String strDate = DateTime.Today.ToString(); 
if (e.CommandName == RadGrid.InitInsertCommandName) //Runs when "Add new" button clicked
        {
                //Code to prepopulate field (But Doesn't Work)
GridCommandItem item = (GridCommandItem)e.Item; /*If I try with data item I get error: Unable to cast object of type 'Telerik.Web.UI.GridCommandItem' to type 'Telerik.Web.UI.GridDataItem'. */
TextBox txtname = (TextBox)item.FindControl("txtDate"); //txtDate is the name of the txtbox I want to prepopulate
txtname.Text = strDate;
}

However because its in a pop up it can't find the control and I get error: "NullReferenceException was unhandled by usercode, Object reference not set to an instance of an object."
Is this(Prepopulating a form template textbox inside a popup) even possible to do?... If so how?... Thank You

3 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 12 Jun 2013, 04:11 AM
Hi,

Please take a look into the following code snippet I tried to pre-populate the TextBox inside the FormTemplate.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
    <MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="EmployeeID">
        <Columns>
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
            </telerik:GridEditCommandColumn>
            <telerik:GridBoundColumn UniqueName="TitleOfCourtesy" HeaderText="TOC" DataField="TitleOfCourtesy">
                <HeaderStyle Width="60px"></HeaderStyle>
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="HireDate" HeaderText="Hire Date" DataField="HireDate"
                DataFormatString="{0:d}">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Title" HeaderText="Title" DataField="Title">
            </telerik:GridBoundColumn>
            <telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" CommandName="Delete">
            </telerik:GridButtonColumn>
        </Columns>
        <EditFormSettings EditFormType="Template" PopUpSettings-Modal="true">
            <FormTemplate>
                <asp:TextBox ID="textbox" runat="server"></asp:TextBox>
            </FormTemplate>
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
    {
        GridEditFormInsertItem edit = (GridEditFormInsertItem)e.Item;
        TextBox txt = (TextBox)edit.FindControl("textbox");
        txt.Text = DateTime.Today.ToString();
    }
}

Thanks,
Shinu.
0
Tim
Top achievements
Rank 1
answered on 12 Jun 2013, 08:28 AM
Hi Shinu,

Thank You, That worked perfectly! :)
0
Sai
Top achievements
Rank 1
answered on 25 Jul 2014, 01:00 PM
Thank u, It Worked for me
Tags
Grid
Asked by
Tim
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Tim
Top achievements
Rank 1
Sai
Top achievements
Rank 1
Share this question
or