开发者

What is wrong in this code snippet, writing server side variable on the page as property of asp.net server controls

ASPX Page

<asp:Label ID="lbk" runat="server" Text='<%= _imgPath %>' />

Code behind

 protected void Page_Load(object sender,开发者_StackOverflow EventArgs e)
    {
        _imgPath = "MyName";

    }

My expectation was that it should render

<span id="lbk">MyName</span>

But it is rendering

<span id="lbk"><%= _imgPath %></span>

Is this correct behavior?


Try this:

<asp:Label ID="lbk" runat="server" Text='<%# _imgPath %>' />


protected void Page_Load(object sender, EventArgs e)
{
    lbk.Text = "MyName";

}

you dont need _imgPath.


For something simple like setting the text of a Label, use the Page_Load event in the code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        lbk.Text = "MyName";
    }
}

Of if you'd rather use a script on the page, you can do this:

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lbk.Text = "MyName";
        }
    }
</script>    
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜