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

C# Asp.net updatepanel清除复选框列表选择,c#,asp.net,updatepanel,checkboxlist,C#,Asp.net,Updatepanel,Checkboxlist,我在Ajax UpdatePanel中有一个ASP.Net CheckBoxList控件 我将在下面的HTML中包含代码(C#) 我发现,这是一些与复选框列表没有坚持通过后回来 顺便说一句,有点乱。这是一个原型 这是用于填充原始复选框列表的方法 protected void BindCheckboxes() { chkBuildings.Items.Clear(); chkNeighborhoods.Items.Clear(); string city = ddlFind

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

我将在下面的HTML中包含代码(C#)

我发现,这是一些与复选框列表没有坚持通过后回来

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

这是用于填充原始复选框列表的方法

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();
}
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();
}
BindCheckBox()是从以下位置调用的:

protected void ddlFindHome_Location_SelectedIndexChanged(object sender, EventArgs e)
{
    BindCheckboxes();
}
这是用于填充另一个复选框列表的复选框的回发方法

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();
}
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();
}
所以它总是一个回发。但我想你可能已经知道了

    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();
}
    }

试试看。

如果每次更改选择时都清除复选框列表,它也会清除所选项目。我会改为在page_load加载项目。

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

protected Control PostBackControl
{
    get { return Page.FindControl(Request.Params.Get("__EVENTTARGET")); }
}
但它看到下拉列表的值不是默认值,并开始重新绑定所有内容

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