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

Access GridDataItem containing RadToolBar in RadGrid

4 Answers 102 Views
ToolBar
This is a migrated thread and some comments may be shown as answers.
The Wire
Top achievements
Rank 1
The Wire asked on 28 Sep 2010, 05:30 PM
I have a RadToolBar (tbActions) within a GridTemplateColumn in a RadGrid.  When I click a button in tbActions, I want to access the ID column of the GridDataItem row that contains the tbActions that I just clicked in.  How can I get that reference?
<rad:RadGrid ...>
        <MasterTableView ...>
            <Columns>
                <rad:GridBoundColumn DataField="ID" UniqueName="ID" />
                <!--...more columns...-->
                <rad:GridTemplateColumn UniqueName="Actions">
                    <ItemTemplate>
                        <rad:RadToolBar ID="tbActions" Orientation="Horizontal" runat="server"
                            OnButtonClick="tbActions_OnButtonClick" >
                            <Items>
                                <rad:RadToolBarButton ImageUrl="images/button1.png" Value="1" />
                                <rad:RadToolBarButton ImageUrl="images/button2.png" Value="2" />
                                <rad:RadToolBarButton ImageUrl="images/button3.png" Value="3" />
                            </Items>
                        </rad:RadToolBar>
                    </ItemTemplate>
                </rad:GridTemplateColumn>
            </Columns>
            <DetailTables>
                ...            
            </DetailTables>
        </MasterTableView>
    </rad:RadGrid>

protected void tbActions_OnButtonClick(object sender, RadToolBarEventArgs e)
{
    //How can I get a reference to the GridDataItem that contains the toolbar
    //of the button that I just clicked?
    //this is not working:
    GridDataItem item = (GridDataItem)e.Item.ToolBar.Parent.Parent;
         string id;
    if (e.Item.Value == "1")
    {
          id = item["ID"].Text;
          //more code here, use ID for db update
 
    }
    else if (e.Item.Value == "2")
    {
          id = item["ID"].Text;
          //more code here, use ID for db update
    }
    else if (e.Item.Value == "3")
    {
          id = item["ID"].Text;
          //more code here, use ID for db update
    }
}

4 Answers, 1 is accepted

Sort by
0
Accepted
Cori
Top achievements
Rank 2
answered on 28 Sep 2010, 06:28 PM
I don't see a problem with your code. I tried your code myself as was able to get the GridDataItem object and retrieve the ID column's text.

Is your code failing at that point?
0
The Wire
Top achievements
Rank 1
answered on 28 Sep 2010, 07:17 PM
My mistake.  I had thought that line was the problem, but I found the problem in my "//more code here, use ID for db update" section.  I was trying to assign a value to sqldatasource parameter using the wrong parameter name.  I guess I just needed a lunch break to clear out my mind :)

Thanks!
0
The Wire
Top achievements
Rank 1
answered on 07 Oct 2010, 05:08 PM
Ok, how can I do this client side?

So far I have
function tbActionsClick(sender, args) {
  var item = args.get_item();
  if (item.get_value() == "1") {
    var tb = item.get_toolBar();
    //now if it's like server side, i need 2 calls to get_parent() to get the row
    var cell = tb.get_parent(); 
    var row = cell.get_parent();   
    var grid = row.get_masterTableView();
    var id = grid.getCellByColumnUniqueName(row, "ID"); 
  }
}

I don't think I'm getting the objects I expect when I assign row and cell.
0
The Wire
Top achievements
Rank 1
answered on 07 Oct 2010, 07:25 PM
I have a workaround that is fine for me, although I still am curious to know how to do what I was originally trying to do.

My workaround was to, in the server-side RadGrid OnItemDataBound event, assign attributes to the toolbar button.  These attributes included the ID value I wanted.
protected void RadGrid_OnItemDataBound(object sender, GridItemEventArgs e)
{
  GridDataItem item = (GridDataItem)e.Item;
  RadToolBar tbActions = (RadToolBar)item["Toolbar"].FindControl("tbActions");
    
  tbActions.Items[0].Attributes["ID"] = item["ID"].Text;
}

Then in client-side, I referenced the attributes collection of the button:
function tbActionsClick(sender, args) {
  var item = args.get_item();
  if (item.get_value() == "1") {
    var id = item.get_attributes().getAttribute("ID");
  }
}
Tags
ToolBar
Asked by
The Wire
Top achievements
Rank 1
Answers by
Cori
Top achievements
Rank 2
The Wire
Top achievements
Rank 1
Share this question
or