radgrid OnItemDataBound doesnt bind after Sorting

3 posts, 0 answers
  1. Nambi
    Nambi avatar
    6 posts
    Member since:
    Sep 2011

    Posted 04 Sep 2014 Link to this post

    Hi,
               I have a rad grid which has three columns ie, one link button and two labels. Link button should be enabled based on some logic. So i did this in
    OnItemDataBound of the RadGrid and it was working fine when the page loads first time. But when i do any sorting by clicking on one of the columns
    the link enabling and disabling logic doesnt reflect in UI. Ie, the first column not getting binded after OnItemDataBound.


    .aspx.cs:
    protected void radgrid1_OnNeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
            {
                FillGrid();

            }

     protected void radgrid1_OnItemDataBound(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridDataItem)
                {
                    GridDataItem item = (GridDataItem)e.Item;
                    LinkButton btnView = (LinkButton)item.FindControl("btnView");
                    DataRowView v = (DataRowView)e.Item.DataItem;
                    string Quotation_no = v.DataView[e.Item.ItemIndex]["Quotation_no"].ToString();
                    string str= btnView.Enabled.ToString();
                    
                    if (Quotation_no.Trim().Equals("1"))
                    {                   
                            btnView.Enabled = false;
                            btnView.Text = Quotation_no + "gg";             
                    }
                    else
                    {               
                         btnView.Enabled = true;
                         btnView.Text = Quotation_no + "mm";
                    }

                }
            }

    .aspx:
            <telerik:RadGrid runat="server" ID="radgrid1" AutoGenerateColumns="False" AllowSorting="true"
                AllowPaging="true" OnNeedDataSource="radgrid1_OnNeedDataSource" OnItemDataBound="radgrid1_OnItemDataBound"
                OnSortCommand="radGrid1_SortCommand">
                <MasterTableView Width="100%" PagerStyle-AlwaysVisible="true" EnableNoRecordsTemplate="true"
                    ShowHeadersWhenNoRecords="false">
                    <NoRecordsTemplate>
                        No Data Found.
                    </NoRecordsTemplate>
                    <Columns>
                        <telerik:GridTemplateColumn UniqueName="Quotation_no" HeaderText="Quotation_no">
                            <ItemTemplate>
                                <asp:LinkButton ID="btnView" runat="server"></asp:LinkButton>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn UniqueName="lob_desc" HeaderText="Decription" SortExpression="lob_desc">
                            <ItemTemplate>
                                <asp:Label ID="lblDesc" runat="server" Text='<%# Eval("lob_desc") %>'></asp:Label>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn UniqueName="lob_desc" HeaderText="Units" SortExpression="UNITS">
                            <ItemTemplate>
                                <asp:Label ID="lblUnits" runat="server" Text='<%# Eval("UNITS") %>'></asp:Label>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                    <PagerStyle AlwaysVisible="True"></PagerStyle>
                </MasterTableView>
            </telerik:RadGrid>
  2. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 08 Sep 2014 Link to this post

    Hi Nambi,

    I tried to reproduce the described issue, but to no avail. When sorting is performed the ItemDataBound is fired. On the following link I attached a small video which demonstrates that. Additionally to achieve the desired functionality I suggest you to change the code as following. Add Quotation_no as DataKeyNames:
    <MasterTableView Width="100%" PagerStyle-AlwaysVisible="true" EnableNoRecordsTemplate="true"
                ShowHeadersWhenNoRecords="false" DataKeyNames="Quotation_no">
                <NoRecordsTemplate>
                    No Data Found.
                </NoRecordsTemplate>
                <Columns>
    Change the code in ItemDataBound as following:
    protected void radgrid1_OnItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                LinkButton btnView = (LinkButton)item.FindControl("btnView");
                string Quotation_no = item.GetDataKeyValue("Quotation_no").ToString();
                if (Quotation_no.Trim().Equals("1"))
                {
                    btnView.Enabled = false;
                    btnView.Text = Quotation_no + "gg";
                }
                else
                {
                    btnView.Enabled = true;
                    btnView.Text = Quotation_no + "mm";
                }
     
            }
        }

    Additionally I am sending you a simple example which is based on your code and contains the described changes. Please check it out and let me know if it helps you.

    Regards,
    Radoslav
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Nambi
    Nambi avatar
    6 posts
    Member since:
    Sep 2011

    Posted 10 Sep 2014 Link to this post

    Hi Admin,
             Thanks for the code. The thing which worked is the use of item.GetDataKeyValue.
    string Quotation_no = item.GetDataKeyValue("Quotation_no").ToString();
Back to Top