当我对GridView进行排序时,我遇到了在GridView的RowDataBound事件期间以编程方式更改HyperLink控件的Text和NavigateUrl属性的问题。 HyperLink控件位于TemplateField中。在初始加载GridView时,Text和NavigateURL属性按预期显示,但在排序时,包含HyperLink控件的单元格的顺序不会随GridView中的其他数据一起更改。 GridView的其余部分按预期排序,但有问题的TemplateField似乎不会移动。
我怀疑RowDataBound事件没有在sort上触发,但我不确定从哪里开始。由于SQL Server 2000,我没有使用SqlDataSource。
这是GridView:
<asp:GridView ID="gvOpenOrders" runat="server" CssClass="gridview" AutoGenerateColumns="false" AllowSorting="true" OnSorting="gvOpenOrders_Sorting" OnRowDataBound="gvOpenOrders_RowDataBound">
<AlternatingRowStyle CssClass="altrow" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="lbSO" runat="server" CommandName="Sort" CommandArgument="csono" Text="SO"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:HyperLink ID="hlSO" runat="server"><%# Eval("csono") %></asp:HyperLink>
</ItemTemplate>
<ItemStyle CssClass="ta-center" />
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="lbCustno" runat="server" CommandName="Sort" CommandArgument="ccustno" Text="Cust No."></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("ccustno") %>
</ItemTemplate>
<ItemStyle CssClass="ta-center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
这就是我在Page_Load中使用GridView的DataBinding if(!IsPostBack):
private void BindGridView()
{
string sqlOpenSO;
string sortDirection = "", sortExpression = "";
sqlOpenSO = // SQL query goes here (removed for post)
using (SqlDataAdapter sda = new SqlDataAdapter(sqlOpenSO, connectionString))
{
sda.Fill(ds);
if (ds.Tables.Count > 0)
{
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["SortDirection"] != null)
{
sortDirection = ViewState["SortDirection"].ToString();
}
if (ViewState["SortExpression"] != null)
{
sortExpression = ViewState["SortExpression"].ToString();
dv.Sort = string.Concat(sortExpression, " ", sortDirection);
}
gvOpenOrders.DataSource = dv;
gvOpenOrders.DataBind();
}
}
}
使用以下OnSorting方法:
protected void gvOpenOrders_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["SortDirection"] == null || ViewState["SortExpression"].ToString() != e.SortExpression)
{
ViewState["SortDirection"] = "ASC";
gvOpenOrders.PageIndex = 0;
}
else if (ViewState["SortDirection"].ToString() == "ASC")
{
ViewState["SortDirection"] = "DESC";
}
else if (ViewState["SortDirection"].ToString() == "DESC")
{
ViewState["SortDirection"] = "ASC";
}
ViewState["SortExpression"] = e.SortExpression;
BindGridView();
}
以下RowDataBound方法:
protected void gvOpenOrders_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hlSO = (HyperLink)e.Row.FindControl("hlSO");
hlSO.NavigateUrl = "<<URL removed for post>>" + ds.Tables[0].Rows[e.Row.RowIndex]["csono"].ToString();
if (!String.IsNullOrWhiteSpace(ds.Tables[0].Rows[e.Row.RowIndex]["crevision"].ToString()))
{
hlSO.Text = ds.Tables[0].Rows[e.Row.RowIndex]["csono"].ToString() + "-" + ds.Tables[0].Rows[e.Row.RowIndex]["crevision"].ToString();
}
else
{
hlSO.Text = ds.Tables[0].Rows[e.Row.RowIndex]["csono"].ToString();
}
}
}
答案 0 :(得分:0)
我认为您可以在GridView中原生使用它而无需TemplateFields:
<asp:GridView ID="gvOpenOrders" runat="server" CssClass="gridview" AutoGenerateColumns="false" AllowSorting="true" OnSorting="gvOpenOrders_Sorting" OnRowDataBound="gvOpenOrders_RowDataBound">
<AlternatingRowStyle CssClass="altrow" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFormatString="<<URL removed for post>>{0}{1}"
DataNavigateUrlFields="csono, crevision" >
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="lbCustno" runat="server" CommandName="Sort"
CommandArgument="ccustno" Text="Cust No."></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("ccustno") %>
</ItemTemplate>
<ItemStyle CssClass="ta-center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
排序和其他一切应该继续本地工作,我认为这将避免你的排序问题。
尝试一下,让我知道它是如何工作的。哦,这应该可以让你摆脱OnRowDataBound中的代码。