单击<a> href element

时间:2015-11-18 21:50:33

标签: asp.net ajax asp.net-ajax

This is my aspx page. When I click on the a href element, I need to call a function in codebehind using jquery ajax.

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

<!DOCTYPE html>

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

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body style="background-color: black">
<form id="form1" runat="server">
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-4"></div>
            <div class="col-sm-4" style="text-align: center">
                <img src="img/logo.jpg" style="height: 185px; width: 185px" />
            </div>
            <div class="col-sm-4"></div>
        </div>
        <div class="row">
            <div class="col-lg-12" style="height: 30px"></div>
        </div>
        <div class="row">
            <div class="col-sm-12" style="height: 100px">

                <div style="background-image: url(img/question.png); height: 100%; background-repeat: no-repeat; background-size: contain;background-position: center;">
                    <asp:Label ID="question" runat="server" Text="Question" Font-Bold="true" Style="color:white;position: absolute; left: 200px; top: 27px"  ></asp:Label>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12" style="height: 30px"></div>
        </div>
        <div class="row">
            <div class="col-lg-6" style="height: 80px">
                <a href="#" id="op1">
                    <div style="background-image: url(img/answer_left.png); height: 100%; background-repeat: no-repeat; background-size: contain; background-position: center;">
                        <asp:Label ID="option1" runat="server" Text="Option1" Font-Bold="true" Style="position: absolute; left: 200px; top: 27px"></asp:Label>
                    </div>
                </a>

            </div>
            <div class="col-lg-6" style="height: 80px">
                <a href="#" id="op2">
                    <div style="background-image: url(img/answer_right.png); height: 100%; background-repeat: no-repeat; background-size: contain; background-position: center;">
                        <asp:Label ID="option2" runat="server" Text="Option2" Font-Bold="true" Style="position: absolute; left: 85px; top: 27px"></asp:Label>
                    </div>
                </a>
            </div>
            <div class="row">
                <div class="col-lg-12" style="height: 30px"></div>
            </div>

        </div>

        <div class="row">
            <div class="col-lg-6" style="height: 80px">
                <a href="#" id="op3">
                    <div style="background-image: url(img/answer_left.png); height: 100%; background-repeat: no-repeat; background-size: contain; background-position: center;">
                        <asp:Label ID="option3" runat="server" Text="Option3" Font-Bold="true" Style="position: absolute; left: 200px; top: 27px"></asp:Label>
                    </div>
                </a>

            </div>
            <div class="col-lg-6" style="height: 80px">
                <a href="#" id="op4">

                    <div style="background-image: url(img/answer_right.png); height: 100%; background-repeat: no-repeat; background-size: contain; background-position: center;">
                        <asp:Label ID="option4" runat="server" Text="Option4" Font-Bold="true" Style="position: absolute; left: 85px; top: 27px"></asp:Label>
                    </div>
                </a>
            </div>

        </div>
    </div>

</form>
</body>
</html>
<script>
$(document).ready(function () {
    alert("hai");
    $("#op1").click(function (e) {
        e.preventDefault();
        $.ajax({
            type: "GET",
            url: "index.aspx/mymethod",
    //contentType: "application/json; charset=utf-8",
    //dataType: "json",
    //success: OnSuccess,
    //failure: function (response) {
    //    alert(response.d);
    //}
         });
    });

    //$("#op1").click(function () {
    //    alert("ssssss");
    //});
});
</script>

and in .aspx.cs file

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace kodeeswaranKBC
{
public partial class index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        StartGame();

    }

    protected void StartGame()
    {
        question.Text = "Here comes your Question";
        option1.Text = "This is Option1";
        option2.Text = "This is option2";
        option3.Text = "This is option3";
        option4.Text = "This is option4";
    }

    [WebMethod]
    public static void mymethod()
    {

    }

}
}

How Can I call the mymethod? I am new to ajax.Please check my code and get me a reply.Also please let me know if my question is not clear

1 个答案:

答案 0 :(得分:1)

为了在代码隐藏的aspx Web表单中调用方法,您需要记住几件事。

  
      
  1. 该方法必须是静态的。
  2.   
  3. 该方法需要公开。
  4.   
  5. 该方法需要标记属性[webmethod]。
  6.   

因此,在您的情况下,该方法将如下所示:

[Webmethod]
public static void mymethod()
{

}

其余的实现从jquery看起来很好,只需要e.preventDefault()来禁用锚标记的默认行为。

编辑:

$("#op1").click(function (e) {
    e.preventDefault();
    $.ajax({
        type: "GET",
        url: "index.aspx/mymethod",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
       success: function (response) {
          alert(response.d);
       },
       error: function (error) {
          alert();
      }
     });
});