【问题标题】:Adding a image button to a gridview header in c# with on click event使用点击事件将图像按钮添加到c#中的gridview标题
【发布时间】:2013-11-20 09:29:44
【问题描述】:

我有一个填充了项目详细信息的 gridview(如下),我希望能够通过单击要排序的标题来对结果进行排序。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="mGrid"
PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" DataKeyNames="ProjectID"
OnRowDataBound="OnRowDataBound" EnableModelValidation="True" Style="width: 95%;
float: left;" AllowSorting="True">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
    <asp:BoundField DataField="Title" HeaderText="Project Title" ItemStyle-Width="10%"
        ItemStyle-Font-Bold="true">
        <ItemStyle Width="10%" />
    </asp:BoundField>
    <asp:BoundField DataField="Division" HeaderText="Division" ItemStyle-Width="10%">
        <ItemStyle Width="10%" />
    </asp:BoundField>
    <asp:BoundField DataField="Due Date" HeaderText="Due Date" ItemStyle-Width="10%">
        <ItemStyle Width="10%" />
    </asp:BoundField>
    <asp:BoundField DataField="Status" HeaderText="Status" ItemStyle-Width="10%">
        <ItemStyle Width="10%" />
    </asp:BoundField>
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>

为此,我在我的 c# 代码中添加了 2 个图像按钮到标题,并添加了一个点击事件,我在填充 gridview 后在 page_load 中调用此方法:

public void addHeaderFilters()
    {
        //Add to project title
        ImageButton titleAscImg = new ImageButton();
        titleAscImg.ID = "IMGB_ProjTitleAsc";
        titleAscImg.Click += new ImageClickEventHandler(sortBy_Click);
        titleAscImg.ImageUrl = "images/image";
        titleAscImg.CssClass = "sortButton";

        ImageButton titleDescImg = new ImageButton();
        titleDescImg.ID = "IMGB_ProjTitleDesc";
        titleDescImg.Click += new ImageClickEventHandler(sortBy_Click);
        titleDescImg.ImageUrl = "images/image2";
        titleDescImg.CssClass = "sortButton";

        Label lbl = new Label();
        lbl.Text = "Project Title ";

        GV_Projects.HeaderRow.Cells[2].Controls.Add(lbl);
        GV_Projects.HeaderRow.Cells[2].Controls.Add(titleAscImg);
        GV_Projects.HeaderRow.Cells[2].Controls.Add(titleDescImg);
    }

    public void sortBy_Click(object sender, EventArgs e)
    {
        ImageButton imgb = new ImageButton();
        imgb = (ImageButton)sender;
    }

这会按预期显示标题,但是当我单击任一图像按钮时,不会触发事件,并且标题会从删除箭头的 aspx 代码返回到其默认值,我不太清楚为什么?

感谢任何帮助。

【问题讨论】:

  • 页面加载方法:if(!IsPostBack) { addHeaderFilters(); }'

标签: c# asp.net gridview event-handling onclick


【解决方案1】:

看起来您需要将 addHeaderFilters() 包含在 !IsPostBack 语句中

这样

if(!IsPostBack)
{
    addHeaderFilters();
}

【讨论】:

  • 这也是我的第一个想法,但它在 !IsPostBack 中
  • 网格绑定方法是否也包含在 !IsPostBack 中?电网是否正在反弹?如果是这样,您还必须在每次重新绑定网格视图时重新绑定标题。
  • 我正在绑定它,因为我在其中有一个 gridview,据我所知,我也在重新绑定标题。我改变了我的设计,使其更容易实现并节省时间。我使用了一个带有 HeaderTemplate 的 TemplateField,这使我可以在 ascx 页面中编码 ImageButtons 而无需动态添加它 - 感谢您的帮助
【解决方案2】:
protected void GridViewTransaction_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.Header)
    {
        ImageButton btnExcelReport = new ImageButton();
        btnExcelReport.ID = "BtnExcelReport";
        btnExcelReport.ImageUrl = "images/excel.gif";
        btnExcelReport.ToolTip = "generate Excel report from result";
        btnExcelReport.Click += new ImageClickEventHandler(btnExcelReport_Click);

        e.Row.Cells[0].Controls.Add(btnExcelReport);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    相关资源
    最近更新 更多