7

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>
1
  • 1
    The DataItem property is only ever available before postback, in the ItemDataBound event. This isn't pointed out very well on MSDN, but is consistent throughout all ASP.NET controls
    – jwheron
    Nov 12, 2011 at 18:22

2 Answers 2

9

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.

0
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.