遍历datalist失败

本文关键字:失败 datalist 遍历 | 更新日期: 2023-09-27 18:17:40

我需要处理来自数据列表的选择,但是我在数据列表中循环的forech循环不起作用。基本上,如果复选框被选中,我想获得的电子邮件地址,这是在复选框的工具提示。数据计数为零。有人能帮我一下吗?

<ajaxToolkit:Accordion ID="Accordion1" runat="server" selectedIndex="0"  FadeTransitions="true" FramesPerSecond="40" TransitionDuration="250" SuppressHeaderPostbacks="true" HeaderCssClass="Header">
            <Panes>
                <ajaxToolkit:AccordionPane ID="AccordionPane1" runat="server">
                    <Header>
                            <asp:LinkButton ID="lbtnSM" runat="server" Width="207px" Text="Users" Font-Bold="true"  CssClass="GeneralText"  /><br /><br />
                    </Header>
                    <Content>
                        <div class="emailleftcolumm">
                            <asp:DataList ID="dlSM" runat="server" DataKeyField="ID" OnItemCommand="dlSM_ItemCommand" 
                                 EnableViewState="false" Width="207px" OnItemDataBound="dlSM_ItemDataBound">
                                <HeaderStyle BorderWidth="1" BorderStyle="Inset" Font-Bold="true" />
                                <HeaderTemplate> 
                                   Select All <asp:CheckBox ID="chkSelectAll" runat="server" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkSelector" runat="server" Text='<%# Eval("firstname") + " " + Eval("lastname") + ", " + Eval("acronym") %>' 
                                        CssClass="GeneralText" ToolTip='<%# Eval("email") + ";" + Eval("altemail") %>'/>                                    </ItemTemplate>
                                <AlternatingItemTemplate>
                                    <div id="altbgcolor">
                                        <asp:CheckBox ID="chkSelector" runat="server" Text='<%# Eval("firstname") + " " + Eval("lastname") + ", " + Eval("acronym") %>'  
                                            ToolTip='<%# Eval("email") + ";" + Eval("altemail") %>' CssClass="GeneralText" />                                        </div>
                                </AlternatingItemTemplate>
                            </asp:DataList>
                        </div>
                    </Content>
                </ajaxToolkit:AccordionPane>

按钮点击事件处理程序如下所示:

protected void btnSend_Click(object sender, EventArgs e)
    {
        string _mailto = string.Empty;
        string _subject = txtSubject.Text;
        string _message = txtMessage.Text;
        DataList DataList1 = (DataList)FindControl("DataList1");
        foreach (DataListItem item in DataList1.Items)
        {
            CheckBox chkSelector = (CheckBox)item.FindControl("chkSelector");
            if (chkSelector.Checked)
            {
                _mailto += chkSelector.ToolTip + ";";
            }
        }
        // do mail call
        // Clean up
        lblEmailConfirm.Font.Bold = true;
        lblEmailConfirm.Text = "Subject: " + _subject + " Message: " + _message;
        txtSubject.Text = string.Empty;
        txtMessage.Text = string.Empty;
    }

遍历datalist失败

将viewstate设置为true就可以了。

你有

DataList DataList1 = (DataList)FindControl("DataList1"); 

应该是

DataList DataList1 = (DataList)FindControl("dlSM"); 

根据您提供的内容。我现在没有看到id为DataList1的控件