C# asp.net 复选框列表中的复选框始终返回未选中状态

发布于 2024-12-11 07:17:14 字数 2532 浏览 0 评论 0原文

我已经为这个问题伤透了一天了。 但我就是不明白。

我有一个名为 cblRounds 的复选框列表,另

<asp:CheckBoxList ID="cblRondes" runat="server">
</asp:CheckBoxList>

请注意,EnableViewstate 设置为 true。

在我后面的代码中,在 page_Load 中,我像这样填写列表:

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 返回一个我用来过滤我的集合的日期。 因此,当我按下搜索按钮时,我的复选框列表中会出现“新”复选框。 这就是为什么我在 if(!IsPostBack) 中没有 Fillcheckboxlist,否则我在新搜索中没有任何复选框。

我一直在寻找这个问题的答案,并尝试了几种方法,但似乎都不起作用。 任何想法表示赞赏。

I've been breaking my head on this one for a day now.
But I just don't see it.

I have a checkboxlist named cblRounds which is

<asp:CheckBoxList ID="cblRondes" runat="server">
</asp:CheckBoxList>

Also to note, EnableViewstate is set to true.

In my code behind, in the page_Load i fill de list like this:

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

Then I click a button where I check the checkstate of the checkboxes

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


    }
}

Everything works correct except that the if (item.Selected) always returns false.

Also I have

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

The Datepicker returns a date I use to filter my collection.
So when I press the searchbutton, I get "new" checkboxes in my checkboxlist.
This is why I DON'T have the Fillcheckboxlist inside the if(!IsPostBack) else I get no checkboxes on a new search.

I've been searching for an answer on this and tried several things but none seem to work.
Any ideas are appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ㄟ。诗瑗 2024-12-18 07:17:14

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

}

Page_Load event code must be wrapped in IsPostBack block.

if(!IsPostBack)
{
  foreach (LogisticsTourEntity tour in tourCollection)
    {
        cblRounds.Items.Add(new ListItem(tour.Name, tour.SeqLogisticsTour.ToString()));
    }
}

Demo:

Markup

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

Code-behind

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

}
甜`诱少女 2024-12-18 07:17:14

您是否确保这不是由于转换为 ListItem 造成的?它不应该是需要的,但值得一试...

for (int i = 0; i < cblRounds.Items.Count; i++)
{
   if (cblRounds.Items[i].Selected)
     //do your stuff
}

此外,如果您在取消引用Selected值的地方设置断点,它的状态是什么?这可能是您在页面生命周期中的哪个点绑定捕获的数据/用户事件与您尝试获取所选值的问题吗?

Have you ensured it is not due to casting to ListItem? It shouldnt be needed but worth a try...

for (int i = 0; i < cblRounds.Items.Count; i++)
{
   if (cblRounds.Items[i].Selected)
     //do your stuff
}

Also if you set a breakpoint to the place where you are dereferencing the Selected value what is it's state? Could this be an issue with at what point in the page life cycle you are binding data/user event being captured vs. where you are trying to get the selected value?

谈下烟灰 2024-12-18 07:17:14

您的方法调用 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(); 
    }
}

Your method call FillCheckBoxList() populates the listbox on page load, whether it is a postback or not.

That means you are overriding the values posted back to the server, as your fill code always executes this:

cblRondes.Items.Clear();
foreach (LogisticsTourEntity tour in tourCollectie)
{
    cblRondes.Items.Add(new ListItem(tour.Name, tour.SeqLogisticsTour.ToString()));
}

You want to only populate the CheckBoxList's items if it it not a postback (i.e. only when the page first loads).

When it is a postback the Items collection of the CheckBoxList has already been populated by the postback itself from the contents of the webpage. You do not have to populate the control on postback.

You just want to change your page_load to:

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        dpPrintRounds.FieldValue = DateTime.Now.AddDays(1);
        FillCheckBoxList(); 
    }
}
じее 2024-12-18 07:17:14

在回答原来的问题时,看看这里可能会对某人有所帮助:-

http://aspdotnetcodebook.blogspot.co.uk/2008/08/how-to-add-checkboxlist-dynamically.html

我已成功动态创建了一个复选框列表,用数据库中的值填充它,然后在更改后更新数据库。

In answer to the original question, a look here may help someone:-

http://aspdotnetcodebook.blogspot.co.uk/2008/08/how-to-add-checkboxlist-dynamically.html

I have successfully dynamically created a checkboxlist, populated it with values from a db and then after changes updated the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文