【问题标题】:asp.net ajax issueasp.net ajax 问题
【发布时间】:2011-08-11 19:44:10
【问题描述】:

我有一个带有文本框和按钮的表单

文本框有一个 onblur 事件,它是在页面加载后使用 jquery 设置的。

该按钮有一个单击事件,可在文本框中设置一些文本。问题是,每当事件发生时,onblur 事件就会消失。如何解决这个问题。

这只是一个例子。我在页面上有几个控件,这些控件使用 jquery 设置了一些值。在他的下拉 ajax 事件之后,所有值都丢失了。

这是显示问题的示例代码

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

<!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 src="/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

    <script language="javascript">

        var ctrl_name = "#<%=name.ClientID %>";

        $(function () {
            $(ctrl_name).blur(
                function () {
                    alert("this texbox has lost its focus");
                }

            );
        });

    </script>


</head>
<body>
    <form id="form1" runat="server">
    <div>



        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="name" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        </ContentTemplate>
        </asp:UpdatePanel>

    </div>
    </form>
</body>
</html>

代码隐藏代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            name.Text = "testing Ajax";
        }
    }
}

【问题讨论】:

  • 你需要发布一些代码让任何人都能回答这个问题

标签: asp.net-ajax


【解决方案1】:

您在页面上丢失值的原因是您的 button_click 是一种服务器端方法,它总是会导致页面重新加载。

您需要使该方法可从客户端调用,以便在 ajax 中使用。它需要是 WebMethod 或 ScriptMethod。

我最近没有使用过这些东西,所以我的语法可能不是 100% 正确,但希望这能指引你正确的方向。

您的按钮点击方法需要采用以下形式:

[WebMethod]
public static void Button1_Click()
{
...
}

你这样称呼它:

<input ID="Button1" Text="Button" onclick="PageMethods.Button1_Click()" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多