Access GridDataItem containing RadToolBar in RadGrid

5 posts, 1 answers
  1. The Wire
    The Wire avatar
    21 posts
    Member since:
    Jan 2010

    Posted 28 Sep 2010 Link to this post

    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
        }
    }
  2. Answer
    Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 28 Sep 2010 Link to this post

    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?
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. The Wire
    The Wire avatar
    21 posts
    Member since:
    Jan 2010

    Posted 28 Sep 2010 Link to this post

    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!
  5. The Wire
    The Wire avatar
    21 posts
    Member since:
    Jan 2010

    Posted 07 Oct 2010 Link to this post

    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.
  6. The Wire
    The Wire avatar
    21 posts
    Member since:
    Jan 2010

    Posted 07 Oct 2010 Link to this post

    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");
      }
    }
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017