我有一个GridView,其中有两列可以进行排序。在排序之后,我想在列旁边显示一个图像,向上或向下箭头指向Asc和Desc排序。
我无法弄清楚如何引用ImageButton对象,因此我可以根据其Asc和Desc。将ImageButton.ImageUrl设置为实际图像。
这是我的.aspx代码:
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="Name_SortLnkBtn" runat="server" Text="Name:" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Name" CausesValidation="false" />
<asp:ImageButton ID="Name_SortImgBtn" runat="server" Visible="false" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Name" CausesValidation="false" />
</HeaderTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "~/TestResults/Diabetes.aspx?ID="+Eval("ID") %>'><%#Eval("Name")%></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="HouseName_SortLnkBtn" runat="server" Text="House Name:" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="House" CausesValidation="false" />
<asp:ImageButton ID="HouseName_SortImgBtn" runat="server" Visible="false" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="House" CausesValidation="false" />
</HeaderTemplate>
<ItemTemplate><%#Eval("House")%></ItemTemplate>
</asp:TemplateField>
</Columns>
非常感谢帮助。
更新了.aspx.cs文件:
public partial class Home : System.Web.UI.Page
{
protected _code.SearchSelection _SearchSelection = new _code.SearchSelection();
protected _code.Utils _utils = new _code.Utils();
protected ImageButton sortImage = new ImageButton();
protected void Page_Load(object sender, EventArgs e) {
//if (!IsPostBack) {
Master.FindControl("Home").ID = "active";
GridView1_DataBind();
//Guid ID = new Guid(_SearchSelection.getUserID().Tables[0].Rows[0]["u_ID"].ToString());
//}
}
protected void GridView1_DataBind() {
string selection = string.Empty;
TreeView treeMain = (TreeView)tree.FindControl("treeMain");
if (treeMain.SelectedNode != null)
selection = treeMain.SelectedNode.Text;
else
selection = Session["Selection"].ToString();
DataSet mainData = _utils.getStoreProcedure(new SqlParameter[] { new SqlParameter("@Selection", selection) }, "sp_getTenantsWithDiabetes", ConfigurationManager.ConnectionStrings["TIPS4"].ConnectionString);
Session["MainData"] = mainData.Tables[0];
GridView1.DataSource = mainData.Tables[0];
GridView1.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) {
//Retrieve the table from the session object.
DataTable dt = Session["MainData"] as DataTable;
ImageButton imageButton = new ImageButton();
if (dt != null) {
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
//imageButton.ImageUrl = "~/App_Themes/Sugar2006/Images/arrow_up.gif";
//imageButton.Visible = true;
this.GridView1.DataSource = Session["MainData"];
this.GridView1.DataBind();
}
}
private string GetSortDirection(string column) {
// By default, set the sort direction to ascending.
string sortDirection = "ASC";
// Retrieve the last column that was sorted.
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null) {
// Check if the same column is being sorted.
// Otherwise, the default value can be returned.
if (sortExpression == column) {
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "ASC")) {
sortDirection = "DESC";
}
}
}
// Save new values in ViewState.
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.Header) {
var imageButton = (ImageButton)e.Row.FindControl("Name_SortImgBtn");
sortImage = imageButton;
//imageButton.ImageUrl = "~/App_Themes/Sugar2006/Images/arrow_up.gif";
//imageButton.Visible = true;
}
}
答案 0 :(得分:2)
要获得对ImageButton
中定义的HeaderTemplate
的引用,您可以将GridView
的{{3}}事件连接起来。在事件处理程序中,使用RowType
属性检查该行是否为标题行,然后使用FindControl
方法获取对该控件的引用。
protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.Header)
{
var imageButton = (ImageButton)e.Row.FindControl("Name_SortImgBtn");
imageButton.ImageUrl = "~/myimage.gif";
}
}
修改强>
我认为你走在正确的轨道上。我会做出以下更改:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = Session["MainData"] as DataTable;
if (dt == null) return;
//Sort the data
dt.DefaultView.Sort = e.SortExpression + " " +
GetSortDirection(e.SortExpression);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
无需担心ImageButton
事件处理程序中的Sorting
。单击标题中的LinkButton
将导致回发,并将调用Sorting
事件处理程序。它将在触发RowDataBound
事件之前运行(在调用GridView1.DataBind
方法之前不会发生)。此外,GetSortDirection
方法将在ViewState
中存储排序表达式和排序顺序。我们稍后会在RowDataBound
事件处理程序中显示这些值(如下所示)。
protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
//Determine sort column and sort order
var column = ViewState["SortExpression"] != null ?
ViewState["SortExpression"].ToString() : string.Empty;
var sortDirection = ViewState["SortDirection"] != null ?
ViewState["SortDirection"].ToString() : string.Empty;
//Find ImageButton based on sort column (return if not found)
var imageButtonID = string.Concat(column, "_SortImgBtn");
var imageButton = e.Row.FindControl(imageButtonID) as ImageButton;
if(imageButton == null) return;
//Determine sort image to display
imageButton.ImageUrl = string.Equals("asc", sortDirection.ToLower()) ?
"~/App_Themes/Sugar2006/Images/arrow_up.gif" :
"~/App_Themes/Sugar2006/Images/arrow_down.gif";
imageButton.Visible = true;
}
}
在此事件处理程序中,我们将检索存储在ViewState
中的值,以确定要生成ImageButton
Visible
以及根据排序方向使用哪个图像网址。我假设您已经为ImageButton
控件提供了ID
列名称加"_SortImgBtn"
(如果您以这种方式执行操作,则可以避免使用switch语句将列映射到控制名称)。只需确保首页中的ImageButton
控件Visible
设置为false
,并且应显示排序图像。