为什么 GridView 的 SortExpression 在 RowDataBound 上变空 - C#
Why is SortExpression of GridView getting empty on RowDataBound - C#
我想在排序网格视图的 header 上显示 up/down 箭头。我已经在下面附加的代码中实现了它,但是在行数据绑定事件中,sortexpression 是空的。因此,我无法设置图像的排序方向。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CssClass="gridview_alter" GridLines="Both"
Caption="Submissions today" CaptionAlign="Top"
AllowSorting="true"
AllowPaging="true" PageSize="10" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound"
OnSorting="GridView1_Sorting">
<Columns>
<asp:BoundField DataField="student_name" HeaderText="student_name"
ReadOnly="True" SortExpression="student_name"> </asp:BoundField>
<asp:BoundField DataField="Role" HeaderText="Role"
ReadOnly="True" SortExpression="Role"> </asp:BoundField>
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string imgAsc = @" <img src='~/images/up.png' border='1' title='Ascending' 'width='50' height='50' />";
string imgDes = @" <img src='~/images/dwn.png' border='1' title='Descendng' 'width='50' height='50' />";
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell td in e.Row.Cells)
{
LinkButton lnkbtn = (LinkButton)td.Controls[0];
if (lnkbtn.Text == GridView1.SortExpression)//sortexpression is grtting empty here
{
if (GridView1.SortDirection == SortDirection.Ascending)
{
lnkbtn.Text += imgAsc;
}
else
lnkbtn.Text += imgDes;
}
}
}
检查 sortexpression(以及任何基于排序的工作)的最佳位置是 Gridview 的 OnSorting
事件。
因此,在此 OnSorting 事件中,您可以获得 Header 行并应用 ASC / DESC 图像。
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
// Get the header row.
GridViewRow headerRow = GridView1.HeaderRow;
if(headerRow != null)
{
foreach (TableCell td in headerRow.Cells)
{
LinkButton lnkbtn = (LinkButton)td.Controls[0];
if (lnkbtn.Text == e.SortExpression)
{
if (GridView1.SortDirection == SortDirection.Ascending)
{
lnkbtn.Text += imgAsc;
}
else
lnkbtn.Text += imgDes;
}
}
}
}
我想在排序网格视图的 header 上显示 up/down 箭头。我已经在下面附加的代码中实现了它,但是在行数据绑定事件中,sortexpression 是空的。因此,我无法设置图像的排序方向。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CssClass="gridview_alter" GridLines="Both"
Caption="Submissions today" CaptionAlign="Top"
AllowSorting="true"
AllowPaging="true" PageSize="10" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound"
OnSorting="GridView1_Sorting">
<Columns>
<asp:BoundField DataField="student_name" HeaderText="student_name"
ReadOnly="True" SortExpression="student_name"> </asp:BoundField>
<asp:BoundField DataField="Role" HeaderText="Role"
ReadOnly="True" SortExpression="Role"> </asp:BoundField>
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string imgAsc = @" <img src='~/images/up.png' border='1' title='Ascending' 'width='50' height='50' />";
string imgDes = @" <img src='~/images/dwn.png' border='1' title='Descendng' 'width='50' height='50' />";
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell td in e.Row.Cells)
{
LinkButton lnkbtn = (LinkButton)td.Controls[0];
if (lnkbtn.Text == GridView1.SortExpression)//sortexpression is grtting empty here
{
if (GridView1.SortDirection == SortDirection.Ascending)
{
lnkbtn.Text += imgAsc;
}
else
lnkbtn.Text += imgDes;
}
}
}
检查 sortexpression(以及任何基于排序的工作)的最佳位置是 Gridview 的 OnSorting
事件。
因此,在此 OnSorting 事件中,您可以获得 Header 行并应用 ASC / DESC 图像。
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
// Get the header row.
GridViewRow headerRow = GridView1.HeaderRow;
if(headerRow != null)
{
foreach (TableCell td in headerRow.Cells)
{
LinkButton lnkbtn = (LinkButton)td.Controls[0];
if (lnkbtn.Text == e.SortExpression)
{
if (GridView1.SortDirection == SortDirection.Ascending)
{
lnkbtn.Text += imgAsc;
}
else
lnkbtn.Text += imgDes;
}
}
}
}