How to call properties inside of a script runat at

2019-09-17 23:55发布

问题:

I have a function that i need to call in the .aspx, and its in a tag as follows, the problem i am having is that i need to evaluate a property in the .cs clas. How would I be able to accomplish this?.

<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>

回答1:

You can do this using Namespace: First put your class in a Namespace

namespace testNamespace
{
  public class test
    {
       public static bool tester(int x, int y)
        {
            if (x == y)
            {
                return true;
            }
            else return false;
        }
    }
}

To import your Namespace use

    <%@ 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>

With this script you can pass values to your class file, validate it and return your answer.



回答2:

You can define a property in your CS class and get access to it using Partial classes like this

CS:

public partial class WebForm1 : System.Web.UI.Page
    {
        public bool MyProperty { get; set; }

       protected void Page_PreInit(object sender,EventArgs e)
       {
           MyProperty = true;
       }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

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

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button Text="text" runat="server" OnClick=Button1_Click />
    </div>
    </form>
</body>
</html>


标签: c# asp.net web