【问题标题】:C# asp.net Checkbox in checkboxlist returns unchecked always复选框列表中的 C# asp.net 复选框始终返回未选中状态
【发布时间】:2012-12-02 22:26:48
【问题描述】:

我已经为这个问题困扰了一天。 但我就是没看到。

我有一个名为 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 的原因,否则我在新搜索中没有复选框。

我一直在寻找这个问题的答案并尝试了几件事,但似乎都没有奏效。 任何想法都表示赞赏。

【问题讨论】:

    标签: c# asp.net checkbox checkboxlist


    【解决方案1】:

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

    【讨论】:

    • 我做了,但它并没有改变错误的回报。
    • 我明白为什么会这样了。但是,如果我有一个按钮可以动态地在复选框列表中生成复选框怎么办。我没有在页面上显示预定义数量的复选框。我根据某个“集合”制作复选框,当我更改 DatePicker 字段并按下“搜索”按钮时,此集合会更改。如果我不清楚这一点,我很抱歉。
    • @Robin - 放置 FillCheckBoxList();在 IsPostBack 块内,什么是 dpPrintRounds?
    • 这是一个数据选择器,我好像忘记了那部分,但我已经添加了它。当我动态搜索时,我的复选框列表会发生变化。 dpPrintRounds 是一个日期选择器字段,我用它来过滤某个日期。
    • 是否有任何数据选择器控制事件在选择新日期时添加回发。如果是这样,则在该处理程序中调用 FillCheckBoxList() 方法,但 page_load 中的代码必须在 IsPostBack 块内。
    【解决方案2】:

    您是否确定这不是由于投射到 ListItem 造成的? 不应该需要,但值得一试...

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

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

    【讨论】:

    • 绑定发生在获取选定值之前。我已经在那里设置了一个断点。它说 item.Selecten 每次都是假的。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

      • 链接到底说了什么?如果此链接过时会怎样?
      • @ivan - 一个很好的问题,但真的不想复制和粘贴整个博客代码,除了复制别人的工作之外,它会填满这篇文章的三四个屏幕。好点,但最好做什么。
      猜你喜欢
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多