c# asp.net checkboxlist中的Checkbox总是返回未选中状态
本文关键字:返回 状态 net asp checkboxlist 中的 Checkbox | 更新日期: 2023-09-27 18:12:04
我已经为这个问题纠结了一天了。但我就是看不出来。
我有一个名为cblRounds的复选框列表它是
<asp:CheckBoxList ID="cblRondes" runat="server">
</asp:CheckBoxList>
还需要注意的是,EnableViewstate被设置为true。
在我的代码后面,在page_Load i填充列表中,像这样:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dpPrintRounds.FieldValue = DateTime.Now.AddDays(1);
}
FillCheckBoxList();
}
private void FillCheckBoxList()
{
tourCollectie = new LogisticsTourCollection();
RelationCollection rc = new RelationCollection(LogisticsItemEntity.Relations.LogisticsItemSpecsEntityUsingSeqSpecs);
rc.Add(LogisticsItemSpecsEntity.Relations.LocationEntityUsingSeqLocationDelivery);
rc.Add(LocationEntity.Relations.LocationLogisticsTourEntityUsingSeqLocation);
rc.Add(LocationLogisticsTourEntity.Relations.LogisticsTourEntityUsingSeqLogisticsTour);
PredicateExpression pe = new PredicateExpression(LogisticsItemSpecsFields.RequiredDeliveryDate == dpPrintRounds.FieldValue);
pe.Add(LogisticsItemFields.DeliveryNumber != DBNull.Value);
tourCollectie.GetMulti(pe, rc);
cblRondes.Items.Clear();
foreach (LogisticsTourEntity tour in tourCollectie)
{
cblRondes.Items.Add(new ListItem(tour.Name, tour.SeqLogisticsTour.ToString()));
}
}
然后我点击一个按钮来检查复选框的复选状态
protected void btnPrintHeaders_Click(object sender, EventArgs e)
{
PrintRounds();
}
private void PrintRounds()
{
if (dpPrintRounds.Date_Selected.HasValue)
{
Dictionary<string, string> rondes = new Dictionary<string, string>();
foreach (ListItem item in cblRounds.Items)
{
if (item.Selected)
{
rondes.Add(item.Value, GetDeliveryNumberFromRonde(item.Value));
}
}
}
}
一切正常,除了if (item.Selected)总是返回false。
还有
<td>
<rm:DatePicker ID="dpPrintRounds" runat="server" />
</td>
<td>
<asp:Button ID="btnSearch" runat="server" Visible="true"
onclick="btnSearch_Click" />
<%--<asp:Literal ID="litLogisticsRoundName" runat="server" />:--%>
</td>
Datepicker返回我用来过滤集合的日期。因此,当我按下搜索按钮时,我的复选框列表中会出现"new"复选框。这就是为什么我没有Fillcheckboxlist在if(!IsPostBack)中,否则我在新的搜索中没有得到复选框。
我一直在寻找这个问题的答案,并尝试了几种方法,但似乎都不起作用。
Page_Load事件代码必须封装在IsPostBack块中。
if(!IsPostBack)
{
foreach (LogisticsTourEntity tour in tourCollection)
{
cblRounds.Items.Add(new ListItem(tour.Name, tour.SeqLogisticsTour.ToString()));
}
}
演示:标记<form id="form1" runat="server">
<asp:CheckBoxList
ID="cblRounds"
runat="server">
</asp:CheckBoxList>
<asp:Button
ID="Button1"
runat="server"
Text="Button" onclick="Button1_Click" />
</form>
后台代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 1; i <= 10; i++)
{
cblRounds.Items.Add(new ListItem("Text" + i ,i.ToString()));
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
PrintRounds();
}
private void PrintRounds()
{
Dictionary<string, string> rondes = new Dictionary<string, string>();
foreach (ListItem item in cblRounds.Items)
{
if (item.Selected)
{
rondes.Add(item.Text , item.Value);
Response.Write("<br/> " + item.Text + " " + item.Value);
}
}
}
您是否确保它不是由于强制转换到listtitem ?应该不需要,但值得一试…
for (int i = 0; i < cblRounds.Items.Count; i++)
{
if (cblRounds.Items[i].Selected)
//do your stuff
}
另外,如果你设置一个断点的地方,你是解引用的Selected
值是什么状态?在页面生命周期的哪个点绑定要捕获的数据/用户事件,以及在哪里尝试获得所选值,这可能是一个问题吗?
你的方法调用FillCheckBoxList()
在页面加载时填充列表框,无论它是否是回发。
这意味着你重写了返回到服务器的值,因为你的填充代码总是执行这个:
cblRondes.Items.Clear();
foreach (LogisticsTourEntity tour in tourCollectie)
{
cblRondes.Items.Add(new ListItem(tour.Name, tour.SeqLogisticsTour.ToString()));
}
你想只填充CheckBoxList的项目,如果它不是回发(即只有当页面第一次加载)。
当它是回发时,CheckBoxList的Items集合已经被回发本身从网页的内容填充。您不必在回发时填充控件。
你只是想改变你的page_load为:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dpPrintRounds.FieldValue = DateTime.Now.AddDays(1);
FillCheckBoxList();
}
}
回答最初的问题,看看这里可能会对某人有所帮助:-
http://aspdotnetcodebook.blogspot.co.uk/2008/08/how-to-add-checkboxlist-dynamically.html我已经成功地动态创建了一个复选框列表,用数据库中的值填充它,然后在更改后更新数据库。