【问题标题】:How to access listbox items in asp.net code behind which I added through jQuery如何访问我通过 jQuery 添加的 asp.net 代码中的列表框项目
【发布时间】:2016-06-18 18:48:02
【问题描述】:

我正在做一个项目,目前我正在做一个属于发布文章页面的模块。在此页面中,我们可以选择使用文本框和按钮添加关键字。

我通过使用 jQuery 成功将文本框输入关键字添加到列表框来完成此操作。

如果用户在将任何特定关键字添加到列表框后想要删除它,用户可以通过在列表框中选择该关键字来删除它,然后会出现一个删除按钮。如果用户单击该 remove button 关键字从列表框中删除。

问题

使用 jQuery 成功将关键字添加到列表框后,我无法在 C# 代码后面访问该列表框项目

Asp.net 页面列表框:

<a id="arkyw" href="#/"><font color="#cc0000">Remove Keyword</font></a>
<span id="spKeyword" style="color:#e74c3c;"></span>    
<asp:ListBox ID="listBoxKeywords" runat="server" CssClass="list-group" style="border:0px; overflow-y:hidden; height:83px; width:300px;">

jQuery:

var count = [];
    var i = 0;
    $("#btnAddKeyword").click(function () {        
        var txt = $("#txtkeyword").val();
        $('[id$=listBoxKeywords]').show();
        if (jQuery.inArray(txt, count) != "-1") {
            $("#spKeyword").text('Keyword alread exists');
        } else {
            var listCount = $('[id$=listBoxKeywords] option').length;
            if (listCount <= 4) {
                count[listCount] = txt;
                var alreadyExist = false;
                $('[id$=listBoxKeywords] option').each(function () {
                    if ($(this).val() == txt) {
                        $("#spKeyword").text('Keyword alread exists');
                        alreadyExist = true;
                        return;
                    }
                });
                if (!alreadyExist) {
                    $('[id$=listBoxKeywords]').append($('<option></option>').attr('value', count[listCount]).text(txt));
                }
            } else {
                $("#spKeyword").text('5 Keyword limit exceed');
            }
        }
    });
    //Remove Value from the List
    $('[id$=listBoxKeywords]').click(function (e) {        
        var $target = $(e.target);
        if ($target.is('option')) {
            $("#arkyw").show();            
        }
    });
    $("#arkyw").click(function () {
        var a = $('[id$=listBoxKeywords] option:selected').val();
        var id = $('[id$=listBoxKeywords] option').length;
        //alert('ListCount : ' + a);
        //alert('Total List Item : ' + id);
        //alert('After Remove : ' + count[a-1]);
        count = jQuery.grep(count, function (e) {
            $('[id$=listBoxKeywords] option:selected').remove();
            return e != a;

        });        
    });

如何在后面的 C# 代码中访问列表框项?

【问题讨论】:

  • &lt;asp:ListBox 是 Web 表单,而不是 MVC

标签: c# jquery asp.net webforms listbox


【解决方案1】:

您还没有在服务器端访问客户端数据。

您的客户端数据在回发时被清除。

ListBox 中的项目,添加到客户端和浏览器级别。

您可以使用 Json 或 Hidden Field 将客户端数据保存到数据库中。 类似主题:

https://www.experts-exchange.com/questions/28379748/Creating-rows-dynamically-on-client-side-and-saving-them-to-database-on-server-side.html

您可以将列表框项目数据添加到字符串变量并将其保存到隐藏字段。 例如,您有 3 个项目:

项目 1

第 2 项

第 3 项

在客户端添加任何项目时,创建字符串如下所示:

,第 1 项,

所以你终于有了这个字符串:

,项目 1,项目 2,项目 3,

将其保存到隐藏字段并在服务器端读取隐藏字段并拆分字符串,然后将每个字符串保存到数据库并从服务器端代码中读取。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    相关资源
    最近更新 更多