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

radgrid OnItemDataBound doesnt bind after Sorting

2 Answers 76 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nambi
Top achievements
Rank 1
Nambi asked on 04 Sep 2014, 08:35 AM
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 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 08 Sep 2014, 11:53 AM
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.

 
0
Nambi
Top achievements
Rank 1
answered on 10 Sep 2014, 08:45 AM
Hi Admin,
         Thanks for the code. The thing which worked is the use of item.GetDataKeyValue.
string Quotation_no = item.GetDataKeyValue("Quotation_no").ToString();
Tags
General Discussions
Asked by
Nambi
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Nambi
Top achievements
Rank 1
Share this question
or