2015-05-26 157 views
0

我想获取嵌套在Repeater中的图像的图像。但我不知道我做错了什么,我总是得到一个空结果。这是我一直在尝试的东西获取嵌套在Repeater中的图像的url asp.net

这是背后的代码。

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> 

我曾试图弥补在转发项的隐藏字段,但我似乎无法得到这工作的。希望你们中的一些人能帮忙,谢谢!

+0

你什么时候执行后面的代码显示?项目数据是否在那一刻被绑定? –

+0

嘿,弗兰克。我的页面加载我给数据源和数据绑定它。 –

+0

您可以在源代码后面粘贴完整的代码吗? –

回答

0

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

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

嘿史蒂夫。现在问题已经在imgUrl变量上了。它说,当它试图从图像中获取URL时,该值为空。但是当我想检查最大框时,我将需要使用这个foreach循环。所以谢谢你! :) –

+0

我更新了可能帮助你的代码片段。我把查找图像控件放在了if中,因为在那之前你没有做任何事情。图像URL为空,因为您试图从repImagesOnSes中获取它,您应该从中继器项目中检索它。 – Steve

+0

由于某些原因,它仍然没有向数据库添加任何东西。我已经尝试了你的建议,并将其张贴在上面 –

0

将数据绑定包装在!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(); 
    } 
}