当我添加 FriendlyUrl 程序集时,jQuery 拖放不起作用

如何解决当我添加 FriendlyUrl 程序集时,jQuery 拖放不起作用

我创建了一个拖放功能,允许用户将一行从一个 GridView 拖到另一个 GridView,从而将记录从活动更新为非活动(反之亦然)。我面临的问题是,当我通过 Global.asax Application_Start 添加 FriendlyUrl RouteConfig 时,记录不会保存。

我的 HTML 包括 Javascscript:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="DragAndDrop._default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Grids</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="InactiveClients" runat="server" ShowHeaderWhenEmpty="true" CssClass="drag_drop_grid gridview" HeaderStyle-CssClass="gridview-header"  AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="Key" SortExpression="">
                        <ItemTemplate>
                            <asp:Label ID="ClientKey" runat="server" Text='<%# Bind("ClientKey") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name" SortExpression="">
                        <ItemTemplate>
                            <asp:Label ID="ClientName" runat="server" Text='<%# Bind("ClientName") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Code" SortExpression="">
                        <ItemTemplate>
                            <asp:Label ID="ClientCode" runat="server" Text='<%# Bind("ClientCode") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="IsActive" SortExpression="">
                        <ItemTemplate>
                            <asp:CheckBox ID="IsActive" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsActive")) %>'/>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
        <div>
        
        </div>
        <div>
            <asp:GridView ID="ActiveClients" ShowHeaderWhenEmpty="true" runat="server" CssClass="drag_drop_grid gridview" HeaderStyle-CssClass="gridview-header" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="Key" SortExpression="">
                        <ItemTemplate>
                            <asp:Label ID="ClientKey" runat="server" Text='<%# Bind("ClientKey") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name" SortExpression="">
                        <ItemTemplate>
                            <asp:Label ID="ClientName" runat="server" Text='<%# Bind("ClientName") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Code" SortExpression="">
                        <ItemTemplate>
                            <asp:Label ID="ClientCode" runat="server" Text='<%# Bind("ClientCode") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="IsActive" SortExpression="">
                        <ItemTemplate>
                            <asp:CheckBox ID="IsActive" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsActive")) %>'/>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $(".drag_drop_grid").sortable({
                    items: 'tr:not(tr:first-child)',cursor: 'arrow',connectWith: '.drag_drop_grid',axis: 'x,y',dropOnEmpty: true,start: function (e,ui) {
                        ui.item.addClass("selected");
                    },receive: function (e,ui) {
                        setTimeout(function () {
                            ui.item.find('[id*=IsActive]').removeAttr('checked');
                            var obj = {};
                            obj.ClientKey = $.trim(ui.item.find('[id*=ClientKey]').html());
                            if (ui.sender[0].id == "InactiveClients") {
                                obj.IsActive = "true";
                                ui.item.find('[id*=IsActive]').attr('checked','checked');
                            }
                            else if (ui.sender[0].id == "ActiveClients") {
                                obj.IsActive = "false";
                                ui.item.find('[id*=IsActive]').removeAttr('checked');
                            }

                            ui.item.removeClass("selected");
                            $.ajax({
                                type: "POST",url: "Default.aspx/SaveClient",data: JSON.stringify({ cdata: obj }),contentType: "application/json; charset=utf-8",dataType: "json"
                            });
                            $(this).find("tbody").append(ui.item);
                            return false;
                        },100);
                    },});
            });
        </script>
    </form>
</body>
</html>

我背后的代码

using System;
using System.Web.UI;
using System.Data;
using System.Data.sqlClient;
using System.Configuration;
using System.Web.Script.Services;
using System.Web.Services;

namespace DragAndDrop
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender,EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                string conString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                using (sqlConnection con = new sqlConnection(conString))
                {
                    using (sqlCommand cmd = new sqlCommand("SELECT ClientKey,ClientName,ClientCode,IsActive FROM TBLClientData WHERE IsActive = 0 ORDER BY ClientName",con))
                    {
                        con.open();

                        sqlDataAdapter da = new sqlDataAdapter(cmd);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        InactiveClients.UseAccessibleHeader = true;
                        InactiveClients.DataSource = dt;
                        InactiveClients.DataBind();
                        con.Close();
                    }

                    using (sqlCommand sqlcmd = new sqlCommand("SELECT ClientKey,IsActive FROM TBLClientData WHERE IsActive = 1 ORDER BY ClientName",con))
                    {
                        con.open();
                        sqlDataAdapter da = new sqlDataAdapter(sqlcmd);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        ActiveClients.UseAccessibleHeader = true;
                        ActiveClients.DataSource = dt;
                        ActiveClients.DataBind();
                        con.Close();
                    }
                }
            }
        }

        public class UpdateClient
        {
            public string ClientKey { get; set; }
            public string IsActive { get; set; }
        }

        [WebMethod]
        [ScriptMethod]
        public static void SaveClient(UpdateClient cdata)
        {
            bool isact = Convert.ToBoolean(cdata.IsActive);
            string conString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            using (sqlConnection con = new sqlConnection(conString))
            {
                using (sqlCommand cmd = new sqlCommand("UPDATE TBLClientData SET IsActive = @IsActive WHERE ClientKey = @ClientKey",con))
                {
                    //cmd.CommandType = CommandType.Text;
                    con.open();
                    cmd.Parameters.AddWithValue("@IsActive",isact);
                    cmd.Parameters.AddWithValue("@ClientKey",cdata.ClientKey);
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
    }
}

我的 Global.asax 文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Optimization;
using System.Web.Routing;

namespace DragAndDrop
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender,EventArgs e)
        {
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

        protected void Session_Start(object sender,EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender,EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender,EventArgs e)
        {

        }

        protected void Application_Error(object sender,EventArgs e)
        {

        }

        protected void Session_End(object sender,EventArgs e)
        {

        }

        protected void Application_End(object sender,EventArgs e)
        {

        }
    }
}

我的 RouteConfig.cs 文件

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Routing;
using Microsoft.AspNet.FriendlyUrls;

namespace DragAndDrop
{
    public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings);
        }
    }
}

如果我在 Global.asax 中注释掉 RouteConfig.RegisterRoutes(RouteTable.Routes);,记录将在删除后更新。有没有办法在包含 RouteConfig 的同时通过 JSON/SaveClient WebMethod 使表更新工作?

解决方法

我想通了。如果我在 RouteConfig 中设置 settings.AutoRedirectMode = RedirectMode.Off;,它可以解决问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?