如何从asp.net中的label.text调用函数背后的代码 [英] How to call code behind function from label.text in asp.net

查看:97
本文介绍了如何从asp.net中的label.text调用函数背后的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Label.Text后面调用用代码定义的函数,但是它不起作用。这是代码... .aspx文件中的
代码

I am trying to call a function defined in code behind from Label.Text but it's not working. Here is the code... code in .aspx file

<asp:Label runat="server" Text='<%# GetPagingCaptionString() %>' ID="pagenumberLabel"></asp:Label>

后面的代码块

public string GetPagingCaptionString()
        {
            int currentPageNumber = Convert.ToInt32(txtHidden.Value);
            int searchOrderIndex;
            if (int.TryParse(Convert.ToString(Session["searchOrderIndex"]), out searchOrderIndex))
            {
                return string.Format("{0} to {1} orders out of {2}", (currentPageNumber * 20) + 1,
                    (currentPageNumber + 1) + 20, GetItemsCount(searchOrderIndex.ToString()));
            }
            return String.Empty;
        }

有人可以告诉我这是怎么回事。

Can anyone tell me what's wrong here.

推荐答案

除非您使用的是基于模板的控件(例如< asp:Repeater> < asp:GridView> ),则不能使用服务器端控件中的内联代码块。

Unless you're using a template based control (such as <asp:Repeater> or <asp:GridView>) then you can't use inline code-blocks such as you have within a server-side control.

换句话说,服务器端控件的属性(例如<$ c $)中不能有<%=%> 块c>< asp:Label> )。该代码将不会运行,您会发现该代码实际上是作为呈现的HTML的一部分发送的。数据绑定控件例外,其中允许<%#%> 代码块

In other words, you can't have <%=%> blocks within the attributes of server-side controls (such as <asp:Label>). The code will not be run and you will find the code is actually sent as part of the rendered HTML. The exception is for databinding controls where <%#%> code-blocks are allowed.

在这种情况下,最好在自己的代码后面设置 .Text 属性。

You're better off in this situation setting the .Text property in the code-behind itself.

例如在页面加载功能中。...

For instance in your page-load function....

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

这篇关于如何从asp.net中的label.text调用函数背后的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆