Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does GridView ButtonField click cause a postback?

I'm having problems when using a ButtonField to issue a row command.

When I click the ImageButton in the ButtonField, IsPostBack is false

My understanding is that an ImageButton in a ButtonField in a GridView should cause postback to be true.

Question: Can someone please explain whether I'm right or wrong and if there are properties to set on the button field to get it to issue a postback.

Some code:

Page_Load(object sender, EventArgs e) 
{
    if (!IsPostBack)
    {
        m_DataTable = GetDataTable();      
        Session["m_DataTable"] = m_DataTable;
    }
    else
    {
        m_DataTable = Session["m_DataTable"];
    }
}

and later in the code:

GridView1.Columns.Clear();

ButtonField cf = new ButtonField();
cf.HeaderStyle.CssClass = "comGridHeadCell";
cf.HeaderText = "some text";
cf.HeaderImageUrl = "images/something.png";
cf.Text = "action";
cf.CommandName = "action";
cf.ImageUrl = "images/something.png";
cf.ButtonType = ButtonType.Image;
cf.ItemStyle.CssClass = "comGridLink";

GridView1.Columns.Add(cf);

GridView1.DataSource = m_DataTable;
GridView1.DataBind();

also:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True"  OnRowCommand="GridView_RowCommand" OnPageIndexChanging="GridView_PageIndexChanging">
  <PagerSettings PageButtonCount="25" />
</asp:GridView>

Edit: I am running the site in debug mode via VS2010. I am testing using IE8. If I use firefox, IsPostBack == true. This looks like a specific issue when debugging in IE8.

like image 988
ghostJago Avatar asked Feb 11 '26 09:02

ghostJago


1 Answers

Are you creating that column on every load?

The following fires off RowCommand as expected and IsPostBack is true as expected.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Data;

namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Debugger.Break();
            }
            else
            {
                Debugger.Break();
            }

            DataTable oDataTable = new DataTable();
            oDataTable.Columns.Add("animal");
            DataRow oDataRow = oDataTable.NewRow();
            oDataRow["animal"] = "cat";
            oDataTable.Rows.Add(oDataRow);

            GridView1.Columns.Clear();

            ButtonField cf = new ButtonField();
            cf.HeaderStyle.CssClass = "comGridHeadCell";
            cf.HeaderText = "some text";
            cf.HeaderImageUrl = "images/something.png";
            cf.Text = "action";
            cf.CommandName = "action";
            cf.ImageUrl = "images/something.png";
            cf.ButtonType = ButtonType.Image;
            cf.ItemStyle.CssClass = "comGridLink";

            GridView1.Columns.Add(cf);

            GridView1.DataSource = oDataTable;
            GridView1.DataBind();
        }


        protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Debugger.Break();
        }
    }
}
like image 58
Nikki9696 Avatar answered Feb 14 '26 00:02

Nikki9696



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!