2012-01-02 13 views
0

コードビハインドページからこのjquery関数を呼び出す方法を教えてください。構文が何であるかわからない。どんなヒーローも大いに評価されるだろう。ユーザーが登録するためのウィンドウが表示されます。ユーザーがサイトの5ページを閲覧すると、そのページがポップされます。セッション変数にその数を保持するようにしてください。以下はコードの背後からjQuery関数を呼び出す

HTMLページ

<ul> 

<li><a href="#dialog" name="modal">Simple Window Modal</a></li> 

</ul> 
div id="dialog" class="window"> 

<a href="#"class="close">Close it</a> 
<br /><br /> 
Register Now its quick and Easy.<br /> 
Rules: 

     1) Items with "*" are required fields to be filled out. 
     <br /> 
    </p> 
    <asp:Label ID="lblEmail" runat="server" Text="*Your Email: "></asp:Label><asp:TextBox 
     ID="txtEmail" runat="server"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtEmail" 
     ErrorMessage="You must enter your email address."></asp:RequiredFieldValidator> 
    <br /> 
    <asp:Label ID="lblUserName" runat="server" Text="*Name: "></asp:Label> 
    <asp:TextBox ID="txtName" runat="server" Width="157px"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtName" 
     ErrorMessage="You must enter a username."></asp:RequiredFieldValidator> 
    <br /> 
    <asp:Label ID="lblCity" runat="server" Text="*City: "></asp:Label> 
    <asp:TextBox ID="txtCity" runat="server" Width="168px"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtCity" 
     ErrorMessage="You must enter your location."></asp:RequiredFieldValidator> 
    <br /> 
    <asp:Label ID="lblState" runat="server" Text=" State: "></asp:Label> 
    <asp:TextBox ID="txtState" runat="server" Width="168px"></asp:TextBox><br /> 
    <asp:Label ID="lblAge" runat="server" Text="*Age: "></asp:Label> 
    <asp:TextBox ID="txtAge" runat="server" Width="165px"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtAge" 
     ErrorMessage="Please enter your age to continue."></asp:RequiredFieldValidator> 
    <br /> 
    <asp:Label ID="lblSex" runat="server" Text="*Gender: "></asp:Label> 
    <asp:RadioButton ID="rdMale" runat="server" GroupName="Gender" Text="Male" /> 
    <asp:RadioButton ID="rdFemale" runat="server" GroupName="Gender" Text="Female" /> 
    <asp:Label ID="RadialLBL" runat="server"></asp:Label> 
    <br /> 
    <br /> 
    <asp:Label ID="lblYahoo" runat="server" Text="Yahoo ID: "></asp:Label> 
    <asp:TextBox ID="YahooID" runat="server"></asp:TextBox> 
    <br /> 
    <asp:Label ID="lblMSN" runat="server" Text="MSN ID: "></asp:Label> 
    <asp:TextBox ID="MSNID" runat="server" Width="133px"></asp:TextBox> 
    <br /> 
    <br /> 
    <asp:Label ID="lblMyspaceLink" runat="server" Text="Your Myspace Link: "></asp:Label> 
    <asp:TextBox ID="MyspaceLink" runat="server" Width="255px"></asp:TextBox> 
    <br /> 
    <asp:Label ID="lblFaceBookLink" runat="server" Text="Your FaceBook Link: "></asp:Label> 
    <asp:TextBox ID="FaceBooklink" runat="server" Width="244px"></asp:TextBox> 
    <br /> 
    <asp:Label ID="lblUploadpic" runat="server" Text="*Upload your picture."></asp:Label> 
    <asp:FileUpload ID="FileUpload1" runat="server" Height="22px" Width="217px" /><br /> 
    <br /> 
    <asp:Label ID="Label1" runat="server"></asp:Label><br /> 
    <br /> 
    <asp:Label ID="lblDesctiption" runat="server" Text="*User Bio: "></asp:Label><br /> 
    <asp:TextBox ID="txtDescription" runat="server" MaxLength="240" Width="390px" Height="73px" 
     Rows="3" TextMode="MultiLine"></asp:TextBox><br /> 
    <br /> 
    <asp:Button ID="btnRegister" runat="server" Text="Register" OnClick="btnRegister_Click" /> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtDescription" 
     ErrorMessage="Enter a bio or description of yourself."></asp:RequiredFieldValidator> 
    <br /> 
    <asp:Label ID="EnterAll" runat="server"></asp:Label> 
    <asp:Label ID="Displayme" runat="server" Text=""></asp:Label> 
    <br /> 
    <br /> 
</div> 

jQueryの機能

<script> 

$(document).ready(function() { 

    //select all the a tag with name equal to modal 
    $('a[name=modal]').click(function (e) { 
     //Cancel the link behavior 
     e.preventDefault(); 

     //Get the A tag 
     var id = $(this).attr('href'); 

     //Get the screen height and width 
     var maskHeight = $(document).height(); 
     var maskWidth = $(window).width(); 

     //Set heigth and width to mask to fill up the whole screen 
     $('#mask').css({ 'width': maskWidth, 'height': maskHeight }); 

     //transition effect  
     $('#mask').fadeIn(1000); 
     $('#mask').fadeTo("slow", 0.8); 

     //Get the window height and width 
     var winH = $(window).height(); 
     var winW = $(window).width(); 

     //Set the popup window to center 
     $(id).css('top', winH/2 - $(id).height()/2); 
     $(id).css('left', winW/2 - $(id).width()/2); 

     //transition effect 
     $(id).fadeIn(2000); 

    }); 

    //if close button is clicked 
    $('.window .close').click(function (e) { 
     //Cancel the link behavior 
     e.preventDefault(); 

     $('#mask').hide(); 
     $('.window').hide(); 
    }); 

    //if mask is clicked 
    $('#mask').click(function() { 
     $(this).hide(); 
     $('.window').hide(); 
    }); 

}); 

答えて

0

012にスクリプトを登録するためのサンプルスニペットです:さらに、あなたのコードを確認した後

if(Session["_PageVisitCount"].ToString() == "5"){ 
    Page.ClientScript.RegisterStartupScript(
     this.GetType(), 
     "scriptsKey", 
     "yourScriptMethodToCall();", 
     true 
    ); 
} 

、明示的に呼び出すことができる任意のクライアント側のコードはありません。何かスクリプトがありませんか?

+0

jqueryコードをregualar関数のように呼び出せるように変更するにはどうすればよいですか? – CsharpBeginner

+0

JavaScriptを修正して「ウィンドウをポップ」する機能を作成します。次に、条件が正しいときに、私が例示したようにスクリプトを登録します。 – xandercoded

1

コードビハインドである必要はなく、ASPXファイルのコードブロックにコードをラップすることができます。

<% if(((int)Session["PageViews"]) > 5) { %> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     // This code will only execute when PageViews > 5 
    }); 
    </script> 
<% } %> 

ここでは、サーバー側のチェックですべてのコードをラップしました。チェックがfalseを返す場合、コードはページにレンダリングされません。

0

コードからjsスクリプトを呼び出すことはできません。しかし、他の人が示しているように、レスポンスがクライアントに返されたときにjquery関数を呼び出すスクリプトを登録できます。

関連する問題