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

Grid - Perform insert when item inserted

1 Answer 45 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 24 Jul 2014, 06:50 PM
Hi,

I'm having trouble getting the data from EditForm when I PerformInsert from the CommandItemTemplate, please help.

<telerik:RadGrid ID="RadGrid1" runat="server"

AllowPaging="false" AllowSorting="false" AutoGenerateColumns="false"

AllowAutomaticDeletes="false" AllowAutomaticInserts="false" AllowAutomaticUpdates="false"

ShowHeader="false"

OnItemCommand="RadGrid1_ItemCommand"

OnPreRender="RadGrid1_PreRender"

OnNeedDataSource="RadGrid1_NeedDataSource"

OnInsertCommand="RadGrid1_InsertCommand">

<MasterTableView DataKeyNames="PersonID" Width="50%" CommandItemDisplay="Bottom">

<CommandItemTemplate>

<telerik:RadButton ID="RadButtonInsert" runat="server" Text="Add" CommandName="PerformInsert">

<Icon PrimaryIconCssClass="rbAdd" PrimaryIconLeft="4" PrimaryIconTop="3"></Icon>

</telerik:RadButton>

</CommandItemTemplate>

<CommandItemSettings />

<NoRecordsTemplate></NoRecordsTemplate>

<EditFormSettings>

<PopUpSettings Modal="true" />

</EditFormSettings>

<Columns>

<telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">

</telerik:GridEditCommandColumn>

<telerik:GridBoundColumn UniqueName="FirstName" HeaderText="FirstName" DataField="FirstName">

</telerik:GridBoundColumn>

<telerik:GridBoundColumn UniqueName="LastName" HeaderText="LastName" DataField="LastName">

</telerik:GridBoundColumn>

<telerik:GridBoundColumn UniqueName="Telephone" HeaderText="Telephone" DataField="Telephone">

</telerik:GridBoundColumn>

<telerik:GridBoundColumn UniqueName="Email" HeaderText="Email" DataField="Email">

</telerik:GridBoundColumn>

<telerik:GridButtonColumn Text="Delete" CommandName="Delete" ButtonType="ImageButton" />

</Columns>

<EditFormSettings EditFormType="Template">

<FormTemplate>

* Person Name (Salutation, First, Middle, Last and Suffix):<br />

<telerik:RadComboBox ID="RadComboBox1" runat="server"

EnableLoadOnDemand="True" ShowMoreResultsBox="true" EnableVirtualScrolling="true"

OnItemsRequested="DropDownListCertHolderSalutation_ItemsRequested">

</telerik:RadComboBox>

<telerik:RadTextBox Width="250px" ID="TextBoxAttorneyFirstName" Text='<%# Bind( "FirstName") %>' runat="server" TabIndex="8">

</telerik:RadTextBox>

<telerik:RadTextBox Width="25px" ID="TextBoxAttorneyMiddleInitial" Text='<%# Bind( "MiddleInitial") %>' runat="server">

</telerik:RadTextBox>

<telerik:RadTextBox Width="250px" ID="TextBoxAttorneyLastName" Text='<%# Bind( "LastName") %>' runat="server">

</telerik:RadTextBox>

<telerik:RadComboBox ID="RadComboBox2" runat="server"

EnableLoadOnDemand="True" ShowMoreResultsBox="true" EnableVirtualScrolling="true"

OnItemsRequested="DropDownListCertHolderSuffix_ItemsRequested">

</telerik:RadComboBox>

<br />

Address:<br />

<telerik:RadTextBox Width="500px" ID="TextBoxAttorneyAddress1" Text='<%# Bind( "Address1") %>' runat="server">

</telerik:RadTextBox>

<br />

<telerik:RadTextBox Width="500px" ID="TextBoxAttorneyAddress2" Text='<%# Bind( "Address2") %>' runat="server">

</telerik:RadTextBox>

<br />

Country:

<telerik:RadComboBox ID="RadComboBox3" runat="server"

EnableLoadOnDemand="True" ShowMoreResultsBox="true" EnableVirtualScrolling="true" AutoPostBack="true"

OnItemsRequested="DropDownListCertHolderCountry_ItemsRequested"

OnSelectedIndexChanged="DropDownListCertHolderCountry_SelectedIndexChanged">

</telerik:RadComboBox>

&nbsp State:

<telerik:RadComboBox ID="RadComboBox4" runat="server"

EnableLoadOnDemand="True" ShowMoreResultsBox="true" EnableVirtualScrolling="true"

OnItemsRequested="DropDownListCertHolderState_ItemsRequested">

</telerik:RadComboBox>

<telerik:RadTextBox ID="RadTextBox6" runat="server" Width="200px" Visible="false"></telerik:RadTextBox>

&nbsp City:

<telerik:RadTextBox ID="RadTextBox7" runat="server" Width="250px"></telerik:RadTextBox>

&nbsp Zip Code:

<telerik:RadMaskedTextBox Width="50px" ID="RadMaskedTextBox1" runat="server" Mask="#####" TextMode="SingleLine">

</telerik:RadMaskedTextBox>

<br />

Telephone:

<telerik:RadMaskedTextBox Width="125px" ID="RadMaskedTextBox2" runat="server" Mask="(###) ###-####-####" TextMode="SingleLine">

</telerik:RadMaskedTextBox>

&nbsp Fax:

<telerik:RadMaskedTextBox Width="125px" ID="RadMaskedTextBox3" runat="server" Mask="(###) ###-####-####" TextMode="SingleLine">

</telerik:RadMaskedTextBox>

&nbsp

Email Address:

<telerik:RadTextBox ID="RadTextBox8" runat="server" Width="250px"></telerik:RadTextBox>

<br />

</FormTemplate>

</EditFormSettings>

</MasterTableView>

</telerik:RadGrid

 

 

>



protected void RadGrid1_PreRender(object sender, EventArgs e)

 

 

{

 

if (!Page.IsPostBack)

 

 

{

 

RadGrid1.MasterTableView.IsItemInserted = true;//so I can insert after load

 

 

RadGrid1.Rebind();

}

}

 

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)

 

 

{

 

if (e.CommandName == RadGrid.PerformInsertCommandName)

 

 

{

 

GridEditFormInsertItem f = (GridEditFormInsertItem)RadGrid1.MasterTableView.GetInsertItem();

Person p = new Person();

p.FirstName = (f.FindControl("FirstName") as TextBox).Text; //?? this control doesn't exist

 

 

}

}

 

protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)

 

 

{

 

GridEditFormInsertItem item = e.Item as GridEditFormInsertItem; //?? this is null

 

 

}

 

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)

 

 

{

 

RadGrid1.DataSource = Conselors;//loads a List<Person>

 

 

}

 

 

 

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 25 Jul 2014, 06:43 AM
Hi Steve,

From your code I have noticed you are setting PerformInsert command as the CommandName of InsertButton. When you want to access controls inside the currently inserted item, having FormTemplate as an edit form in Telerik RadGrid you should use InitInsert CommandName. Please do the following modification in your code snippet which works fine at my end.

ASPX:
...
<
CommandItemTemplate>
    <telerik:RadButton ID="RadButtonInsert" runat="server" Text="Add" CommandName="InitInsert">
        <Icon PrimaryIconCssClass="rbAdd" PrimaryIconLeft="4" PrimaryIconTop="3"></Icon>
    </telerik:RadButton>
</CommandItemTemplate>
...

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.InitInsertCommandName)
    {
        e.Item.OwnerTableView.InsertItem();// Insert the new item and rebind the grid
        GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item.OwnerTableView.GetInsertItem();//reference to the newly inserted item using the GetInsertItem() method
        RadTextBox box = insertedItem.FindControl("TextBoxAttorneyFirstName") as RadTextBox; //accessing the control
    }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Steve
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Share this question
or