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

[Solved] Trap ItemCommand of Hyperlink radgrid column

3 Answers 401 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kerry
Top achievements
Rank 1
Kerry asked on 05 Aug 2009, 08:05 PM
Hello, I'd like to trap the click event of a RadGrid column that is defined as asp:HyperLink.  I need to set some server-side variables before redirecting the user to another webpage.  THis was very easy when the column was a ButtonColumn but I had to change to a Hyperlink , I would trap the ItemCommand event then test the e.CommandName arguement then set my variables and redirect the user, but it seems the ItemCommand event is not fired when I click the hyperlink.

Thanks

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Aug 2009, 04:27 AM
Hi Kerry,

One suggestion would be using LinkButton instead of HyperLink and set the CommandName for LinkButton, then in ItemCommand event check for e.CommandName to redirect the page explicitly.

ASPX:
 
<telerik:GridTemplateColumn UniqueName="TemplateColumn2"
    <ItemTemplate> 
        <asp:LinkButton ID="LinkButton1" CommandName="Redirect" Text="Click button" runat="server"></asp:LinkButton> 
    </ItemTemplate> 
</telerik:GridTemplateColumn> 

C#:
 
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    if(e.CommandName == "Redirect"
    { 
        Response.Redirect("http://www.google.com"); 
    } 

Thanks,
Princy.
0
Kerry
Top achievements
Rank 1
answered on 06 Aug 2009, 04:50 AM
Yes I see now, thanks..... I also notice that the ItemCommand event will not fire if I also set [onclick] attribute for the LinkButton....is there a way to have the ItemCommand event fire when there is a Javascript call on the [onclick] event.  I set a call to a Javascript fucntion on the ItemDataBound event.

Thanks

 

0
Shinu
Top achievements
Rank 2
answered on 06 Aug 2009, 07:09 AM
Hi Kerry,

I tried calling a JavaScript function on the clicking the linkbutton and it was firing the the ItemCommand event once the client side function returns true. Here is the sample code:

ASPX:
 
<telerik:GridTemplateColumn UniqueName="TempCol" HeaderText="TempCol" > 
                         <ItemTemplate> 
                          <asp:LinkButton ID="LinkButton1" CommandName="Redirect" Text="Click button" runat="server"  ></asp:LinkButton> 
                         </ItemTemplate> 
                        </telerik:GridTemplateColumn> 

CS:
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            LinkButton lnkbtn = (LinkButton)item["TempCol"].FindControl("LinkButton1"); 
            lnkbtn.Attributes.Add("onclick"," return Show();"); 
        } 
    } 

JS:
 
<script type="text/javascript" > 
 function Show() 
 { 
  if(confirm("You want to proceed?")) 
    return true
    else 
    return false
 } 
</script> 


Regards
Shinu

Tags
Grid
Asked by
Kerry
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Kerry
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or