gpt4 book ai didi

c# - GridView ButtonField 单击是否会导致回发?

转载 作者:行者123 更新时间:2023-11-30 16:27:21 25 4
gpt4 key购买 nike

我在使用 ButtonField 发出行命令时遇到问题。

当我单击 ButtonField 中的 ImageButton 时,IsPostBack 为 false

我的理解是 GridView 的 ButtonField 中的 ImageButton 应该导致回传为真。

问题:有人可以解释我是对还是错,以及是否有属性要设置在按钮字段上以使其发出回发。

部分代码:

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

和后面的代码:

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();

还有:

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

编辑:我正在通过 VS2010 以 Debug模式运行站点。我正在使用 IE8 进行测试。如果我使用 firefox,IsPostBack == true。这看起来像是在 IE8 中调试时的一个特定问题。

最佳答案

您是否在每次加载时都创建该列?

以下命令按预期触发 RowCommand,IsPostBack 按预期为真。

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();
}
}
}

关于c# - GridView ButtonField 单击是否会导致回发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7997859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com