单击按钮时浏览DataList控件的所有行

本文关键字:控件 DataList 按钮 浏览 单击 | 更新日期: 2023-09-27 17:59:28

我需要在单击按钮时检查数据列表的每一行,以检查每一行中的复选框是否被选中。我把按钮放在DataList的FooterTemplate中,但我还找不到方法。这是我的ItemCommand方法;

protected void DataList1_ItemCommand(object sender, DataListCommandEventArgs e) {
    if (e.Item.ItemType == ListItemType.Footer) {
        if (e.CommandName == "AddContinue") {

        } else if (e.CommandName == "SkipContinue") {

        }
    }
}

这是我的脚丫;

<FooterTemplate>
    <div class="top-margin-25">
        <div class="left-floathy">
            <asp:Button runat="server" ID="btnPreviousStep"  Text="<<< Previous Page" 
                class="blueButtonSmall boxShadow" onclick="btnPreviousStep_Click" />
        </div>
        <div class="right-floathy">
            <asp:Button runat="server" ID="btnAddContinue" Text="Add & Contuniue >>>" 
                class="blueButtonSmall boxShadow" CommandName="AddContinue" /><br />
        </div>
        <div class="clarFix"></div>
        <div class="right-floathy">
            <asp:Button runat="server" ID="btnSkipContinue" Text="Skip & Continue >>>" 
                class="blueButtonSmall boxShadow" CommandName="SkipContinue" />
        </div>
        <div class="clarFix"></div>
    </div>
</FooterTemplate>

单击按钮时浏览DataList控件的所有行

好吧,显然我有点粗心,没有看到DataList.Items的东西。答案是坐在这里;

http://blog.ysatech.com/post/2011/06/03/ASPNET-Get-selected-checkbox-value-in-DataList.aspx

编辑

对于其他有同样问题的人,以下是代码;

    protected void DataList1_ItemCommand(object sender, DataListCommandEventArgs e) {
        if (e.Item.ItemType == ListItemType.Footer) {
            if (e.CommandName == "AddContinue") {
                foreach (DataListItem item in DataList1.Items) {
                    CheckBox extraCheck
                        = item.FindControl("extraCheck") as CheckBox;
                    if (extraCheck != null) {
                        if (extraCheck.Checked) {
                            Response.Write(item.ItemIndex);
                        }
                    }
                }
            } else if (e.CommandName == "SkipContinue") {

            }
        }
    }