C# 如何在服务器上运行的脚本内部调用属性

C# 如何在服务器上运行的脚本内部调用属性,c#,asp.net,web,C#,Asp.net,Web,我需要在.aspx中调用一个函数,它在一个标记中,如下所示,我遇到的问题是我需要计算.cs clas中的一个属性。我怎样才能做到这一点 <script type="text/javascript"> function Redirect() { location.href = "homePage.aspx"; } </script> <script runat="server"> protecte

我需要在.aspx中调用一个函数,它在一个标记中,如下所示,我遇到的问题是我需要计算.cs clas中的一个属性。我怎样才能做到这一点

<script type="text/javascript">
    function Redirect() {
        location.href = "homePage.aspx";
    } 
 </script> 
     <script runat="server"> 
        protected void Button1_Click(Object sender, EventArgs e)
        {
            if (something is true from the propties set in .cs)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),
               "ConfirmBox", "if(confirm('The numbers selected are not in the directory, you wish to continue?') == true){Redirect();};", true);
            } 
        }
    </script>

函数重定向(){
location.href=“homePage.aspx”;
} 
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
如果(根据.cs中设置的属性,某些内容是正确的)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
“ConfirmBox”,“如果(确认('所选号码不在目录中,是否继续?')==true){Redirect();};”,true);
} 
}

您可以在CS类中定义一个属性,并使用类似这样的分部类访问它

政务司司长:

ASPX:


受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
如果(我的财产)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
“ConfirmBox”,“如果(确认('所选号码不在目录中,是否继续?')==true){Redirect();};”,true);
} 
}

您可以使用名称空间执行此操作:首先将类放在名称空间中

namespace testNamespace
{
  public class test
    {
       public static bool tester(int x, int y)
        {
            if (x == y)
            {
                return true;
            }
            else return false;
        }
    }
}
要导入命名空间,请使用

    <%@ Import Namespace="testNamespace" %>

<script runat="server"> 
        protected void Button1_Click(Object sender, EventArgs e)
        {
            if (test.tester(2, 2))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),
               "ConfirmBox", "if(confirm('The numbers selected are not in the directory, you wish to continue?') == true){Redirect();};", true);
            } 
        }
    </script>

受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
if(测试仪(2,2))
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
“ConfirmBox”,“如果(确认('所选号码不在目录中,是否继续?')==true){Redirect();};”,true);
} 
}

使用此脚本,您可以将值传递到类文件,验证它并返回答案。

为什么要在aspx和cs文件中混合嵌入代码?如果你能把所有的东西都保存在cs文件中,那就容易多了。其实我没有试过。。。不起作用。当我使用Page.ClientScript时。。。。在my.cs中,我需要在它后面加一个回车符,这样它就会显示弹出框,否则它就会忽略它并继续。这是vb代码,因为我现在打开了一个vb项目,但在c#:Page.ClientScript.RegisterClientScriptBlock(Me.GetType,“key”,“if(确认('所选的数字不在目录中,是否要继续?'))中也是如此==true){Redirect();};“,true)请忘记我的评论,我做错了什么,它甚至不再在我的测试项目中工作:-(它有点工作..但它给我的函数Redirect()是未定义的。(javascript错误)
namespace testNamespace
{
  public class test
    {
       public static bool tester(int x, int y)
        {
            if (x == y)
            {
                return true;
            }
            else return false;
        }
    }
}
    <%@ Import Namespace="testNamespace" %>

<script runat="server"> 
        protected void Button1_Click(Object sender, EventArgs e)
        {
            if (test.tester(2, 2))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),
               "ConfirmBox", "if(confirm('The numbers selected are not in the directory, you wish to continue?') == true){Redirect();};", true);
            } 
        }
    </script>