首页 > 解决方案 > ASP.NET 文本框无法保留更新面板内 JavaScript 设置的值

问题描述

这可能是一个重复的问题,但我发现没有一个问题适合我。

我有 10-20 个 div 代表类别。每个 div 都有包含类别 id 的 rel 属性。所有代码都在 UpdatePanel 中。

我想更新数据库中选定的类别(单击的 div)ID。有一个 asp:TextBox 和一个 asp:Button 单击,我将在文本框中设置选定的值。

<asp:TextBox ID="lbSelectedCategories" CssClass="lbSelectedCategories" runat="server"></asp:TextBox>

为此,我添加了以下 jQuery 代码:

$(".area-interest-list li").on('click', function () {
        $(this).toggleClass("active");
        copySelectedCategoriesToLabel();
});

function copySelectedCategoriesToLabel() {
    var selectedCategoryIds = [];

    $(".area-interest-list").find('li').get().forEach(function (item, index, array) {
        var selectedCategoryId = $(item).attr('rel');

        if ($(item).attr('class') == 'active') {
            selectedCategoryIds.push(selectedCategoryId);
        }
    });

    // Remove last ,
    var strSelectedCategoryIds = selectedCategoryIds.join(',');

    $(".lbSelectedCategories").text(strSelectedCategoryIds);
}

我尝试了 text() 和 val() 函数。我可以看到在 textobx 中正确设置了值,但是当我尝试在 Button_Click 上阅读时,我得到空值。

string lbSelectedCategories = lbSelectedCategories.Text;

可能是 UpdatePanel 上的回发正在丢失 textobx 值。

我读到标签不保留它们的值,但对我来说,文本框或隐藏字段都没有保留它们的值。

更新面板代码

<asp:UpdatePanel ID="updatePanelCategories" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span class="ti-close"></span></button>
            <asp:Button runat="server" ID="btnSaveCategory" class="btn btn-style-red" Text="Save" OnClick="btnSaveCategory_Click"></asp:Button>
            <h4>Areas Of Interest</h4>
        </div>
        <div class="modal-body">
            <asp:Repeater ID="repeaterResourceCategory" runat="server" OnItemDataBound="repeaterResourceCategory_ItemDataBound">
                <HeaderTemplate>
                    <div class="area-interest-list">
                        <ul>
                </HeaderTemplate>
                <ItemTemplate>
                    <li class="" rel='<%# Eval("ItemID") %>' id="liSelected" runat="server">
                        <span class="icon-cloud-computing" runat="server" id="spIcon" rel='<%# Eval("IsSelected") %>'></span>
                        <p runat="server" id="pText"><%# Eval("CategoryName") %></p>
                    </li>
                </ItemTemplate>
                <AlternatingItemTemplate>
                    <li class="" rel='<%# Eval("ItemID") %>' id="liSelected" runat="server">
                        <span class="icon-cloud-computing" runat="server" id="spIcon" rel='<%# Eval("IsSelected") %>'></span>
                        <p runat="server" id="pText"><%# Eval("CategoryName") %></p>
                    </li>
                </AlternatingItemTemplate>
                <FooterTemplate>
                    </ul>
            </div>
                </FooterTemplate>
            </asp:Repeater>
            <asp:TextBox ID="lbSelectedCategories" CssClass="lbSelectedCategories" runat="server" style="width:1px;height:1px;"></asp:TextBox>
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

按钮代码

protected void btnSaveCategory_Click(object sender, EventArgs e)
{
    AddSelectedCategories();
}

private void AddSelectedCategories()
{
    UserInfo userInfo = MembershipContext.AuthenticatedUser;

    string[] arSelectedCategories = lbSelectedCategories.Text.Split(',');

    if (arSelectedCategories.Length > 0)
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CMSConnectionString"].ConnectionString))
        {
            con.Open();

            for (int i = 0; i < arSelectedCategories.Length; i++)
            {
                string sql = @"insert into sometable(UserID, ResourceCategoryID) values(" + userInfo.UserID + "," + arSelectedCategories[i] + ")";

                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.ExecuteNonQuery();
            }

            con.Close();
        }
    }
}

我在这里做错了什么?

标签: asp.netupdatepanel

解决方案


推荐阅读