2012-04-16 5 views
0

私は、.aspxで呼び出す必要がある関数を持っています。次のようにタグ内で呼び出す必要があります。 .cs clas。どのように私はこれを達成することができるでしょうか?サーバーでスクリプトランナット内のプロパティを呼び出す方法

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

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

なぜ組み込みコードをaspxとcsファイルに混ぜていますか?あなたはcsファイルのすべてを保持する場合は、はるかに簡単です。 – Philipp

+0

本当に私はそれを試していない...うまくいきませんでした。 Page.ClientScriptを私の.csファイルで使用すると、後ろにリターンを入れてポップアップボックスを表示する必要があります。それ以外の場合は無視して続けます。 – user710502

+0

申し訳ありませんが、これはvbコードですプロジェクトは今開いていますが、C#で同じです: Page.ClientScript.RegisterClientScriptBlock(Me.GetType、 "key"、 "if(確認してください( '選択した番号がディレクトリにないので、続行しますか?') – Philipp

答えて

1

することができます名前空間を使用してこれを行う:まずクラスを名前空間に入れる

01あなたの名前空間は、あなたが、あなたのクラスファイルに値を渡すことを確認し、あなたの答えを返すことができます。このスクリプトでは

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

を使用するインポートするには

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

0

あなたのCSクラスでプロパティを定義することができ、この

CSのような部分的なクラスを使用して、それへのアクセスを取得

<%@ 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> 
関連する問題