+1-888-365-2779
Try Now
More in this section

Forums / Deployment / Gridview RowCommand

Gridview RowCommand

2 posts, 0 answered
  1. Asit
    Asit avatar
    87 posts
    Registered:
    07 Nov 2009
    15 Dec 2009
    Link to this post
    Hi i m using a gridview in a user control and i m using this user control in a new page that i created in sitefinity.

    but the RowCommand event of gridview is not firing.
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    15 Dec 2009
    Link to this post
    Hello asit pani,

    Thank you for using our services.

    The GridView RowCommand does fire. Would you like to show us how you bind to this command execution. Bellow is sample code of a user control with GridView bound to an IList containing content items. Please take a look at the code to see how I bind to the row command and based on command name do something with the command argument bubbled in this event:

    Markup:
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="GridView.ascx.cs" Inherits="UserControls_GridView" %>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="20" AutoGenerateColumns="false">
        <Columns>
             <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:LinkButton ID="NameLink" runat="server"></asp:LinkButton>
                </ItemTemplate>           
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Author">
                <ItemTemplate>
                    <asp:Literal ID="Author" runat="server"></asp:Literal>
                </ItemTemplate>
             </asp:TemplateField>  
        </Columns>
    </asp:GridView>

    and code file:
    using System;
    using System.Collections;
    using System.Web.UI.WebControls;
    using Telerik.Cms.Engine;
     
    public partial class UserControls_GridView : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //get news
            ContentManager cntManager = new ContentManager("News");
            IList NewsList = cntManager.GetContent();
            GridView1.DataSource = NewsList;
            //subscribe to events for Grid View
            GridView1.RowDataBound += new GridViewRowEventHandler(GridView_RowDataBound);
            //subscibre to the GridView Rowcommand event
            GridView1.RowCommand += new GridViewCommandEventHandler(GridView_RowCommand);
            GridView1.DataBind();
        }
        //event handler for the RowCommand event
        void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Redirect")
                Response.Redirect(e.CommandArgument.ToString());
        }
     
        void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                IContent content = (IContent)e.Row.DataItem;
                LinkButton nameLink = (LinkButton)e.Row.FindControl("NameLink");
                Literal author = (Literal)e.Row.FindControl("Author");
                if(content.GetMetaData("Title").ToString()!=null)
                    nameLink.Text = content.GetMetaData("Title").ToString();
                if (content.GetMetaData("Author").ToString() != null)
                    author.Text = content.GetMetaData("Author").ToString();
                //pass command name and argument that will be bubbled in the Rowcommand event
                nameLink.CommandName = "Redirect";
                nameLink.CommandArgument = "http://www.google.com";
            }
        }
    }

    This should do the trick. It will redirect to the specified URL once the RowCommand fires.

    All the best,
    Radoslav Georgiev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
2 posts, 0 answered