ASP.NET文本框无法在更新面板中保留JavaScript设置的值

ASP.NET文本框无法在更新面板中保留JavaScript设置的值,asp.net,updatepanel,Asp.net,Updatepanel,这可能是一个重复的问题,但我发现没有一个适合我 我有10-20个代表类别的div。每个div都有包含类别id的rel属性。 所有代码都在UpdatePanel中 我想更新数据库中选定的类别(单击div)ID。有一个asp:TextBox和一个asp:Button单击,我将在TextBox中设置所选值 <asp:TextBox ID="lbSelectedCategories" CssClass="lbSelectedCategories" runat="server"></as

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

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

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

<asp:TextBox ID="lbSelectedCategories" CssClass="lbSelectedCategories" runat="server"></asp:TextBox>
我尝试了text()和val()函数。 我可以看到文本框中的值被正确设置,但当我试图点击按钮读取时,我得到了空值

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: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();
        }
    }
}
受保护的无效btnSaveCategory\u单击(对象发送方,事件参数e)
{
AddSelectedCategories();
}
私有void AddSelectedCategories()
{
UserInfo UserInfo=MembershipContext.AuthenticatedUser;
字符串[]arSelectedCategories=lbSelectedCategories.Text.Split(',');
如果(arSelectedCategories.Length>0)
{
使用(SqlConnection con=newsqlconnection(ConfigurationManager.ConnectionStrings[“CMSConnectionString”].ConnectionString))
{
con.Open();
对于(int i=0;i

我在这里做错了什么?

在从code behind更改文本框值时,您是否使用了
IsPostBack
复选框?您是否可以添加
UpdatePanel
按钮
代码?我不是从codebehind更改文本框值,只是阅读它。该值是使用JavaScript设置的,但当我尝试读取添加的代码@SelimYıldızI时,代码中的值为空。我已经测试了您的代码,并且我可以获得
lbSelectedCategories
的文本,在单击按钮时没有任何问题。我想你把这个标签的文本也放在别的地方,比如在页面加载中,这可能吗?