获取嵌套在 Repeater asp.net 中的图像的 url

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

我正在尝试获取嵌套在转发器中的图像的 imageurl。但我不知道我做错了什么,我总是得到一个空结果。这是我到目前为止一直在尝试的

这是后面的代码。

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

    }

}

这里是 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>

我曾尝试在转发器项目中创建一个隐藏字段,但我似乎也无法让它工作。希望各位大神帮忙,谢谢!

您是否循环遍历中继器的每个项目以确定复选框是否被选中?您应该在转发器项目中找到控件。以下代码片段应该有所帮助。

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

将您的数据绑定包装在 !IsPostBack 中,因为您正在清除您的选择。

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

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

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