3

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.

ghostJago
  • 3,381
  • 5
  • 36
  • 51
  • It would be useful to see markup of the GridView in question, and also if you would remove code that doesn't do anything. – pseudocoder Nov 03 '11 at 15:56
  • @pseudocoder the ButtonField is only defined in code. I'll add the gridview markup – ghostJago Nov 03 '11 at 15:58
  • Also FYI I believe there is a typo in your first code block...shouldn't it be `Session["m_DataTable"] = m_DataTable;` ? – pseudocoder Nov 03 '11 at 16:01
  • 2
    When you say ispostback is false, do you mean literally (you traced the code and that property evaluates to false), or do you mean that when you click the button, nothing happens? Or are you trying to describe some other behavior? Also, in which page event are you creating that column dynamically? – Nikki9696 Nov 03 '11 at 16:33
  • I've traced the click and the IsPostBack var is false. The dynamic creation happens in function called from Page_Load – ghostJago Nov 03 '11 at 16:34
  • What is the value of `Page.Request.RequestType`? – pseudocoder Nov 03 '11 at 16:44

2 Answers2

2

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();
        }
    }
}
Nikki9696
  • 6,260
  • 1
  • 28
  • 23
1

The problem is most likely related to your dynamic creation of the ButtonField. When you dynamically create a control, it does not fire events on postback unless you recreate it on every postback on Page.Load or earlier. What event handler is your ButtonField code in?

My suspicion is that Page.IsPostback == False because of this, although I don't know exactly why it would do that.

pseudocoder
  • 4,314
  • 2
  • 25
  • 40