是否可以使用变量动态地设置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运行时错误:在此上下文中不支持代码块
难道就不能这样做吗?
发布于 2020-01-27 09:27:26
protected void summaryGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
if(your condition)
{
e.Row.Cells[0].Text = "T";
}
}
}https://stackoverflow.com/questions/59920423
复制相似问题