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

[Solved] Manipulate the controls inside a template column

10 Answers 471 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 2
Kevin asked on 08 May 2008, 07:18 PM
I'm sure this is documented somewhere but I've searched high and low and I'm missing it.

I have a Grid with an unbound ItemTemplate column in it.  Inside of the ItemTemplate is a RadDatePicker.  I'm attempting to set the SharedCalendarId in the ItemCreated event. 

First, is this the best way to set the property inside of the grid.?
Second, I can't seem to get to a place where I can do a FindControl on the RadDatePicker in the ItemTemplate.  I can get column that I'm interested in but how do I get the controls collection within a given column.

10 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 09 May 2008, 05:50 AM
Hi,

Try accessing the RadDatePicker in a GridTemplateColumn as shown below.

CS:
   protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem dataitem = (GridDataItem)e.Item; 
            RadDatePicker radpkr = (RadDatePicker)dataitem["columnUniqueName"].FindControl("RadDatePicker1"); 
        } 
 
    } 

You can also refer the following KB article.
Using RadDatePicker/RadDateTimePicker as editor in template column of RadGrid

Thanks
Shinu.
0
Kevin
Top achievements
Rank 2
answered on 13 May 2008, 03:53 AM
One other thing that I'm attempting to do with this is add a required field validator to the column such that when the user clicks the "Add 2 Cart" button on the row, the Qty and Requested Date columns are client side validated.

It seems that this is a little unusual since the two data columns are unbound but I should be able to do this with the RadGrid and not resort to using a DataList.  My column definition for the Qty is:
<telerik:GridTemplateColumn HeaderText="Qty" UniqueName="Qty_Column" ItemStyle-Width="100px">
<ItemTemplate>
<telerik:RadNumericTextBox ID="Qty" Runat="server" MinValue="0"
ShowSpinButtons="True" Width="65px" EnableViewState="false">
<NumberFormat AllowRounding="True" DecimalDigits="0" GroupSeparator="" />
</telerik:RadNumericTextBox>
<asp:RequiredFieldValidator ID="qtyRequiredFieldValidator" runat="server" ControlToValidate="Qty" ErrorMessage="Must have a Qty" Text="*" />
</ItemTemplate>
</telerik:GridTemplateColumn>

The trouble is when I click on the Add 2 Cart button the required field validator is attempting to validate every row.

Do I need to change this to an EditItemTemplate?  If so how do I put every row into edit mode?

0
Princy
Top achievements
Rank 2
answered on 13 May 2008, 06:54 AM
Hi Kevin,

Check out the following help article to see how to put the entire Grid rows  in edit mode.
Put all items in edit mode without additional rebind

Princy.
0
Kevin
Top achievements
Rank 2
answered on 13 May 2008, 04:42 PM
Thank you for pointing this out to me but I'm still have two problems. 

First is that I'm implementing SEO paging on the grid and when I go to another page the rows are not put into the edit state.  I've attempted to move the code from the page_load to the end of the NeedDataSource event as well as loop from 0 to the VirtualItemCount but it doesn't work consistently.  I've tried it with ItemsPerPage as well as the VirtualItemCount.
        // Put the entire grid into edit mode 
        for (int i = 0; i < ProductListGrid.MasterTableView.VirtualItemCount; i++) 
        { 
            ProductListGrid.EditIndexes.Add(i); 
        } 
 

Second, on pages that do go into edit mode correctly,  the required field validator is fired for all rows when it only should be applied to the row that the button was clicked on.  Every row shows the error text.  I'm using the same markup as my prior message for this.

0
Princy
Top achievements
Rank 2
answered on 14 May 2008, 07:35 AM
Hi Kevin,

Can you try the following code snippet in the PreRender event and see whether it is working or not?

CS:
 protected void RadGrid1_PreRender(object sender, EventArgs e)  
    {  
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)  
        {  
            item.Edit = true;  
             
        }  
        RadGrid1.Rebind();  
    } 


Thanks
Princy.
0
Kevin
Top achievements
Rank 2
answered on 14 May 2008, 02:57 PM
<%@ Import namespace="System.ComponentModel"%> 
<%@ Control Language="c#" Inherits="Mediachase.eCF.Web.BaseClasses.CategoryBase" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<%@ Register Src="../../../../SharedModules/PriceModule.ascx" TagName="PriceModule" TagPrefix="uc2" %> 
<%@ Register Namespace="Mediachase.eCF.Web.UI.Controls" Assembly="Mediachase.eCommerce.WebUtility" TagPrefix="ecf" %> 
<%@ Register Src="../../../../SharedModules/EditorModules/SimpleEditor.ascx" TagName="SimpleEditor" TagPrefix="ecf" %> 
<%@ Register TagPrefix="ecf" TagName="ProductReviewRatingModule" Src="../../../../SharedModules/ProductReviewRatingModule.ascx" %> 
<%@ Register TagPrefix="ecf" TagName="ProductRatingModule" Src="../../../../SharedModules/ProductRatingModule.ascx" %> 
<%@ Register Src="../../../../SharedModules/ProductPagerModule.ascx" TagName="ProductPagerModule" TagPrefix="uc2" %> 
<%@ Register Src="../../../../SharedModules/CompareButtonModule.ascx" TagName="CompareButtonModule" TagPrefix="ecf" %> 
<%@ Register Src="~/SharedModules/BreadcrumbModule.ascx" TagName="BreadcrumbModule" TagPrefix="ecf" %> 
<%@ Register Src="../../Modules/SideMenuControl.ascx" TagName="SideMenu" TagPrefix="ecf" %> 
 
<script language="C#" runat="server"
    void Page_Load(Object sender, EventArgs e) 
    { 
        ApplyLocalization(); 
         
        if (ViewState["Items_TotalPages"] != null) 
            PageCount = Int32.Parse(ViewState["Items_TotalPages"].ToString()); 
        else 
        { 
            ViewState["Items_TotalPages"] = PageCount; 
        } 
 
    } 
 
    protected decimal GetRating(Item product) 
    { 
        if (product == null || product.CustomerReviews == null || !product.CustomerReviews.AverageRatingSpecified) 
            return 0; 
        else 
            return product.CustomerReviews.AverageRating; 
    } 
 
    protected void ApplyLocalization() 
    { 
//      if (ProductList != null && ProductList.Columns.Count > 1) 
//      { 
//          ProductList.Columns[0].HeaderText = RM.GetString("GENERAL_IMAGE_LABEL"); 
//          ProductList.Columns[1].HeaderText = RM.GetString("GENERAL_NAME_LABEL"); 
//          ProductList.DataBind();  
//      }  
    } 
 
    public virtual int ItemsPerPage 
    { 
        get 
        { 
            return 12; 
        } 
    } 
 
    public virtual int CurrentPage 
    { 
        get 
        { 
            return ClientHelper.GetIntegerValue(Request.QueryString["currentpage"], 0); 
        } 
    } 
 
    public int PageCount = 0
 
 
    public void PerformSearch() 
    { 
        int itemsPerPage = ItemsPerPage
        int TotalRecords = 0
 
//        Item[] skus = GetSkus(0, 0, ref TotalRecords); 
        int selectedPage = ProductListGrid.MasterTableView.CurrentPageIndex; 
        Item[] skus = GetSkus(ItemsPerPage, selectedPage, ref TotalRecords); 
 
        PageCount = (int)Math.Ceiling(((float)TotalRecords / (float)itemsPerPage)); 
        ViewState["Items_TotalPages"] = PageCount; 
 
        // Put the entire grid into edit mode 
//        for (int i = 0; i < TotalRecords; i++) 
//        { 
//            ProductListGrid.EditIndexes.Add(i); 
//        } 
         
        ProductListGrid.DataSource = skus
        ProductListGrid.MasterTableView.PageSize = ItemsPerPage
        ProductListGrid.MasterTableView.VirtualItemCount = TotalRecords
    } 
 
     
    protected void ProductListGrid_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "Add2Cart") 
        { 
            GridDataItem item = (GridDataItem) e.Item; 
            int strKey = Convert.ToInt32(item.GetDataKeyValue("Id")); 
            RadNumericTextBox qtyTextBox = item.FindControl("Qty") as RadNumericTextBox; 
            RadDatePicker requestDatePicker = item.FindControl("RequestDatePicker") as RadDatePicker; 
            if (qtyTextBox != null && requestDatePicker != null) 
            { 
                double? qtyValue = qtyTextBox.Value; 
                DateTime? requestDate = requestDatePicker.SelectedDate; 
                 
                if (qtyValue != null && requestDate != null) 
                { 
                    int qty = 0
                    qty = Convert.ToInt32(qtyValue.Value); 
 
                    AddressInfo ai = ClientContext.Context["TempAddressInfo"] as AddressInfo; 
                    //add it to the cart 
                    ClientCart.Add(strKey, qty, ai, requestDate.Value); 
                } 
                qtyTextBox.Value = null
                requestDatePicker.SelectedDate = null
            } 
        } 
    } 
 
    protected void ProductListGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    { 
        PerformSearch(); 
    } 
 
     
     
    protected void ProductListGrid_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem dataItem = (GridDataItem) e.Item; 
            dataItem.Edit = true
             
            RadDatePicker dp = (RadDatePicker)dataItem["RequestDate"].FindControl("RequestDatePicker"); 
            if (dp != null) 
                dp.SharedCalendar = SharedCalendar; 
 
//            if (e.Item.IsInEditMode) 
//            { 
//                GridEditableItem eeditableItem = e.Item as GridEditableItem; 
//                GridTemplateColumnEditor editor = 
//                    (GridTemplateColumnEditor) editableItem.EditManager.GetColumnEditor("Qty_Column"); 
//                editor.TemplateControl.ID = "ID_for_validation"
// 
//                TableCell cell = (TableCell) editor.TemplateControl.Parent; 
// 
//                RequiredFieldValidator validator = new RequiredFieldValidator(); 
//                validator.ErrorMessage = "*"
//                validator.ControlToValidate = editor.TemplateControl.ID; 
// 
//                cell.Controls.Add(validator); 
//            } 
        } 
        else 
        { 
            if (e.Item.ItemType == GridItemType.NoRecordsItem) 
                SharedCalendar.Visible = false
        } 
    } 
 
    protected void ProductListGrid_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in ProductListGrid.MasterTableView.Items) 
        { 
            item.Edit = true
        } 
    } 
</script> 
 
 
<table width="100%"
    <tr> 
        <td colspan="2"><ecf:BreadcrumbModule ID="BreadcrumbModule1" runat="server"></ecf:BreadcrumbModule></td
    </tr> 
    <tr> 
        <td valign="top"
         
<table style="width: 100%" cellpadding="0" cellspacing="0" id="Table1"
    <tr valign="top"
        <td style="width: 100%"
        <table cellspacing="0" cellpadding="0" style="width: 100%" border="0"
            <tbody> 
                <tr> 
                    <td style="width:287"
                        <ecf:MetaImage ID="PrimaryImage" PropertyName="Everything_CategoryPrimaryImage" DataSource="<%#Category.ItemAttributes.Image%>" runat="server" /> 
                    </td> 
                    <td style="width: 100%" valign="top"
                        <ecf:SimpleEditor runat="server" PropertyName="Everything_Description" DataSource="<%# Category%>" IdType="Category"></ecf:SimpleEditor> 
                    </td> 
                </tr> 
            </tbody> 
        </table> 
        </td> 
    </tr> 
    <tr> 
        <td style="width: 100%" align="left"
            <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" EnableHistory="true"
                <AjaxSettings> 
                    <telerik:AjaxSetting AjaxControlID="ProductListGrid"
                        <UpdatedControls> 
                            <telerik:AjaxUpdatedControl ControlID="ProductListGrid"  
                                LoadingPanelID="MasterRadAjaxLoadingPanel" /> 
                            <telerik:AjaxUpdatedControl ControlID="SharedCalendar" /> 
                        </UpdatedControls> 
                    </telerik:AjaxSetting> 
                </AjaxSettings> 
            </telerik:RadAjaxManager>             
                 
            <telerik:RadGrid ID="ProductListGrid" runat="server" 
                GridLines="None" Width="800px"  
                OnNeedDataSource="ProductListGrid_NeedDataSource"  
                AutoGenerateColumns="False"  
                onitemcommand="ProductListGrid_ItemCommand" 
                AllowPaging ="true" AllowCustomPaging="true" 
                AllowMultiRowEdit="true" AllowMultiRowSelection="true" 
                onitemcreated="ProductListGrid_ItemCreated"  
                onprerender="ProductListGrid_PreRender"
                <MasterTableView DataKeyNames="Id" EnableNoRecordsTemplate="true" EditMode="InPlace"
                    <NoRecordsTemplate> 
                        There are no available products in this category. 
                    </NoRecordsTemplate> 
                    <PagerStyle NextPageText="Next" PrevPageText="Prev" Mode="NextPrevAndNumeric"></PagerStyle> 
                    <Columns> 
                        <telerik:GridBoundColumn HeaderText="ID" DataField='Id' Visible="false" ReadOnly="true"></telerik:GridBoundColumn> 
                        <telerik:GridTemplateColumn HeaderText="Qty" UniqueName="Qty_Column" ItemStyle-Width="100px"
                            <EditItemTemplate> 
                                <telerik:RadNumericTextBox ID="Qty" Runat="server" MinValue="0"  
                                    ShowSpinButtons="True" Width="65px" EnableViewState="false"
                                    <NumberFormat AllowRounding="True" DecimalDigits="0" GroupSeparator="" /> 
                                </telerik:RadNumericTextBox> 
                                <%-- asp:RequiredFieldValidator ID="qtyRequiredFieldValidator" runat="server" ControlToValidate="Qty" ErrorMessage="Must have a Qty" Text="*" EnableClientScript="true" / --%> 
                            </EditItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn HeaderText=""
                            <ItemTemplate> 
                                <asp:Label ID="UnitOfMeasureLabel" runat="server" Text='<%# GetMetaField("UofM", (Item)Container.DataItem) %>' /> 
                            </ItemTemplate>                         
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn HeaderText="Part Number"
                            <ItemTemplate> 
                                <asp:HyperLink ID="ProductHyperLink" runat="server" NavigateUrl='<%# ClientHelper.FormatUrl("product", GetMetaField("ProductId", (Item)Container.DataItem), DataBinder.Eval(Category, "SiteNodeId"), GetMetaField("Name", (Item)Container.DataItem)) %>'><h3><%# GetMetaField("Name", (Item)Container.DataItem)%></h3></asp:HyperLink> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn HeaderText="Description"
                            <ItemTemplate> 
                                <asp:Label ID="DescriptionLabel" runat="server" Text='<%# GetMetaField("Description", (Item)Container.DataItem) %>' /> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn HeaderText="Code" Visible="false"
                            <ItemTemplate> 
                                <asp:Label ID="CodeLabel" runat="server" Text='<%# GetMetaField("Code", (Item)Container.DataItem) %>' /> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn HeaderText="Product Id" Visible="false"
                            <ItemTemplate> 
                                <asp:Label ID="ProductIdLabel" runat="server" Text='<%# GetMetaField("ProductId", (Item)Container.DataItem) %>' /> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn HeaderText="Availability" Visible="false"
                            <ItemTemplate> 
                                <asp:Label ID="AvailabilityLabel" runat="server" Text='<%# GetMetaField("Availability", (Item)Container.DataItem) %>' /> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn UniqueName="RequestDate" HeaderText="Request Date"
                            <ItemTemplate> 
                                <telerik:RadDatePicker ID="RequestDatePicker" Runat="server" Width="80px"  
                                    MinDate="<%# DateTime.Now.Date %>"
                                    <DateInput runat="server" InvalidStyleDuration="100"
                                    </DateInput> 
                                    <Calendar runat="server" /> 
                                </telerik:RadDatePicker> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn HeaderText="Retail Price"
                            <ItemTemplate> 
                                <asp:Label ID="ListPriceLabel" runat="server" Text='<%# ((Item) Container.DataItem).Offers.Offer[0].OfferListing[0].Price.FormattedPrice %>' /> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn HeaderText="Your Price"
                            <EditItemTemplate> 
                                <asp:Label ID="YourPriceLabel" runat="server" /> 
                                <asp:Button ID="CheckPriceButton" runat="server" Text="Check Price" CausesValidation="true" /> 
                            </EditItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn> 
                            <EditItemTemplate> 
                                <asp:Button ID="Add2Cart" runat="server" Text="Add to Cart" CommandName="Add2Cart" /> 
                            </EditItemTemplate> 
                        </telerik:GridTemplateColumn> 
                    </Columns> 
                </MasterTableView> 
            </telerik:RadGrid> 
            <telerik:RadCalendar ID="SharedCalendar" ShowColumnHeaders="false" ShowRowHeaders="false" runat="server"/> 
        </td> 
    </tr> 
</table> 
 
        </td> 
    </tr> 
</table> 
 
 
 

I tried your suggestion.  Unfortunately the item doesn't want to go into edit mode.  Immediately after setting the Edit property to true when I check the item.IsInEditMode property and it remains false.  This stuff drives me crazy :)


0
Iana Tsolova
Telerik team
answered on 14 May 2008, 03:44 PM
Hi Kevin,

Please try rebinding the grid after putting the desired items in edit mode and let us know of the results:

    protected void ProductListGrid_PreRender(object sender, EventArgs e)    
    {    
        foreach (GridDataItem item in ProductListGrid.MasterTableView.Items)    
        {    
            item.Edit = true;    
        }  
        ProductListGrid.Rebind();  
    }    
 


Greetings,
Iana
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Kevin
Top achievements
Rank 2
answered on 14 May 2008, 04:03 PM
Well it works when I put all of the code in!  Thank you.  You all have amazing patience and skills.

But is there some reason that the documented practice that Princy originally pointed to for putting the grid into edit mode didn't work?  And could that be the reason why the RequiredFieldValidators still fire every row when only one of them should?
0
Kevin
Top achievements
Rank 2
answered on 15 May 2008, 05:18 PM
Any thoughts on how to make the required field validator work on a per row basis.  I attempted to set a validation group per row in the the ItemCreated event but at that point the row is not in edit mode and it doesn't work properly.

???
0
Princy
Top achievements
Rank 2
answered on 16 May 2008, 06:43 AM
Hi Kevin,

Go through the following forum discussion.
How to customize a GridColum with built-in (default) validators

Princy.
Tags
Grid
Asked by
Kevin
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Kevin
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Iana Tsolova
Telerik team
Share this question
or