【问题标题】:ASP:BoundField headerText as variableASP:BoundField headerText 作为变量
【发布时间】:2020-01-26 16:37:34
【问题描述】:

是否可以使用变量动态设置 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 运行时错误:此上下文不支持代码块

这样不行吗?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:
    protected void summaryGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            if(your condition)
            {
                e.Row.Cells[0].Text = "T";
            }
        }
    }
    

    【讨论】:

    • 我明白了,这意味着我必须在文件后面的代码中进行更改,而不是在 .ascx 文件本身中进行更改?你能解释一下为什么不能在 ascx 文件中使用动态变量吗?
    • 很好的例子。节省我的时间。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多