GridButtonColumn - use custom event?

9 posts, 0 answers
  1. Marty
    Marty avatar
    61 posts
    Member since:
    Oct 2008

    Posted 14 Jan 2009 Link to this post

    I have a grid that is used to display meta-data for files that have been uploaded to my app.  I have two GridButtonColumns in the grid: one to Delete the doc shown on the row, and another to Save/View the doc.

    I got the Delete command to work fine using the Command="Delete" attribute on the GridButtonColumn (shown below).  However, I'm having some difficulty telling the grid to use a custom handler that I've written on the second GridButtonColumn.  All I need to know how to do is to assign a custom handler to the GridButtonColumn where I can then ignore the validators on the page and open the file.

    Any input is greatly appreciated.  Thanks.

    Marty

    Grid:

    <telerik:RadGrid ID="GrdDocs" runat="server" GridLines="None" ShowGroupPanel="True" 
                                                                            AutoGenerateColumns="False" AllowPaging="True" OnNeedDataSource="GrdDocs_OnNeedDataSource" 
                                                                            OnDeleteCommand="GrdDocs_OnDelete">  
                                                                            <HeaderContextMenu EnableTheming="True">  
                                                                                <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                                                                            </HeaderContextMenu> 
                                                                            <MasterTableView DataKeyNames="DocumentID">  
                                                                                <RowIndicatorColumn> 
                                                                                    <HeaderStyle Width="20px"></HeaderStyle> 
                                                                                </RowIndicatorColumn> 
                                                                                <ExpandCollapseColumn> 
                                                                                    <HeaderStyle Width="20px"></HeaderStyle> 
                                                                                </ExpandCollapseColumn> 
                                                                                <Columns> 
                                                                                    <telerik:GridBoundColumn DataField="DocumentID" EmptyDataText="&amp;nbsp;" HeaderText="Document ID" 
                                                                                        UniqueName="column" Visible="False">  
                                                                                    </telerik:GridBoundColumn> 
                                                                                    <telerik:GridBoundColumn DataField="FileName" EmptyDataText="&amp;nbsp;" HeaderText="File Name" 
                                                                                        UniqueName="column1">  
                                                                                    </telerik:GridBoundColumn> 
                                                                                    <telerik:GridBoundColumn DataField="DateUploaded" EmptyDataText="&amp;nbsp;" HeaderText="Date Uploaded" 
                                                                                        UniqueName="column2">  
                                                                                    </telerik:GridBoundColumn> 
                                                                                    <telerik:GridBoundColumn DataField="AddedBy" EmptyDataText="&amp;nbsp;" HeaderText="Added By" 
                                                                                        UniqueName="column3">  
                                                                                    </telerik:GridBoundColumn> 
                                                                                    <telerik:GridButtonColumn ButtonType="LinkButton" Text="Delete" UniqueName="colDelete" 
                                                                                        CommandName="Delete">  
                                                                                    </telerik:GridButtonColumn> 
                                                                                    <telerik:GridButtonColumn ButtonType="LinkButton" Text="Save/View" UniqueName="colSave" >
                                                                                          
                                                                                    </telerik:GridButtonColumn> 
                                                                                  
                                                             
                                                                  
                                                                                </Columns> 
                                                                            </MasterTableView> 
                                                                            <FilterMenu EnableTheming="True">  
                                                                                <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                                                                            </FilterMenu> 
                                                                        </telerik:RadGrid> 
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 14 Jan 2009 Link to this post

    Hello Marty,

    You can access the button in the GridButtonColumn from the server side and then add a handler to the button as shown below:
    cs:
     protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridDataItem) 
            { 
                GridDataItem dataItem = (GridDataItem)e.Item;           
                Button btn1 = (Button)dataItem["ButtonColumnUniqueName"].Controls[0]; 
                btn1.Click += new EventHandler(btn1_Click); 
            } 
        } 
     
        void btn1_Click(object sender, EventArgs e) 
        { 
             
        } 

    Thanks
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Marty
    Marty avatar
    61 posts
    Member since:
    Oct 2008

    Posted 15 Jan 2009 Link to this post

    Thanks for your response.

    It makes sense that it would work like that, however, when I tried it this morning, I get "Unable to cast object of type 'Telerik.Web.UI.GridLinkButton' to type 'System.Web.UI.WebControls.Button'." on this line:
     Button btnSaveView = (Button)dataItem["colSave"].Controls[0]; 


    Any thoughts?

    I did find a way to get this working using built-in commands... however, I'd still like to see this solution through as well.

    Thanks,

    Marty
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 15 Jan 2009 Link to this post

    Hi Marty,

    Try the following line of code and see if it is working.

    LinkButton btnSaveView = (LinkButton)dataItem["colSave"].Controls[0]; 

    Regards
    Shinu
  6. Marty
    Marty avatar
    61 posts
    Member since:
    Oct 2008

    Posted 16 Jan 2009 Link to this post

    That cast did work.  However, I was then having difficulty, in my custom event handler, in accessing the row of the item that the button was clicked in.

    So, I did it using a custom grid command by setting my command name to "View" on the GridButtonColumn and the 'OnItemCommand' grid attribute to the method below:
    protected void GrdDocs_OnItemCommand(object sender, GridCommandEventArgs e)  
        {  
            if (e.CommandName == "View")  
            {  
                GridDataItem item = (GridDataItem)e.Item;   
                string str = item.GetDataKeyValue("DocumentID").ToString();  
                string URLToOpen = "";  // CTS.ChangeControl.DAL.cCustomerSettings.GetInternalURL();  
     
                if (InHistoryView())  
                    URLToOpen += "loadDoc.aspx?isHistory=true&documentID=" + str;  
     
                else 
                    URLToOpen += "loadDoc.aspx?isHistory=false&documentID=" + str;  
     
     
                ScriptManager.RegisterStartupScript(thisthis.GetType(), "key""window.open('" + URLToOpen + "','_blank');"true);  
            }  
        } 

    This works out well.

    However, if you still want to tell me how I'd access the row element, the more I know couldn't hurt.

    Thanks for the info.

    Marty
  7. Nandan
    Nandan avatar
    15 posts
    Member since:
    Jun 2010

    Posted 10 Jun 2010 Link to this post

    Hi,
    I used the following Markup

    <telerik:GridButtonColumn ButtonType="LinkButton" Text="Save/View" CommandName="View"  UniqueName="btnView">

    </telerik:GridButtonColumn>

    And the Event I am writing

     

     

    protected void grdReviewRequest_OnItemCommand(object sender, GridCommandEventArgs e)

     

    {

     

     

    if (e.CommandName == "View")

     

    {

     

     

    GridDataItem item = (GridDataItem)e.Item;

     

     

     

    string str = item.GetDataKeyValue("DocumentID").ToString();

     

     

     

    string URLToOpen = ""; // CTS.ChangeControl.DAL.cCustomerSettings.GetInternalURL();

     

    URLToOpen +=

     

    "AdmReviewRetailEnrollment.aspx?isHistory=true&documentID=" + str;

     

     

     

    ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "window.open('" + URLToOpen + "','_blank');", true);

     

    }

    }


    but when i click on that button the event is not getting fired. Please let me know where i m missing

  8. paul
    paul avatar
    4 posts
    Member since:
    Jul 2010

    Posted 25 Jul 2010 Link to this post

    looks like you're missing an OnItemCommand when setting up the RadGrid item...

    <telerik:GridButtonColumn ID="RadGrid1" runat="server" .... OnItemCommand="grdReviewRequest_OnItemCommand">
  9. Jason
    Jason avatar
    26 posts
    Member since:
    Nov 2006

    Posted 23 Nov 2010 Link to this post

    For those who still want to use Princy's technique of adding a custom event handler to the Button (or LinkButton) but need to access the row data item, you can get to it through the following.
    void btn1_Click(object sender, EventArgs e)
    {
        GridDataItem dataItem = ((GridDataItem)(((WebControl)sender).Parent.Parent));
     
        ///etc
    }
  10. Naveen
    Naveen avatar
    3 posts
    Member since:
    Aug 2012

    Posted 23 Aug 2012 Link to this post

    Nandan..It worked wonders thanks....
    Also..we have to add OnItemCommand  property to the grid.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017