开发者

Template variable in ASP.NET

开发者 https://www.devze.com 2023-01-01 06:03 出处:网络
I have to add a Page\'s variable into a ItemTemplate but dont know how. For example: <rad:RadMenuItem ID=\"f\" runat=\"server\" Text=\"Product开发者_JAVA技巧s\">

I have to add a Page's variable into a ItemTemplate but dont know how.

For example:

<rad:RadMenuItem ID="f" runat="server" Text="Product开发者_JAVA技巧s">
   <ItemTemplate>
     <div class="pitem"><%= MyText %></div>
   </ItemTemplate>
</rad:RadMenuItem>

The MyText variable does exist in the context and has value but the text does not show up

Another question: How can I add MyText to the ASP.NET page like the following?

<asp:Button Text="<%=MyText%>" .../>

I dont want to edit the code like btn.Text=MyText, just want to do that on the .aspx file as required.


Answers to second question. (Btw, you should only ask one question at a time here on Stack Overflow.)

You could use

<asp:Button Text="<%# MyText %>" />

if you call DataBind() in you code behind.

public void Page_Init(object sender, EventArgs e)
{
    DataBind();
}

If the databind is expensive, I believe you could use this code, so that you only call it once, and then save the values in the ViewState.

public void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DataBind();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消