我从昨天起就一直试图这样做,但想不到解决方案。 我有一个包含复选框和文件上载的转发器,这个转发器根据我的表的内容重复多次。选中文件上传下方的复选框时,它不应检查文件上载量。我想不出有什么方法可以做到这一点。有任何想法吗?这是代码。
班级:
protected void UploadButton_Click(object sender, EventArgs e)
{
String savePath = @"~/files/";
try
{
foreach (RepeaterItem item in rptVrijstellingen.Items)
{
FileUpload file=(FileUpload)item.FindControl("FileUpload1");
HiddenField uid = (HiddenField)item.FindControl("hiddenid");
CheckBox ch = (CheckBox)item.FindControl("CBupload");
if(ch.Checked)
Response.Write("checked");
else
{
if (file.HasFile)
{
String fileName = file.FileName;
savePath += fileName;
file.SaveAs(Server.MapPath(savePath + fileName));
tblBijlage s = new tblBijlage();
s.bijlageTitel = fileName;
s.bijlageURL = savePath;
s.bijlageType = "1";
s.fk_externvakID = Convert.ToInt16(uid.Value);
BLLstudent.insertFile(s);
}
else
throw new Exception("Gelieve bij alle vakken een file toe te voegen of gegeven aan mevrouw Van Orlé aan te vinken en een afspraak te maken.");
}
Response.Redirect("s_student_Ovrijstellingen.aspx");
}
}
catch (Exception ex)
{
UploadStatusLabel.Text = ex.Message;
}
}
观点:
<!-- language: xml -->
<asp:Repeater ID="rptVrijstellingen" runat="server">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<h2><%# Eval("tblExternVak.ExternvakNaam") %></h2>
<asp:HiddenField ID="hiddenid" Value='<%# Eval("tblExternVak.pk_externvakID") %>' runat="server" />
<h4>Selecteer een bestand om te uploaden:</h4>
Gelieve het bestand de naam te geven van het overeenkomstige vak om de verwerking vlot te laten verlopen.
<br /><br /> <br />
<asp:FileUpload id="FileUpload1" runat="server"></asp:FileUpload>
<br />
<asp:CheckBox id="CBupload" runat="server" /><asp:Label id="lblUpload" runat="server"> Geleverd aan Mevrouw Van Orlé</asp:Label>
<hr />
</ItemTemplate>
<SeparatorTemplate><hr /></SeparatorTemplate>
</asp:Repeater>
<asp:Label id="UploadStatusLabel" runat="server" ForeColor="Red"></asp:Label>
<br /><br />
<asp:Button id="UploadButton" Text="volgende > " OnClick="UploadButton_Click" runat="server"></asp:Button>
你可以看到它只是一个逻辑问题......谁能给我一个如何解决这个问题的例子?
答案 0 :(得分:3)
您可能在页面加载期间将项目绑定到转发器。你在检查PostBack吗?
我认为发生的事情是,当您单击按钮时,页面将重新加载,转发器将填满您的数据,覆盖您所做的复选框选项。只需确保在页面加载中执行类似的操作:
if(!Page.IsPostBack)
{
//Fill repeater with items here
}
现在当您在单击按钮后读出转发器项目时,您应该看到复选框的实际值,而不是始终为假。