ItemDataBound FindControl in template not working

9 posts, 0 answers
  1. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 08 Sep 2010 Link to this post

    Hi,

    For this:

    <telerik:GridTemplateColumn 
        UniqueName="Value" 
        HeaderText="Values">      
        <HeaderTemplate>     
            <asp:Label 
                Text="Value" 
                runat="server">
            </asp:Label>     
        </HeaderTemplate>     
        <ItemTemplate>     
            <asp:Label 
                ID="CurrentValues" 
                runat="server" 
                Text='<%# Eval("Values") %>'>
            </asp:Label>     
        </ItemTemplate>     
        <EditItemTemplate>     
            <telerik:RadListBox 
                ID="Values" 
                runat="server" 
                DataTextField="ValueTitle" 
                DataValueField="ValueId">
            </telerik:RadListBox>     
        </EditItemTemplate>     
    </telerik:GridTemplateColumn>

    On firing, OnItemDataBound, this works:

    var gridEditFormItem = (GridEditFormItem)e.Item;
      
    var radListBox = (RadListBox)gridEditFormItem["Value"].FindControl("Values");

    But this does not (Label is null):

    var label = (Label)gridEditFormItem["Value"].FindControl("CurrentValues");

    I can't see the wood for the trees, I think. Why can't I get a reference to the label?

    Thanks,
    Richard
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 09 Sep 2010 Link to this post

    Hello Richard,
    Since your the label control is inside the item template ,you can try the following code to access the label control.
    C#:

    if(e.Item is GridDataItem)
            {
                GridDataItem item=(GridDataItem)e.Item;
                var label = (Label)item["Value"].FindControl("CurrentValues");
                label.Text = "test";
            }

    Regards,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 09 Sep 2010 Link to this post

    Hi, thanks, I can get a reference using that approach.

    However, I want to get the text from the label and use it to populate a multi-select listbox and I can't seem to get the two to co-exist.

    Here's an example:

    protected void MyGrid_OnItemDataBound(object sender, GridItemEventArgs e)
    {
        if ((e.Item is GridEditFormItem) && e.Item.IsInEditMode)
        {
            var gridEditFormItem = (GridEditFormItem)e.Item;
      
            var radListBox = (RadListBox)gridEditFormItem["Value"].FindControl("Values");
      
            var valueData = new ValueData();
      
            var values = valueData.Select();
      
            TelerikHelper.BindRadListBox(radListBox, values, false);
        }
      
        if (e.Item is GridDataItem)
        {
            GridDataItem gridDataItem = (GridDataItem)e.Item;
      
            var label = (Label)gridDataItem["Value"].FindControl("CurrentValues");
      
            // label.Text = "Category 1,Category 2"
      
            /* I want to do this...
             
            var radListBox = (RadListBox)someItem["Value"].FindControl("Values");
      
            string[] currentValues = label.Text.Split(',');
      
            foreach (var value in currentValues)
            {
                radListBox.FindItemByText(value).Selected = true;
            }
            */
        }
    }

    Seems so simple, doesn't it? :) Any help in merging the two together would be great.

    Thanks,
    Richard

  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 09 Sep 2010 Link to this post

    Hello,


    You can access the values using following code and select the corresponding items in RadListBox.

    CS:
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if ((e.Item is GridEditFormItem) && e.Item.IsInEditMode)
            {
                var gridEditFormItem = (GridEditFormItem)e.Item;
                RadListBox radListBox = (RadListBox)gridEditFormItem["Value"].FindControl("Values");
                // Get the value
                string values = gridEditFormItem.ParentItem["Values"].Text;
                // Your logic to select the ListItems       
            
             . . .
    }




    Thanks,
    Princy.
  6. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 09 Sep 2010 Link to this post

    Hi Princy, I already tried that but ParentItem is null and throws an exception:

    System.NullReferenceException was unhandled by user code
      Message="Object reference not set to an instance of an object."
      Source="My.NameSpace"
      StackTrace:
           at My.NameSpace..InvestigationsGrid_OnItemDataBound(Object sender, GridItemEventArgs e) in C:\FilePath\File.aspx.cs:line 457
           at Telerik.Web.UI.RadGrid.OnItemDataBound(GridItemEventArgs e)
           at Telerik.Web.UI.RadGrid.CallOnItemDataBound(GridItemEventArgs e)
           at Telerik.Web.UI.GridEditFormItem.SetupItem(Boolean dataBind, Object dataItem, GridColumn[] columns, ControlCollection rows)
           at Telerik.Web.UI.GridTableView.CreateInsertItem(Boolean useDataSource, GridEnumerableBase resolvedDataSource, ControlCollection rows)
           at Telerik.Web.UI.GridTableView.CreateTopInsertItem(Boolean useDataSource, GridEnumerableBase resolvedDataSource, GridTHead tHead)
           at Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource)
           at Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource)
           at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
           at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
           at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
           at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
           at Telerik.Web.UI.GridTableView.PerformSelect()
           at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
           at Telerik.Web.UI.GridTableView.DataBind()
           at Telerik.Web.UI.GridTableView.Rebind()
           at Telerik.Web.UI.GridTableView.InsertItem(IDictionary newValues)
           at My.NameSpace..InsertEmptyRecord(String radGridId, GridCommandEventArgs e) in C:\FilePath\File.aspx.cs:line 1671
           at My.NameSpace..Grid_OnItemCommand(Object source, GridCommandEventArgs e) in C:\FilePath\File.aspx.cs:line 251
           at Telerik.Web.UI.RadGrid.OnItemCommand(GridCommandEventArgs e)
           at Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e)
           at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
           at Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e)
           at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
           at Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e)
           at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
           at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
           at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
           at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
           at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
           at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
           at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
      InnerException: 

    It's driving me crazy! I'm sure it must be something simple I overlooked but I can't think what it could be.

    Richard
  7. Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 15 Sep 2010 Link to this post

    Hello Richard,

    I tested the approach offered by Princy in a small sample project and it works fine with a column definition similar to yours. Could you please take a look at it and let me know what am I missing out from your scenario?

    Also, if you still cannot find a solution, you could send us your code through a formal support ticket, so we can take a closer look at your logic and find out what causes the erroneous behavior faster.

    Regards,
    Tsvetina
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 15 Sep 2010 Link to this post

    In the end, the only way I could get it to work was as follows:

    namespace MyNamespace
    {
        public partial class MyPage : System.Web.UI.Page
        {
            private string currentValues;
      
            protected void ValuesGrid_OnItemDataBound(object sender, GridItemEventArgs e)
            {
                if ((e.Item is GridEditFormItem) && e.Item.IsInEditMode)
                {
                    var gridEditFormItem = (GridEditFormItem)e.Item;
      
                    var radListBox = (RadListBox)gridEditFormItem["Value"].FindControl("Values");
      
                    var valueData = new ValueData();
      
                    var values = valueData.Select();
      
                    TelerikHelper.BindRadListBox(radListBox, values, false);
      
                    if (this.currentValues != null)
                    {
                        radListBox = (RadListBox)gridEditFormItem["Value"].FindControl("Values");
      
                        string[] currentValues = this.currentValues.Split(',');
      
                        foreach (var value in currentValues)
                        {
                            radListBox.FindItemByText(value).Selected = true;
                        }
                    }
                }
      
                if (e.Item is GridDataItem)
                {
                    if (e.Item.OwnerTableView.IsItemInserted)
                    {
                        this.currentValues = null;
                    }
                    else
                    {
                        var gridDataItem = (GridDataItem)e.Item;
      
                        var label = (Label)gridDataItem["Value"].FindControl("CurrentValues");
      
                        if (label != null)
                        {
                            this.currentValues = label.Text;
                        }
                    }
                }
            }
        }
    }

    currentValues is just a comma delimited string that comes out from the database (e.g. "this,that,those").

    It works really well for me and as the project is a big one with many other issues to address, I'm happy at that :)

    Regards,
    Richard
  9. Sonny
    Sonny avatar
    5 posts
    Member since:
    Jun 2014

    Posted 17 Jun 2014 Link to this post

    How would you do this when the grid's EditMode="Batch" and with a TextBox?
  10. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 18 Jun 2014 in reply to Sonny Link to this post

    Hi Sonny,

    Please take a look into this help documentation  which discuss about the method of accessing ItemTemplate and EditItemTemplate control of RadGrid in Batch edit mode.

    Thanks,
    Shinu.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017