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

Insert-Update using FormTemplate

7 Answers 135 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tejas
Top achievements
Rank 1
Tejas asked on 14 Apr 2011, 04:32 PM
Hi,
I have RadGrid with gets data from 4 different database tables using Entity Framework and LINQ with MS Framework 4.0. I have problem getting values from the EditForm controls while performing insert and Update Operations:
.aspx Mark-up:
<telerik:RadGrid ID="grdCollections" runat="server" 
            GridLines="None" AutoGenerateColumns="False"
            OnInsertCommand="grdCollections_Insert"  OnUpdateCommand="grdCollections_Update"
            OnEditCommand="grdCollections_Command" AllowSorting="True"
            OnDeleteCommand="grdCollections_Command" OnNeedDataSource="grdCollections_NeedDataSource" 
            OnCancelCommand="grdCollections_Command" OnItemDataBound="grdCollections_ItemDataBound"
            PageSize="10"
             >
    
 
    <MasterTableView  EditMode="EditForms"
                      CommandItemDisplay="Top"
                      DataKeyNames="CollectionID"
                      InsertItemDisplay="Bottom">
   
   <CommandItemSettings ShowAddNewRecordButton="true"  AddNewRecordImageUrl="~/Images/AddRecord.gif"
                    AddNewRecordText="Add New Collection Entry"  >
    </CommandItemSettings>
 
  
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
 
    <Columns>
        <telerik:GridBoundColumn DataField="CollectionID"
            DataType="System.Int32"  ReadOnly="true"
            FilterControlAltText="Filter CollectionID column" HeaderText="ID"
            SortExpression="CollectionID" UniqueName="CollectionID">
        </telerik:GridBoundColumn>
        
       <telerik:GridDropDownColumn FilterControlAltText="Filter CreditorID column"
            UniqueName="CreditorID" DataField="CreditorID" HeaderText="*CreditorID"
            Visible="true" >
        </telerik:GridDropDownColumn>
 
       <telerik:GridDropDownColumn FilterControlAltText="Filter Name column"
            UniqueName="Name" DataField="Name" HeaderText="*Agency Name"
            ListTextField="Name" ListValueField="CreditorID">
       </telerik:GridDropDownColumn>
 
        <telerik:GridBoundColumn DataField="AccountNumber"
            FilterControlAltText="Filter AccountNumber column" HeaderText="Account No."
            SortExpression="AccountNumber" UniqueName="AccountNumber">
        </telerik:GridBoundColumn>
        
        <telerik:GridDateTimeColumn FilterControlAltText="Filter RptdDate column"
            DataField="RptdDate" DataType="System.DateTime" HeaderText="*Opened Date"
            SortExpression="RptdDate" UniqueName="RptdDate" DataFormatString="{0:d}">
        </telerik:GridDateTimeColumn>
        
        <telerik:GridDateTimeColumn DataField="RvsdDate" DataType="System.DateTime"
            FilterControlAltText="Filter RvsdDate column" HeaderText="Date Revised"
            SortExpression="RvsdDate" UniqueName="RvsdDate" DataFormatString="{0:d}">
        </telerik:GridDateTimeColumn>
        <telerik:GridDateTimeColumn DataField="LastPmtDate" DataType="System.DateTime"
            FilterControlAltText="Filter LastPmtDate column" HeaderText="Last Payment Date"
            SortExpression="LastPmtDate" UniqueName="LastPmtDate" DataFormatString="{0:d}">
        </telerik:GridDateTimeColumn>
 
        <telerik:GridNumericColumn DataField="Amount" DataType="System.Decimal"
            FilterControlAltText="Filter Amount column" HeaderText="Amount"
            SortExpression="Amount" UniqueName="Amount" DataFormatString="{0:C}">
        </telerik:GridNumericColumn>
 
        <telerik:GridNumericColumn DataField="Balance" DataType="System.Decimal"
            FilterControlAltText="Filter Balance column" HeaderText="Total Balance" DataFormatString="{0:C}"
            SortExpression="Balance" UniqueName="Balance">
        </telerik:GridNumericColumn>
 
        <telerik:GridEditCommandColumn FilterControlAltText="Filter EditCommandColumn column">
        </telerik:GridEditCommandColumn>
        <telerik:GridButtonColumn CommandName="Delete"
            FilterControlAltText="Filter column1 column" Text="Delete" UniqueName="column1">
        </telerik:GridButtonColumn>
    </Columns>
 
    <EditFormSettings EditFormType="Template">
        <EditColumn UniqueName="EditCommandColumn1">
        </EditColumn>
        <FormTemplate>
            <table width="580px">
            <tr>
                <td style="width:19%; text-align:right;">
                    <asp:Label ID="lblEditAgency" runat="server" Text="*Agency:"></asp:Label>
                </td>
                <td style="width:1%;"
                </td>
                <td style="width:80%;">
                        <telerik:RadComboBox ID="cmbCreditors" runat="server" 
                            DataTextField="Name" DataValueField="CreditorID"
                            DropDownWidth="440px" Height="150px"
                            EmptyMessage="Select the name of the collection agency. If the agency is not on the list, click Add New">
                            <HeaderTemplate>
                                <table style="width: 440px" cellspacing="0" cellpadding="0">
                                    <tr>
                                    <td style="width:30px">ID
                                    </td>
                                    <td style="width:120px">Agency Name
                                    </td>
                                    <td style="width:140px">Address
                                    </td>
                                    <td style="width:75px">Phone
                                    </td>
                                    <td style="width:75px">Fax
                                    </td>
                                    </tr>
                                </table>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <table  style="width: 440px" cellspacing="0" cellpadding="0">
                                    <tr>
                                        <td style="width:30px"><%# DataBinder.Eval(Container.DataItem, "CreditorID")%>
                                        </td>
                                        <td style="width:120px"><%# DataBinder.Eval(Container.DataItem, "Name")%>
                                        </td>
                                        <td style="width:140px"><%# DataBinder.Eval(Container.DataItem, "Address")%>
                                        </td>
                                        <td style="width:75px"><%# DataBinder.Eval(Container.DataItem, "Phone")%>
                                        </td>
                                        <td style="width:75px"><%# DataBinder.Eval(Container.DataItem, "Fax")%>
                                        </td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </telerik:RadComboBox>
                            
                        <asp:Button ID="Button3" Text="Add Agency" runat="server" OnClientClick="radbtnAddAgency(); return false;">
                        </asp:Button>
 
                </td>
            </tr>
 
            <tr>
                <td  style="text-align:right;">
                    <asp:Label ID="Label2" runat="server" Text="*Opened Date:"></asp:Label>
                </td>
                <td
                </td>
                <td>
                <telerik:RadDatePicker ID="DtPickerOpenedDate" runat="server">
                </telerik:RadDatePicker>
                </td>
            </tr>
            <tr>
                <td  style="text-align:right;">
                <asp:Label ID="Label1" runat="server" Text="*Revised Date:"></asp:Label>
                </td>
                <td
                </td>
                <td>
                <telerik:RadDatePicker ID="DtPickerRevisedDate" runat="server">
                </telerik:RadDatePicker>
                </td>
            </tr>
            <tr>
                <td  style="text-align:right;">
                <asp:Label ID="Label4" runat="server" Text="Account Number:"></asp:Label>
                </td>
                <td
                </td>
                <td>
                <telerik:RadTextBox ID="txtAccNumber" runat="server" Width="200px" EmptyMessage="Enter the internal account number if applicable">
                </telerik:RadTextBox>
                </td>
            </tr>
            <tr>
                <td  style="text-align:right;">
                <asp:Label ID="Label5" runat="server" Text="Last Payment Date:"></asp:Label>
                </td>
                <td
                </td>
                <td>
                <telerik:RadDatePicker ID="DtPickerLastPaymentDate" runat="server">
                </telerik:RadDatePicker>
                </td>
            </tr>
            <tr>
                <td  style="text-align:right;">
                <asp:Label ID="Label6" runat="server" Text="Debt Amount:"></asp:Label>
                </td>
                <td
                </td>
                <td>
                <telerik:RadNumericTextBox ID="txtDebtAmount" runat="server" Width="70px" Type="Currency"  EmptyMessage="Click here to enter amount">
                </telerik:RadNumericTextBox>
                </td>
            </tr>
            <tr>
                <td  style="text-align:right;">
                <asp:Label ID="Label7" runat="server" Text="Total Balance:"></asp:Label>
                </td>
                <td
                </td>
                <td>
                <telerik:RadNumericTextBox ID="txtTotalBalance" runat="server" Width="70px" Type="Currency"  EmptyMessage="Click here to enter amount">
                </telerik:RadNumericTextBox>
                </td>
            </tr>
            <tr>
                <td colspan="3">
                <asp:Button ID="btnSave" Text='<%# ((bool)DataBinder.Eval(Container, "OwnerTableView.IsItemInserted")) ? "Insert" : "Update" %>'
                        runat="server" CommandName='<%# ((bool)DataBinder.Eval(Container, "OwnerTableView.IsItemInserted")) ? "PerformInsert" : "Update" %>'>
                </asp:Button>  
                <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel">
                </asp:Button>
                </td>
            </tr>
            </table>
        </FormTemplate>
    </EditFormSettings>
</MasterTableView>
 
<FilterMenu EnableImageSprites="False"></FilterMenu>
 <ClientSettings>
        <ClientEvents OnCommand="RaiseCommand" />
    </ClientSettings>
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
        </telerik:RadGrid>

Code behind:
protected void grdCollections_Insert(object sender, GridCommandEventArgs e)
        {
 
            
 
            GridEditFormInsertItem insItem = e.Item as GridEditFormInsertItem;
            //GridEditableItem  editItem = e.Item.OwnerTableView.GetInsertItem();
 
            int AgentId = 0;
            RadComboBox cmb = insItem.FindControl("cmbCreditors") as RadComboBox;
//cmb shows null
            if ((cmb != null) && (cmb.DataSource != null))
            {
                AgentId = Convert.ToInt32(cmb.SelectedValue);
 
            }
//txtAmt  shows null         
RadTextBox txtAmt = insItem.FindControl("txtDebtAmount") as RadTextBox;
}
 
 protected void grdCollections_Update(object sender, GridCommandEventArgs e)
        {
 
            GridEditableItem editedItem = e.Item as GridEditableItem;
 
            int AgentId = 0;
            RadComboBox cmb = editedItem.FindControl("cmbCreditors") as RadComboBox;
//cmb shows null 
            if ((cmb != null) && (cmb.DataSource != null))
            {
                AgentId = Convert.ToInt32(cmb.SelectedValue);
 
            }
//txtAmt  shows null 
            RadTextBox txtAmt = editedItem.FindControl("txtDebtAmount") as RadTextBox;
 
        }

Can you please tell me whats am I doing wrong here ?

Thanks

Tejas

7 Answers, 1 is accepted

Sort by
0
Mike Wendell
Top achievements
Rank 1
answered on 14 Apr 2011, 05:41 PM
I am having the same problem as you. I cannot retrieve the values from the insert/update form. I have tried multiple different ways to access these form fields with no success.

I hope someone from the Telerik support team can help us sort this out.
0
Elliott
Top achievements
Rank 2
answered on 14 Apr 2011, 06:44 PM
did you try to case e.Item as GridDataItem?
0
Tejas
Top achievements
Rank 1
answered on 14 Apr 2011, 07:40 PM
Hi,
Thanks for the reply. I tried the case e.Item as GridDataItem, but It returns null. Any other solution ?

Thanks

Tejas
0
Tejas
Top achievements
Rank 1
answered on 15 Apr 2011, 11:11 PM
Hi,
Any update on this question, please ??

Thanks

Tejas
0
Tsvetina
Telerik team
answered on 20 Apr 2011, 08:52 AM
Hello Tejas,

Such problem could occur if you are not binding your grid correctly. Could you share any databinding logic that you have - your NeedDataSource code, any places where you rebind the grid, etc? Make sure that you do not make calls to grid.DataBind() anywhere in your coe. Generally, the pasted code looks correct, I cannot notice in the code snippets an obvious reason why it would not work .

Regards,
Tsvetina
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Mike Wendell
Top achievements
Rank 1
answered on 20 Apr 2011, 02:29 PM
Hey Tejas,

Just to confirm what the solution for me was...I was using manual, asp.net style data binding. Once I added a NeedDataSource event handler to my grid, the form values post as expected. It did require some minor changes to my design, but it is working great.

Thanks to the Telerik team for their help!
Mike
0
Tejas
Top achievements
Rank 1
answered on 20 Apr 2011, 04:00 PM
Hi,
It solved my problem when I've deleted grid.DataBind(); method. In addition, I could observe that I needed to find control RadNumericTextbox and not the RadTextbox.

Thanks to all for your valuable helps

Tejas
Tags
Grid
Asked by
Tejas
Top achievements
Rank 1
Answers by
Mike Wendell
Top achievements
Rank 1
Elliott
Top achievements
Rank 2
Tejas
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or