是否可以使用变量在ASP:BoundField上动态设置HeaderText属性?
我有一个ascx控件,该控件在两个地方使用,它检查当前请求路径,以查看它是否应将一个字符串或另一个字符串用作HeaderText值
<%
var headerTextVal = "Top";
var path = Page.Request.Path;
if (!path.Contains("/desktop/homescreen.aspx"))
{
headerTextVal = "T";
}
%>
<asp:GridView ID="summaryGridView" DataSourceID="MySummary" runat="server"
Visible="true" EnableViewState="true" AutoGenerateColumns="False" DataKeyNames="name, top"
Width="100%" AllowSorting="true" GridLines="None" OnRowDataBound="summaryGridView_RowDataBound"
OnRowCommand="summaryGridView_RowCommand" OnSorting="summaryGridView_Sorting">
<Columns>
<asp:HyperLinkField DataTextField="name" HeaderText="Name" SortExpression="name" />
<asp:BoundField DataField="top" HeaderText="<%#Eval("headerTextVal")%>" SortExpression="top" />
</Columns>
</asp:GridView>
当我这样尝试时,出现以下错误:
ASP.Net运行时错误:在这种情况下不支持代码块
不可能做到这一点吗?
答案 0 :(得分:1)
protected void summaryGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
if(your condition)
{
e.Row.Cells[0].Text = "T";
}
}
}