Asp.net的UpdatePanel清除CheckBoxList的选择
-
19-08-2019 - |
题
我有一个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>
我应该提到的是()函数是从
称为bindcheckboxesprotected void ddlFindHome_Location_SelectedIndexChanged(object sender, EventArgs e)
{
BindCheckboxes();
}
因此,它始终是一个回发。但我想你可能会到一些这一点。
解决方案 3
经过进一步研究,我已发现,控制不通过后持续背部,并且脱落的视图状态的。因此,每个回发时间,存在从返回一个空对象:
protected Control PostBackControl
{
get { return Page.FindControl(Request.Params.Get("__EVENTTARGET")); }
}
但它看到下拉列表中的值不是默认值,并开始重新绑定一切。
当我只绑定列表复选框时PostBackControl是下拉列表中,因为在更新面板的一切永远不会被绑定的控件的范围辍学。
其他提示
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();
}
}
尝试。
那么,如果你清楚你的CheckBoxList每次你改变一个选择,这会清楚你选择的项目太多。我将在的Page_Load代替加载的项目。
不隶属于 StackOverflow