Reputation: 613
I've been trying to do this since yesterday but can't think of a solution. I have a repeater containing a checkbox and a fileupload, this repeater repeats numerous times depending on the content of my table. When the checkbox below the file upload is checked it shouldnt check the fileupload. I can't think of any way to do this. Any ideas? Heres the code.
The class:
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;
}
}
The view:
<!-- 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>
As u can see its just a logics problem... Can anyone give me an example on how to solve this?
Upvotes: 5
Views: 3534
Reputation: 1309
You are probably binding items to the repeater during the Page Load. Are you checking for PostBack?
What I think is happening is that when you click the button the page is reloaded and the repeater gets filled with your data, overwriting the checkbox choices you have made. Just make sure you do something like this in your Page Load:
if(!Page.IsPostBack)
{
//Fill repeater with items here
}
Now when you read out the repeater items after the button click you should see the actual value of the checkboxes instead of it always being false.
Upvotes: 3