Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp内联表达式_C#_Asp.net - Fatal编程技术网

C# Asp内联表达式

C# Asp内联表达式,c#,asp.net,C#,Asp.net,我想知道在标签控件上使用内联表达式的最佳方式是什么。 实例 使用数据绑定内联表达式 addnumbers.cs ` ` ASPX标记 <asp:Label ID="Label1" runat="server" Text="<%#addnumbers.totals() %>"></asp:Label> addnumbers类不在ASPX页面的代码隐藏中。标记是我要将方法值传递给的类 谢谢 这样行吗 <%@ Page Language="C#"

我想知道在标签控件上使用内联表达式的最佳方式是什么。 实例 使用数据绑定内联表达式 addnumbers.cs `

`

ASPX标记

    <asp:Label ID="Label1" runat="server" Text="<%#addnumbers.totals() %>"></asp:Label>

addnumbers类不在ASPX页面的代码隐藏中。标记是我要将方法值传递给的类

谢谢

这样行吗

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication2.test" %>
<%@ Import Namespace ="WebApplication2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script runat = "server">
          protected int GetTotal()
          {
              addnumbers ctr = new addnumbers();
              return ctr.totals();

          }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="height: 87px">

 <p> The Total is  <%=GetTotal()%></p>


    </div>


    </form>
</body>
</html>

受保护的int GetTotal()
{
addnumbers ctr=新的addnumbers();
返回中心总计();
}
总数是


?虽然设计很糟糕,但您可能希望调用
new addnumbers().totals()
并将结果存储在页面的属性中,并绑定到该属性。顺便说一下,这只是一个伪属性,但这就是您所说的受保护的int GetTotal(){addnumbers ctr=new addnumbers();return ctr.totals();}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication2.test" %>
<%@ Import Namespace ="WebApplication2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script runat = "server">
          protected int GetTotal()
          {
              addnumbers ctr = new addnumbers();
              return ctr.totals();

          }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="height: 87px">

 <p> The Total is  <%=GetTotal()%></p>


    </div>


    </form>
</body>
</html>