ASP.Net - Retrieving Data from RepeaterItem

I am new to ASP.Net and I'm not sure if I will do it right. I have a repeater associated with a list of Image objects. Inside each RepeaterItem there is a checkbox, and I have an OnClick event on the button that I want to display some attributes of objects with checked images.

The label is updated, but the metadata is empty. DataBinder.Eval (i.DataItem, "FileName") is coming back, but I'm not sure why? I thought that maybe the feedback from the checkbox is causing problems, but I still get the same problem if I try to display the data before any callbacks occur, so maybe I am getting the attributes incorrectly. Or am I completely wrong about this? Any help was appreciated.

the 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> 
+6
source share
2 answers

i.DataItem is not available (null) in btnImport_Click, only available in the ItemDataBound event (if I remember the name of the event correctly).
You can use HiddenField to store the FileName, then you will need to call i.FindControl.

+8
source

I think this question asks how to get data from the repeater on the postback and, more specifically, how to interact with the CheckBox, which is inside the repeater. So, on the back of another control, an example of how to do this:

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

Source: https://habr.com/ru/post/901346/


All Articles