Search code examples
asp.netasprepeater

Getting a url of an image nested in a Repeater asp.net


I am trying to get the imageurl of a image that is nested in a repeater. But i dont know what i am doing wrong, i always get a null result. Here is what i have been trying so far

this is the code behind.

    public partial class CreateSession : System.Web.UI.Page
{
    ImagesForSession img = new ImagesForSession();
    WordForSession wrd = new WordForSession();
    RolesForSession rol = new RolesForSession();
    CreateSes newSes = new CreateSes();
    ManyToMany mToM = new ManyToMany();

    protected void Page_Load(object sender, EventArgs e)
    {
        repImagesOnSes.DataSource = img.GetAllImages();
        repImagesOnSes.DataBind();

        repWordsOnSes.DataSource = wrd.GetAllWords();
        repWordsOnSes.DataBind();

        repRolesOnSes.DataSource = rol.GetAllRoles();
        repRolesOnSes.DataBind();
    }

    protected void btnCreateSession_Click(object sender, EventArgs e)
    {
        var userID = User.Identity.GetUserId();
        newSes.CreateNewSession(userID, true, txtSesName.Text);

        //var checkBox = repImagesOnSes.FindControl("imgCheckBox") as CheckBox;
        //var imgControl = (System.Web.UI.WebControls.Image)repImagesOnSes.FindControl("imgForSession");
        //var imgUrl = imgControl.DescriptionUrl;
        //var imgID = img.GetImageId(imgUrl);

        //mToM.AddImagesToSession(newSes.GetNewestSession(userID), imgID);


        foreach (RepeaterItem i in repImagesOnSes.Items)
        {
            CheckBox chk = i.FindControl("imgCheckBox") as CheckBox;

            if (chk.Checked)
            {
                var image = (System.Web.UI.WebControls.Image)i.FindControl("imgForSession");
                var imgUrl = image.ImageUrl;
                var imgID = img.GetImageId(imgUrl);

                mToM.AddImagesToSession(newSes.GetNewestSession(userID), imgID);
            }
        }

    }

}

and here is the html

<asp:Repeater ID="repImagesOnSes" runat="server" >
                    <ItemTemplate>
                        <div class="row">
                            <div class="col-md-10 col-sm-10 col-xs-10">
                                <asp:Image ID="imgForSession" CssClass="img-responsive" runat="server" ImageUrl='<%# string.Format("~/Images/{0}", Eval("FileName")) %>' Height="150px" />
                            </div>
                            <div class="col-md-2 col-sm-2 col-xs-2" style="padding-top: 65px;">
                                <asp:CheckBox ID="imgCheckBox" runat="server" />
                            </div>
                        </div>
                        <hr />
                    </ItemTemplate>
                </asp:Repeater>

i have tried to make a hidden field in the repeater item but i cant seem to get that to work either. Hope some of you can help, thanks!


Solution

  • Are you looping through each item of the repeater to determine if the checkbox is checked? You should be finding the control within the repeater item. The following code snippet should help.

    foreach (RepeaterItem ri in repImagesOnSes.Items)
    {
        CheckBox chk = (CheckBox)ri.FindControl("imgCheckBox");
    
        if (chk.Checked)
        {
            Image imgControl = (Image)ri.FindControl("imgForSession");
            var imgUrl = imgControl.ImageUrl;
        }
    }