Risho
Risho

Reputation: 2647

Looping through datalist fails

I need to process the selection from a datalist but the forech loop where I'm looping through the items in the datalist does not work. Basically if the checkbox is checked, I want to get the email address which is in the tooltip of the chekc box. The datalist count is zero. Can someone help me with this?

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

The button click event handler looks like this:

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

Upvotes: 0

Views: 2376

Answers (2)

Risho
Risho

Reputation: 2647

Setting viewstate to true did the trick - who new? (well Adrian did...)

Upvotes: 1

Jared Peless
Jared Peless

Reputation: 1120

You have

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

This is likely supposed to be

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

based on what you provided. I don't see a control with the id of DataList1 right now.

Upvotes: 0

Related Questions