Pregunta

Tengo un control ASP.Net CheckBoxList dentro de un Ajax UpdatePanel.

Incluiré el código (C #) junto con el HTML a continuación.

He descubierto que es algo con la CheckBoxList que no persiste en la publicación posterior.

Por cierto, es un poco desordenado. Es un prototipo.

Este es el método utilizado para completar la CheckBoxList original

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 () se llama desde:

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

Este es el método de publicación posterior para completar las casillas de verificación de otra 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();
}

Aquí está el HTML ASP.Net

<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>

Debería haber mencionado que la función bindcheckboxes () se llama desde

<*>

Entonces, siempre es un PostBack. Pero creo que podrías estar en algo con eso.

¿Fue útil?

Solución 3

Después de una investigación más profunda, descubrí que los controles no persisten en la publicación posterior y están abandonando el estado de vista. Entonces, cada vez que se publica de nuevo, hay un objeto nulo devuelto por:

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

pero ve que el valor de la lista desplegable no es el valor predeterminado y comienza a volver a vincular todo.

Cuando solo enlazo las listas de casillas de verificación cuando PostBackControl es la lista desplegable, los controles nunca se enlazan, ya que todo en el panel de actualización está fuera de alcance.

Otros consejos

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

prueba eso.

Bueno, si borra su CheckBoxList cada vez que cambia una selección, también borrará los elementos seleccionados. En su lugar, cargaría los elementos en page_load.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top