从页脚调用命令时获取转发器项复选框值

问题描述 投票:6回答:2

通过复选框列表进行以下转发器控制:

<asp:Repeater ID="rptItemList" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
    <div>
        <asp:CheckBox ID="chkItem" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ItemName").ToString() %>' />
        <asp:HiddenField ID="hdItem" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "ItemId").ToString() %>' />
    </div>
</ItemTemplate>
<FooterTemplate>
    <asp:LinkButton ID="lbtnDel" runat="server" Text="Delete" OnClick="lbtnDel_Click" OnClientClick="return confirm('Are you sure you want to delete selected items from this list?')"></asp:LinkButton>
</FooterTemplate>
</asp:Repeater>

并按照后面的代码来处理lbtnDel_Click事件:

    protected void lbtnDel_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem ri in rptItemList.Items)
        {
            CheckBox chk = (CheckBox)ri.FindControl("chkItem");
            HiddenField hd = (HiddenField)ri.FindControl("hdItem");

            if (chk.Checked)
            {
                var tc = new ItemController();
                tc.DeleteItem(Convert.ToInt32(hd.Value));
            }
        }
        Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
    }

当我选中一个复选框并单击“删除”时,代码会找到该复选框,但会将其读取为未选中状态,因此不会删除该项目。

有任何想法吗?

c# asp.net repeater
2个回答
10
投票

不是100%肯定,但你是否在每个页面加载中进行数据绑定?尝试只在!IsPostBack上绑定每当我遇到这样的问题时,通常是因为页面加载导致转发器重新绑定并杀死所有当前状态


0
投票

我在更新面板中有一个转发器。因此RepeaterItem中唯一的控件是DataBoundLiteralControl

这对我有用:

foreach (RepeaterItem item in rpLists.Items)
                {

                    if (item.Controls.Count > 0)
                    {
                        DataBoundLiteralControl dbLt = item.Controls[0] as DataBoundLiteralControl;
                        if (dbLt != null)
                        {
                            var controlCollection = this.ParseControl(dbLt.Text);
                            HtmlInputCheckBox cbInclude = (HtmlInputCheckBox) FindControl(controlCollection, "cbIncludeList");
                            if (cbInclude != null)
                            {
                                if (cbInclude.Checked)
                                {
                                    //your code here 
                                }
                            }
                        }
                    }
                }

我不得不为FindControl创建一个递归方法,如果它不是页面的一部分,它就不起作用了。耸肩见这里ASP.Net FindControl is not working - How come?

private Control FindControl(Control parent, string id)
        {
            if (parent.ID == id)
                return parent;

            if (parent.HasControls())
            {
                foreach (Control childControl in parent.Controls)
                {

                    if (childControl.ID == id)
                        return childControl;

                    if (childControl.HasControls())
                        return FindControl(childControl, id);
                }

            }

            return null;
        }
© www.soinside.com 2019 - 2024. All rights reserved.