Asp.net updatepanel 清除复选框列表选择

发布于 2024-07-11 21:12:36 字数 4032 浏览 5 评论 0原文

我在 Ajax UpdatePanel 中有一个 ASP.Net CheckBoxList 控件。

我将包含代码 (C#) 以及下面的 HTML。

我发现 CheckBoxList 在回发过程中没有持续存在。

顺便说一句,有点乱。 它是一个原型。

这是用于填充原始 CheckBoxList 的方法

protected void BindCheckboxes()
{
    chkBuildings.Items.Clear();
    chkNeighborhoods.Items.Clear();
    string city = ddlFindHome_Location.SelectedItem.Value.ToLower();
    ResidentDataContext rdc = new ResidentDataContext(Utility.Lookup.GetResidentConnectionString());
    var neighs = (from n in rdc.spNeighborhoods where n.vchCity.Equals(city) select n);
    foreach (var neighborhood in neighs)
    {
        ListItem li = new ListItem();
        li.Value = neighborhood.intNeighborhoodID.ToString();
        li.Attributes["onclick"] = string.Format("document.getElementById('{0}').click();", btnNeighHack.ClientID);
        li.Text = neighborhood.vchNeighborhood;
        chkNeighborhoods.Items.Add(li);
    }
    var builds = (from b in rdc.spBuildings
                  join nb in rdc.spNeighborhoodBuildings on b.intBuildingID equals nb.intBuildingID
                  join n in rdc.spNeighborhoods on nb.intNeightborhoodID equals n.intNeighborhoodID
                  where n.vchCity.ToLower().Equals(city)
                  select b).Distinct();
    foreach (var buildings in builds)
    {
        ListItem li = new ListItem();
        li.Value = buildings.intBuildingID.ToString();
        li.Text = buildings.vchName;
        chkBuildings.Items.Add(li);
    }
    upNeighs.Update();
    upBuilds.Update();
}

BindCheckboxes() 调用自:

protected void ddlFindHome_Location_SelectedIndexChanged(object sender, EventArgs e)
{
    BindCheckboxes();
}

这是用于填充另一个 CheckBoxList 的复选框的回发方法

protected void btnNeighHack_Click(object sender, EventArgs e)
{
    List<int> neighs = new List<int>();

    foreach (ListItem li in chkNeighborhoods.Items)
    {
        if (li.Selected)
            neighs.Add(Convert.ToInt32(li.Value));
    }
    ResidentDataContext rdc = new ResidentDataContext(Utility.Lookup.GetResidentConnectionString());
    var builds = (from b in rdc.spBuildings
                  join nb in rdc.spNeighborhoodBuildings on b.intBuildingID equals nb.intBuildingID
                  where neighs.Contains(nb.intNeightborhoodID)
                  select b.intBuildingID).Distinct();
    foreach (ListItem li in chkBuildings.Items)
    {
        li.Selected = false;
    }
    foreach (ListItem li in chkBuildings.Items)
    {
        if (builds.Contains(Convert.ToInt32(li.Value)))
            li.Selected = true;
    }
    upBuilds.Update();
}

这是 ASP.Net HTML

<asp:UpdatePanel ID="upNeighs" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <div style="font-weight: bold;">
            Neighborhood
        </div>
        <div style="padding-top: 7px; padding-left: 3px;">
            <input type="checkbox" id="chkNeighborhood_CheckAll" />Select All
        </div>
        <hr />
        <div>
            <asp:CheckBoxList ID="chkNeighborhoods" runat="server" />
            <asp:Button style="display: none;" ID="btnNeighHack" runat="server" 
                onclick="btnNeighHack_Click" />
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel ID="upBuilds" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <div style="padding-left: 6px; padding-top: 5px; font-weight: bold;">
            Building
        </div>
        <div>
            <asp:CheckBoxList ID="chkBuildings" runat="server" />
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

我应该提到调用了 bindcheckboxes() 函数from

protected void ddlFindHome_Location_SelectedIndexChanged(object sender, EventArgs e)
{
    BindCheckboxes();
}

所以它总是一个PostBack。 但我认为你可能对此感兴趣。

I have an ASP.Net CheckBoxList control inside an Ajax UpdatePanel.

I will include the code (C#) along with the HTML below.

I have found that it is something with the CheckBoxList not persisting through the post back.

BTW, it is a little messy. It is a prototype.

This is the method used to populate the original CheckBoxList

protected void BindCheckboxes()
{
    chkBuildings.Items.Clear();
    chkNeighborhoods.Items.Clear();
    string city = ddlFindHome_Location.SelectedItem.Value.ToLower();
    ResidentDataContext rdc = new ResidentDataContext(Utility.Lookup.GetResidentConnectionString());
    var neighs = (from n in rdc.spNeighborhoods where n.vchCity.Equals(city) select n);
    foreach (var neighborhood in neighs)
    {
        ListItem li = new ListItem();
        li.Value = neighborhood.intNeighborhoodID.ToString();
        li.Attributes["onclick"] = string.Format("document.getElementById('{0}').click();", btnNeighHack.ClientID);
        li.Text = neighborhood.vchNeighborhood;
        chkNeighborhoods.Items.Add(li);
    }
    var builds = (from b in rdc.spBuildings
                  join nb in rdc.spNeighborhoodBuildings on b.intBuildingID equals nb.intBuildingID
                  join n in rdc.spNeighborhoods on nb.intNeightborhoodID equals n.intNeighborhoodID
                  where n.vchCity.ToLower().Equals(city)
                  select b).Distinct();
    foreach (var buildings in builds)
    {
        ListItem li = new ListItem();
        li.Value = buildings.intBuildingID.ToString();
        li.Text = buildings.vchName;
        chkBuildings.Items.Add(li);
    }
    upNeighs.Update();
    upBuilds.Update();
}

BindCheckboxes() is called from:

protected void ddlFindHome_Location_SelectedIndexChanged(object sender, EventArgs e)
{
    BindCheckboxes();
}

This is the post back method for populating the Check Boxes of another CheckBoxList

protected void btnNeighHack_Click(object sender, EventArgs e)
{
    List<int> neighs = new List<int>();

    foreach (ListItem li in chkNeighborhoods.Items)
    {
        if (li.Selected)
            neighs.Add(Convert.ToInt32(li.Value));
    }
    ResidentDataContext rdc = new ResidentDataContext(Utility.Lookup.GetResidentConnectionString());
    var builds = (from b in rdc.spBuildings
                  join nb in rdc.spNeighborhoodBuildings on b.intBuildingID equals nb.intBuildingID
                  where neighs.Contains(nb.intNeightborhoodID)
                  select b.intBuildingID).Distinct();
    foreach (ListItem li in chkBuildings.Items)
    {
        li.Selected = false;
    }
    foreach (ListItem li in chkBuildings.Items)
    {
        if (builds.Contains(Convert.ToInt32(li.Value)))
            li.Selected = true;
    }
    upBuilds.Update();
}

Here is the ASP.Net HTML

<asp:UpdatePanel ID="upNeighs" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <div style="font-weight: bold;">
            Neighborhood
        </div>
        <div style="padding-top: 7px; padding-left: 3px;">
            <input type="checkbox" id="chkNeighborhood_CheckAll" />Select All
        </div>
        <hr />
        <div>
            <asp:CheckBoxList ID="chkNeighborhoods" runat="server" />
            <asp:Button style="display: none;" ID="btnNeighHack" runat="server" 
                onclick="btnNeighHack_Click" />
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel ID="upBuilds" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <div style="padding-left: 6px; padding-top: 5px; font-weight: bold;">
            Building
        </div>
        <div>
            <asp:CheckBoxList ID="chkBuildings" runat="server" />
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

I should have mentioned that the bindcheckboxes() function is called from

protected void ddlFindHome_Location_SelectedIndexChanged(object sender, EventArgs e)
{
    BindCheckboxes();
}

So it is always a PostBack. But I think you might be onto something with that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人生戏 2024-07-18 21:12:36
    protected void BindCheckboxes()
    {
        if(!IsPostBack)
{
chkBuildings.Items.Clear();
        chkNeighborhoods.Items.Clear();
        string city = ddlFindHome_Location.SelectedItem.Value.ToLower();
        ResidentDataContext rdc = new ResidentDataContext(Utility.Lookup.GetResidentConnectionString());
        var neighs = (from n in rdc.spNeighborhoods where n.vchCity.Equals(city) select n);
        foreach (var neighborhood in neighs)
        {
            ListItem li = new ListItem();
            li.Value = neighborhood.intNeighborhoodID.ToString();
            li.Attributes["onclick"] = string.Format("document.getElementById('{0}').click();", btnNeighHack.ClientID);
            li.Text = neighborhood.vchNeighborhood;
            chkNeighborhoods.Items.Add(li);
        }
        var builds = (from b in rdc.spBuildings
                      join nb in rdc.spNeighborhoodBuildings on b.intBuildingID equals nb.intBuildingID
                      join n in rdc.spNeighborhoods on nb.intNeightborhoodID equals n.intNeighborhoodID
                      where n.vchCity.ToLower().Equals(city)
                      select b).Distinct();
        foreach (var buildings in builds)
        {
            ListItem li = new ListItem();
            li.Value = buildings.intBuildingID.ToString();
            li.Text = buildings.vchName;
            chkBuildings.Items.Add(li);
        }
        upNeighs.Update();
        upBuilds.Update();
}
    }

尝试一下。

    protected void BindCheckboxes()
    {
        if(!IsPostBack)
{
chkBuildings.Items.Clear();
        chkNeighborhoods.Items.Clear();
        string city = ddlFindHome_Location.SelectedItem.Value.ToLower();
        ResidentDataContext rdc = new ResidentDataContext(Utility.Lookup.GetResidentConnectionString());
        var neighs = (from n in rdc.spNeighborhoods where n.vchCity.Equals(city) select n);
        foreach (var neighborhood in neighs)
        {
            ListItem li = new ListItem();
            li.Value = neighborhood.intNeighborhoodID.ToString();
            li.Attributes["onclick"] = string.Format("document.getElementById('{0}').click();", btnNeighHack.ClientID);
            li.Text = neighborhood.vchNeighborhood;
            chkNeighborhoods.Items.Add(li);
        }
        var builds = (from b in rdc.spBuildings
                      join nb in rdc.spNeighborhoodBuildings on b.intBuildingID equals nb.intBuildingID
                      join n in rdc.spNeighborhoods on nb.intNeightborhoodID equals n.intNeighborhoodID
                      where n.vchCity.ToLower().Equals(city)
                      select b).Distinct();
        foreach (var buildings in builds)
        {
            ListItem li = new ListItem();
            li.Value = buildings.intBuildingID.ToString();
            li.Text = buildings.vchName;
            chkBuildings.Items.Add(li);
        }
        upNeighs.Update();
        upBuilds.Update();
}
    }

try that.

诗酒趁年少 2024-07-18 21:12:36

好吧,如果您每次更改选择时都清除 CheckBoxList,它也会清除您选择的项目。 我会在 page_load 加载项目。

Well if you clear your CheckBoxList everytime you change a selection, it's gonna clear your selected items too. I would load the items at page_load instead.

丶情人眼里出诗心の 2024-07-18 21:12:36

经过进一步的研究,我发现控件在回发过程中并未持续存在,并且正在退出视图状态。 因此,每次回发时,都会从以下位置返回一个空对象:

protected Control PostBackControl
{
    get { return Page.FindControl(Request.Params.Get("__EVENTTARGET")); }
}

但它发现下拉列表的值不是默认值,并开始重新绑定所有内容。

当我仅在 PostBackControl 是下拉列表时绑定复选框列表时,控件永远不会绑定,因为更新面板中的所有内容都超出了范围。

After further research, I have found that the controls aren't persisting through the post back, and are dropping out of the view state. So each time it posts back, there is a null object returned from:

protected Control PostBackControl
{
    get { return Page.FindControl(Request.Params.Get("__EVENTTARGET")); }
}

but it sees that the drop down list's value isn't the default value and starts to rebind everything.

When I only bind the checkbox lists when the PostBackControl is the drop down list, the controls never get bound since everything in the update panel is dropping out of scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文