How to Add an Image Commandfield to Gridview dynamically in asp.net


Adding Commandfield to Gridview dynamically in pageload with not IsPostBack

Add the following Namespace in the top for CommandField   :

using System.Web.UI.WebControls;

then
protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
          CommandField   cmdfld = new CommandField();
           //Specify Image path
            cmdfld.SelectText = "<img src='./_images/EditIcon.gif'>";
            cmdfld.ShowSelectButton = true;
            cmdfld.HeaderText = "Edit";
            cmdfld.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
            GridView1.Columns.Add(cmdfld);
            GridView1.DataBind();

        }
    }




0 comments:

Post a Comment