循环通过datalist失败

时间:2011-12-13 22:05:10

标签: c# asp.net

我需要处理来自datalist的选择,但是我循环遍历datalist中的项目的forech循环不起作用。基本上,如果选中该复选框,我想获取chekc框工具提示中的电子邮件地址。数据列表计数为零。有人可以帮我这个吗?

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

2 个答案:

答案 0 :(得分:1)

将viewstate设置为true诀窍 - 谁新? (阿德里安做了......)

答案 1 :(得分:0)

你有

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

这很可能是

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

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