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

[Solved] Dynamically hiding a button in Nested View from code behind

3 Answers 254 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ulonda
Top achievements
Rank 1
Ulonda asked on 21 May 2013, 09:42 PM
Hello,

     I am trying to dynamically hide a button that is contained in a nested view contained in each row on a telerik grid.

The grid represents a series of transactions, each with a transaction date. Upon expanding a row on the grid the button should be hidden for that particular row if the transaction date for that row is older than today. If the transaction date for that record is = today, then the button should appear,

I have tried the ItemCommand method, checking for ExpandCollapse event

if (e.CommandName == RadGrid.ExpandCollapseCommandName && !e.Item.Expanded)
 
This gets me the event that is firing for row expand. At this point, however, my data is not bound to the nested view for that row on the grid so I can't involve logic to hide based on a date for this particular row.

I will note that I am setting a series of labels on the nested view for the row - and the transaction date is one of the values. I tried accessing that label's text for my date logic but it is not set at the time of the "Expand" in itemCommand. If I expand, collapse, and expand again the data is there - so there is a timing issue with my data in using itemCommand.

In general which event is preferred to access controls in a Nested View and what methods could I employ to get at the data associated with the nested view? Would a client side approach be easier? 

Nested view settings:
<NestedViewSettings DataSourceID="SqlDataSourceInnerTable">
                <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="TRAN_ID" MasterKeyField="TRAN_ID" />
                </ParentTableRelation>
 </NestedViewSettings>

Datasource:
<asp:SqlDataSource ID="SqlDataSourceInnerTable"
ConnectionString="<%$ ConnectionStrings:LAWConnectionString %>"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM TRANS_HIST AS TH INNER JOIN
                         TRANS_TYPE AS TT ON TH.TRANS_TYPE_ID = TT.TRANS_TYPE_ID INNER JOIN
                         TRANS_STATUS ON TH.STATUS_ID = TRANS_STATUS.STATUS_ID LEFT OUTER JOIN
                         TRANS_SUBTYPE ON TH.TRANS_SUBTYPE_ID = TRANS_SUBTYPE.TRANS_SUBTYPE_ID
         WHERE (TH.TRAN_ID = @TRAN_ID)"
runat="server">
        <SelectParameters>

3 Answers, 1 is accepted

Sort by
0
Ulonda
Top achievements
Rank 1
answered on 21 May 2013, 11:03 PM
Update:

I have tried using ItemCommand and adding the transaction date I need to access as a key. I now have the date I need per row but am having problems accessing the nested item. The below is only retrieving the first nested row. How can I access the nested item that is expanding?

I tried (GridNestedViewItem) e.Item for my item but no luck.


protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
     if
(e.CommandName == RadGrid.ExpandCollapseCommandName && !e.Item.Expanded)
     {
     var requestDatetime = (DateTime)dataItem.GetDataKeyValue("REQUEST_DATETIME");
     var nesteditem = (GridNestedViewItem)RadGrid1.MasterTableView.GetItems(GridItemType.NestedView)[0];
     var btn = (Button)nesteditem.FindControl("btnDeleteTrans");
 
     if (requestDatetime.Date < DateTime.Now.Date)
         {
               btn.Visible = false;   
         }
         else
         {
               btn.Visible = true;   
         }
   }
}
0
Princy
Top achievements
Rank 2
answered on 22 May 2013, 12:09 PM
Hi Ulonda,

Please try if this helps you and place this code in the PreRender event of your program.

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
          
        if (item.Expanded) //checking if the row is expanded
        {
            var requestDatetime = (DateTime)item.GetDataKeyValue("REQUEST_DATETIME");
            if (requestDatetime.Date < DateTime.Now.Date)
            {
                ((GridDataItem)item).ChildItem.FindControl("btnDeleteTrans").Visible = false;
            }
           else
            {
                ((GridDataItem)item).ChildItem.FindControl("btnDeleteTrans").Visible = true;
            }
        }
    }
}

Thanks
Princy
0
Ulonda
Top achievements
Rank 1
answered on 22 May 2013, 01:54 PM
That worked Princy. Thanks!
Tags
Grid
Asked by
Ulonda
Top achievements
Rank 1
Answers by
Ulonda
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or