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

GridButtonColumn - use custom event?

8 Answers 573 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marty
Top achievements
Rank 1
Marty asked on 14 Jan 2009, 08:36 PM

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> 

8 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 15 Jan 2009, 04:34 AM
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.
0
Marty
Top achievements
Rank 1
answered on 15 Jan 2009, 12:36 PM
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
0
Shinu
Top achievements
Rank 2
answered on 16 Jan 2009, 04:07 AM
Hi Marty,

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

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

Regards
Shinu
0
Marty
Top achievements
Rank 1
answered on 16 Jan 2009, 12:57 PM
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
0
Nandan
Top achievements
Rank 1
answered on 10 Jun 2010, 10:17 AM
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

0
paul
Top achievements
Rank 1
answered on 26 Jul 2010, 02:02 AM
looks like you're missing an OnItemCommand when setting up the RadGrid item...

<telerik:GridButtonColumn ID="RadGrid1" runat="server" .... OnItemCommand="grdReviewRequest_OnItemCommand">
0
Jason
Top achievements
Rank 1
answered on 24 Nov 2010, 02:40 AM
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
}
0
Naveen
Top achievements
Rank 1
answered on 23 Aug 2012, 06:47 PM
Nandan..It worked wonders thanks....
Also..we have to add OnItemCommand  property to the grid.
Tags
Grid
Asked by
Marty
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Marty
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Nandan
Top achievements
Rank 1
paul
Top achievements
Rank 1
Jason
Top achievements
Rank 1
Naveen
Top achievements
Rank 1
Share this question
or