【发布时间】: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