开发者

Template variable in ASP.NET

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜