1

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

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

我发现这是 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() 函数是从

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

所以它总是一个 PostBack。但我认为你可能会对此有所了解。

4

3 回答 3

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

试试看。

于 2009-01-18T22:11:00.510 回答
0

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

于 2009-01-18T23:06:24.237 回答
0

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

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

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

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

于 2009-01-18T23:19:41.527 回答