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

ItemInserted event code behind

7 Answers 316 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Torben
Top achievements
Rank 1
Torben asked on 29 Sep 2009, 07:27 PM
Hi

I need to access the value of a GridTemplateColumn in the iteminsertet event.
It is working fine with GridBoundColumn.

 

<telerik:GridBoundColumn DataField="contactmobile" HeaderText="Mobile"
SortExpression="mobile" UniqueName="contactmobile">
</telerik:GridBoundColumn>

<telerik:GridTemplateColumn DataField="address" HeaderText="Address"
         SortExpression="Address" UniqueName="Address">
<EditItemTemplate>
     <asp:TextBox ID="addressTextBox" TextMode="MultiLine" Width="300px" Height="75px"
     runat="server" Text='<%# Bind("address") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="addressLabel" runat="server" Text='<%# Eval("address") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>

 


code behind:
        protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
        {
            GridEditFormInsertItem editformItem = (GridEditFormInsertItem)e.Item;
            TextBox contactmobile = (TextBox)editformItem["contactmobile"].Controls[0];
            // working fine
            TextBox address = (TextBox)editformItem["address"].Controls[0];
            // working fine not working 
            // dumps with Cannot find cell bound to column with unique name 'address'
     }

How do I read the value from Address here ??
Thx
Torben

 

7 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 30 Sep 2009, 03:56 AM
Hello Torben,

Inorder to access controls in the GridTemplateColumns you would have to use the FindControl method as shown below:
c#:
protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e) 
        { 
            GridEditFormInsertItem editformItem = (GridEditFormInsertItem)e.Item; 
            TextBox contactmobile = (TextBox)editformItem["contactmobile"].Controls[0]; 
            TextBox address = (TextBox)dataItem.FindControl("addressTextBox"); 
            
       } 

Thanks
Princy.
0
Torben
Top achievements
Rank 1
answered on 30 Sep 2009, 05:34 AM
Hi Princy

Super thx, it is working fine.

regards Torben
0
Chandan Dey
Top achievements
Rank 1
answered on 22 Nov 2011, 03:47 PM
Hi,


I am using Rad Grid Insert/Update/Delete with my SQL Server Database.

I am using trial version of "Telerik.Web.UI_2011_1_413_Dev".

But my rgMaterials_ItemInserted, rgMaterials_ItemUpdated, rgMaterials_ItemDeleted not fire in code behind.
If I use rgMaterials_NeedDataSource event my grid not generate and grid not showing. There is no data in my database but if I set like this
rgMaterials.DataSource = serviceRecommendationMaterial.LoadData(connection);
rgMaterials.DataBind();
Grid show "No item to display" message.
.aspx

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
                                            <AjaxSettings>
                                                <telerik:AjaxSetting AjaxControlID="rgMaterials">
                                                    <UpdatedControls>
                                                        <telerik:AjaxUpdatedControl ControlID="rgMaterials" LoadingPanelID="RadAjaxLoadingPanel1" />
                                                    </UpdatedControls>
                                                </telerik:AjaxSetting>
                                            </AjaxSettings>
                                        </telerik:RadAjaxManager>
                                        <telerik:RadGrid ID="rgMaterials" runat="server" 
                                            AllowFilteringByColumn="false" AllowSorting="true" AllowPaging="false" 
                                            AutoGenerateColumns="False" Skin="WebBlue"
                                            AllowAutomaticInserts="true" AllowAutomaticUpdates="true" AllowAutomaticDeletes="true"
                                            onitemcommand="rgMaterials_ItemCommand" oninit="rgMaterials_Init" 
                                            OnItemInserted="rgMaterials_ItemInserted" 
                                            onitemupdated="rgMaterials_ItemUpdated" 
                                            onitemdeleted="rgMaterials_ItemDeleted" 
                                            onneeddatasource="rgMaterials_NeedDataSource">
                                            
                                            <PagerStyle Mode="NextPrevAndNumeric" />
                                            
                                            <MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="Part" CommandItemSettings-ShowRefreshButton="false" CommandItemSettings-AddNewRecordText="Add" AutoGenerateColumns="False">
                                                <%--<CommandItemStyle CssClass="OuterItemStyle" />--%>
                                                <CommandItemTemplate>
                                                    <table style="background-color:white; width:100%;">
                                                        <tr>
                                                            <td align="left">
                                                                <asp:Label ID="lblMaterials" runat="server" Text="Materials" CssClass="detail_box_item_font" ForeColor="Black"></asp:Label>
                                                            </td>
                                                            <td align="right">
                                                                <asp:Button ID="btnAdd" Text="Add New" runat="server" CommandName="InitInsert"></asp:Button>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </CommandItemTemplate>
                                                <Columns>
                                                    <telerik:GridBoundColumn DataField="Part" SortExpression="Part" HeaderText="Part #" UniqueName="Part" ColumnEditorID="GridTextBoxColumnEditor1" ></telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="Comment" SortExpression="Comment" HeaderText="Comment" UniqueName="Comment" ColumnEditorID="GridTextBoxColumnEditor2"></telerik:GridBoundColumn>
                                                    <telerik:GridTemplateColumn HeaderText="Price" SortExpression="Price" UniqueName="TemplateColumn" EditFormColumnIndex="1">
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblPrice" Text='<%# Eval("Price", "{0:C}") %>'></asp:Label>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <%--<span>--%>
                                                                <telerik:RadNumericTextBox runat="server" ID="tbPrice" Width="100px" DbValue='<%# Bind("Price") %>' ShowSpinButtons="true" IncrementSettings-InterceptArrowKeys="true" IncrementSettings-InterceptMouseWheel="false" HoveredStyle-HorizontalAlign="Left" MinValue="0">
                                                                </telerik:RadNumericTextBox>
                                                                <%--<telerik:RadNumericTextBox runat="server" ID="tbPrice" Width="40px" DbValue='<%# Bind("Price") %>'>
                                                                </telerik:RadNumericTextBox>--%>
                                                                <span style="color: Red">
                                                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="tbPrice" ErrorMessage="*" runat="server">
                                                                    </asp:RequiredFieldValidator>
                                                                </span>
                                                        </EditItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn HeaderText="" >
                           <ItemTemplate>
                           <asp:LinkButton id="lnkEdit" runat="server" Text="Edit" CausesValidation="false" CommandName="Edit"></asp:LinkButton>
                           </ItemTemplate>
                           </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn HeaderText="" >
                           <ItemTemplate>
                           <asp:LinkButton id="lnkDelete" runat="server" Text="Delete" CausesValidation="false" CommandName="Delete"></asp:LinkButton>
                           </ItemTemplate>
                           </telerik:GridTemplateColumn>
                                                </Columns>
                                                <EditFormSettings ColumnNumber="2" InsertCaption="New Material" >
                                                    <FormTableItemStyle Wrap="false" VerticalAlign="Top"></FormTableItemStyle>
                                                    <FormCaptionStyle HorizontalAlign="Left" ></FormCaptionStyle>
                                                    <FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3" BackColor="White" Width="100%" />
                                                    <FormTableStyle CellSpacing="0" CellPadding="2" Height="60px" BackColor="White" />
                                                    <FormTableAlternatingItemStyle Wrap="false"></FormTableAlternatingItemStyle>
                                                    <FormTableButtonRowStyle HorizontalAlign="Left" ></FormTableButtonRowStyle>
                                                </EditFormSettings>
                                            </MasterTableView>
                                        </telerik:RadGrid>
                                        <telerik:GridTextBoxColumnEditor ID="GridTextBoxColumnEditor1" runat="server" TextBoxStyle-Width="150px" />
                                        <telerik:GridTextBoxColumnEditor ID="GridTextBoxColumnEditor2" runat="server" TextBoxStyle-Width="150px" />
                                        <telerik:GridNumericColumnEditor ID="GridNumericColumnEditor1" runat="server" NumericTextBox-Width="40px" />
                                        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" Skin="WebBlue" IsSticky="false" InitialDelayTime="0" MinDisplayTime="1000" EnableSkinTransparency="false" runat="server"></telerik:RadAjaxLoadingPanel>



aspx.cs

protected void rgMaterials_ItemInserted(object source, GridInsertedEventArgs e)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["xtramec_dbConnectionString"].ToString();
            GridEditFormInsertItem editformItem = (GridEditFormInsertItem)e.Item;
            ServiceRecommendationMaterial serviceRecommendationMaterial = new ServiceRecommendationMaterial();
            SqlConnection connection = new SqlConnection(connectionString);
            serviceRecommendationMaterial.RecommendationId = serviceRecommendationMaterial.GetRecommendationId(connection);
            serviceRecommendationMaterial.PartNumber = editformItem["Part"].Controls[0].ToString();
            serviceRecommendationMaterial.Comment = editformItem["Comment"].Controls[0].ToString();
            serviceRecommendationMaterial.Price = Convert.ToDecimal(editformItem["Price"].Controls[0]);
            serviceRecommendationMaterial.Quantity = Convert.ToDecimal(0);
            serviceRecommendationMaterial.InsertServiceRecommendationMaterial(connection);
            //TextBox contactmobile = (TextBox)editformItem["contactmobile"].Controls[0];
            //TextBox address = (TextBox)dataItem.FindControl("addressTextBox");
            connection.Close();
            connection = null;
        }

protected void rgMaterials_ItemUpdated(object sender, GridUpdatedEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string PartNo = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["Part"].ToString();
        }

protected void rgMaterials_ItemDeleted(object sender, GridDeletedEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string PartNo = item.OwnerTableView.DataKeyValues[item.ItemIndex]["Part"].ToString();
        }

protected void rgMaterials_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            ShowSortData();
        }

Can you help me why rgMaterials_NeedDataSource show the grid & rgMaterials_ItemInserted/rgMaterials_ItemUpdated/rgMaterials_ItemDeleted not fire.

Thanks,
Chandan
0
Shinu
Top achievements
Rank 2
answered on 23 Nov 2011, 05:28 AM
Hello Chandan,

You should never call DataBind() method when using advanced data-binding through NeedDataSource event. Make sure that you have data when populating the RadGrid. Also the events you mentioned are used in the case of Automatic Operations.

-Shinu.
0
Chandan Dey
Top achievements
Rank 1
answered on 24 Nov 2011, 12:29 AM
Hi Shinu,

Thanks for you quick reply. It works for me.

I have one query. Is it possible to maintain two separate column for Update and Cancel link or button while inline Editing or Add new record?

Thanks
Chandan 
0
Shinu
Top achievements
Rank 2
answered on 24 Nov 2011, 05:19 AM
Hello Chandan,

You can customize the edit form as explained in this demo.
Grid / Form Template Edit Form

-Shinu.
0
Mangal
Top achievements
Rank 1
answered on 13 Jun 2015, 12:26 AM

I need to access the value of  RadNumericTextBox in the iItemDataBound
 

 

Tags
Grid
Asked by
Torben
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Torben
Top achievements
Rank 1
Chandan Dey
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Mangal
Top achievements
Rank 1
Share this question
or