简体   繁体   中英

How to call a code behind function in the aspx file in a server tag

I have a TabPanel whose HeaderText property I want to set using a code behind function eg.

<asp:TabPanel ID="id" runat="server" HeaderText='<%= String.Format("{0}","some text") %>'>  

I can't put the function call between the start and end tags because it is a TabPanel, but when I do the above I just get an empty header on the page. I have also tried <%# %> (I am not sure of the difference between the two).

The String.Format is just an example, not the real function I am trying to call.

I know some attributes don't support inline syntax, and this might be one of them, unfortunately. The syntax looks okay, which makes me think that might be the case. To workaround this issue, just set the HeaderText in code-behind.

As for the difference between <%= ... %> and <%# ... %> , the latter is used strictly for databinding syntax:

<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <%# Eval("SomeValue") %>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

EDIT

There's actually an easier way that I didn't think of before. Just use the HeaderTemplate to set the column header text dynamically:

<Columns>
    <asp:TemplateField>
        <HeaderTemplate>
            <%# Eval("SomeHeaderValue") %>
        </HeaderTemplate>
        <ItemTemplate>
            <%# Eval("SomeValue") %>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>    

Try:

HeaderText='<%# TabPanel_HeaderText %>'

In Code behind

protected string TabPanel_HeaderText
{
    get { return String.Format("{0}","some text"); }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM