我试图获取嵌套在转发器中的图像的图像。但我不知道我做错了什么,我总是得到一个空结果。这是我到目前为止所尝试的内容
这是背后的代码。
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)
您是否循环遍历转发器的每个项目以确定是否选中了复选框?您应该在转发器项目中找到控件。以下代码段应该有所帮助。
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;
}
}
答案 1 :(得分: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();
}
}