簡體   English   中英

使用 jquery ajax 將值設置為(文本框)服務器控件

[英]To set the value to a (textbox) server control using jquery ajax

在下面提到的代碼中,只有當我使用 HTML 控件(按鈕和文本)時,我才能設置從 ajax 調用獲得的值。 如果我使用按鈕之類的 asp 服務器控件,即使我將按鈕用作服務器控件並將文本框用作普通 HTML 控件,我也不會從 ajax 調用中獲得任何輸出。 提前致謝。

AjaxCall.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxCall.aspx.cs" Inherits="SampleLogin.AjaxCall" %>
<!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>Ajax Call</title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
    // $('#<%= btnAjax.ClientID%>').click(function () {     // If i use this iam not getting any response
            $('#submit').click(function () {
                alert("clicked");
                $.ajax({
                    type: "POST",
                    url: "AjaxCall.aspx/HelloWorld",
                    data: "{}",
                    contentType: "application/json",
                    dataType: "json",
                    success: function (msg) {

                        alert(msg.d);
                        $("#Result").val(msg.d);
                    }
                });
            });
        });
  </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" value="click" id="submit"  />
        <asp:Button ID="btnAjax" runat="server" Text="GetVal" />
        <input type="text" ID="Result" runat="server">
    </div>
    </form>
</body>
</html>

AjaxCall.aspx.cs 代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
namespace SampleLogin
{
    public partial class AjaxCall : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        [WebMethod(EnableSession = false)]
        public static string HelloWorld()
        {
            return "Hello";
        }
    }
}

這是因為服務器端按鈕導致頁面回發到服務器...不知道為什么要使用服務器控件進行 ajax 調用?

我使用了 preventDefault 函數,它現在對我有用

<script>
        $(document).ready(function () {
            $('#<%= btnAjax.ClientID %>').click(function (e) {
                alert("clicked e");
                e.preventDefault();

                $.ajax({
                    type: "POST",
                    url: "AjaxCall.aspx/HelloWorld",
                    data: "{}",
                    contentType: "application/json",
                    dataType: "json",
                    success: function (msg) {
                        // Replace the div's content with the page method's return.
                        alert(msg.d);
                        $("#<%=txtAjaxVal.ClientID %>").val(msg.d);
                    }
                });
            });
        });
  </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM