ASP.Net - 从 RepeaterItem 获取数据

发布于 2024-12-15 00:41:09 字数 1524 浏览 5 评论 0原文

我对 ASP.Net 还很陌生,我不确定我是否以正确的方式处理这个问题。我有一个中继器,它绑定到“图像”对象列表。每个 RepeaterItem 中都有一个复选框,并且有一个按钮 OnClick 事件,我想在该事件中显示选中的 Image 对象的一些属性。

标签更新,但元数据为空。 DataBinder.Eval(i.DataItem, "FileName") 返回 null,但我不确定为什么?我认为也许来自复选框的回发导致了问题,但如果我尝试在发生任何回发之前显示数据,我仍然会遇到同样的问题,所以也许我没有正确获取属性。还是我以完全错误的方式处理这件事?任何帮助表示赞赏。

代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        string importPath = Server.MapPath("~/Images/ForImport");
        ImageProcessor processor = new ImageProcessor(importPath);

        rptImageList.DataSource = processor.ImageList;
        rptImageList.DataBind();
    }
}

protected void btnImport_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem i in rptImageList.Items)
    {
        CheckBox chk = i.FindControl("chkSelectImage") as CheckBox;
        if (chk.Checked)
        {
            Testlabel.Text += "Selected: " + DataBinder.Eval(i.DataItem, "FileName");
        }
    }
}

HTML:

<asp:Repeater ID="rptImageList" runat="server">
    <ItemTemplate>
    <div class="photoinstance">
        <asp:Image runat="server" ImageUrl='<%#"Images/ForImport/" +DataBinder.Eval(Container.DataItem, "FileName") %>' />
        <asp:CheckBox ID="chkSelectImage" AutoPostBack="true" runat="server"/>
        <p><%#Eval("FileName")%> - <%#Eval("FileSize")%> bytes</p>
        </div>
    </ItemTemplate>
</asp:Repeater>

I'm pretty new to ASP.Net and I'm not sure I'm going about this the right way. I have a Repeater which is bound to a list of "Image" objects. Within each RepeaterItem is a checkbox and I have a button OnClick event, which I want to display some attributes of the checked Image objects.

The label updates, but the metadata is blank. DataBinder.Eval(i.DataItem, "FileName") is coming back null, but I'm not sure why? I thought perhaps the postback from the checkbox was causing problems but I still get the same issue if I try to display the data before any postbacks have occurred, so perhaps I'm not fetching the attributes correctly. Or am I going about this in completely the wrong way? Any help appreciated.

Code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        string importPath = Server.MapPath("~/Images/ForImport");
        ImageProcessor processor = new ImageProcessor(importPath);

        rptImageList.DataSource = processor.ImageList;
        rptImageList.DataBind();
    }
}

protected void btnImport_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem i in rptImageList.Items)
    {
        CheckBox chk = i.FindControl("chkSelectImage") as CheckBox;
        if (chk.Checked)
        {
            Testlabel.Text += "Selected: " + DataBinder.Eval(i.DataItem, "FileName");
        }
    }
}

HTML:

<asp:Repeater ID="rptImageList" runat="server">
    <ItemTemplate>
    <div class="photoinstance">
        <asp:Image runat="server" ImageUrl='<%#"Images/ForImport/" +DataBinder.Eval(Container.DataItem, "FileName") %>' />
        <asp:CheckBox ID="chkSelectImage" AutoPostBack="true" runat="server"/>
        <p><%#Eval("FileName")%> - <%#Eval("FileSize")%> bytes</p>
        </div>
    </ItemTemplate>
</asp:Repeater>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吐个泡泡 2024-12-22 00:41:09

i.DataItembtnImport_Click 中不可用(为空),但仅在 ItemDataBound 事件中可用(如果我没记错事件名称) 。

您可以使用 HiddenField 来存储 FileName,然后您必须调用 i.FindControl

i.DataItem is not available (is null) at btnImport_Click but is available only at the ItemDataBound event (if I recall correctly the event name).

You can use a HiddenField to store the FileName then you will have to call i.FindControl.

岁月染过的梦 2024-12-22 00:41:09

我认为这个问题是问如何在回发时从转发器获取数据,更具体地说,如何与转发器内的复选框进行交互。因此,在另一个控件的回发中,如何执行此操作的示例是:

    protected void CheckBox_CheckedChanged(object sender, EventArgs e)
    {
        foreach (RepeaterItem ri in Repeater.Items)
        {
            foreach (Control c in ri.Controls)
            {
                if (typeof(CheckBox) == c.GetType())
                {
                    CheckBox checkBox = (CheckBox)c;
                    checkBox.Checked = true;
                }
            }
        }
    }

I think this question is asking how to get data from a repeater on postback and more specifically how to interact with a CheckBox that is within a repeater. So on the postback of another control an example of how to do this is;

    protected void CheckBox_CheckedChanged(object sender, EventArgs e)
    {
        foreach (RepeaterItem ri in Repeater.Items)
        {
            foreach (Control c in ri.Controls)
            {
                if (typeof(CheckBox) == c.GetType())
                {
                    CheckBox checkBox = (CheckBox)c;
                    checkBox.Checked = true;
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文