Frage

Ich habe eine ASP.Net CheckBoxList Kontrolle innerhalb eines Ajax Update.

werde ich den Code (C #) umfasst zusammen mit dem folgenden HTML-Code.

ich gefunden habe, dass es etwas mit dem CheckBoxList nicht wieder durch die Pfosten persistierenden.

BTW, es ist ein wenig chaotisch. Es ist ein Prototyp.

Dies ist die Methode verwendet, um die ursprünglichen CheckBoxList aufzufüllen

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 () aufgerufen werden aus:

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

Das ist die Post zurück Methode für die Kontrollkästchen einer anderen CheckBoxList bevöl

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

Hier ist die 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>

Ich habe zu erwähnen, dass der bindcheckboxes () Funktion von

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

So ist es immer ein Postback ist. Aber ich denke, Sie mit, dass auf etwas sein könnten.

War es hilfreich?

Lösung 3

Nach weiteren Recherchen habe ich festgestellt, dass die Kontrollen nicht durch die Post sind persistierende zurück, und aus dem Ansichtszustand fallen. Also jedes Mal wieder postet, gibt es ein Null-Objekt zurück aus:

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

, aber es sieht, dass die Dropdown-Liste des Wert ist nicht der Standardwert und alles beginnt erneut zu binden.

Wenn ich nur die Checkbox-Listen binden, wenn die PostBackControl die Dropdown-Liste, die Kontrollen nie da alles in dem Update-Panel gebunden bekommen sinken außerhalb des Gültigkeitsbereiches.

Andere Tipps

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

versuchen.

Nun, wenn Sie klar Ihre CheckBoxList jedes Mal wenn Sie eine Auswahl ändern, es wird klar, Ihre ausgewählten Elemente zu. Ich würde stattdessen die Elemente bei page_load laden.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top