在中继器控件中使用复选框
从昨天开始我就一直在尝试这样做,但想不出解决方案。 我有一个包含复选框和文件上传的中继器,该中继器根据我的表格内容重复多次。当选中文件上传下方的复选框时,不应检查文件上传。我想不出有什么办法可以做到这一点。有什么想法吗?这是代码。
类:
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>
正如你所看到的,这只是一个逻辑问题......任何人都可以给我一个如何解决这个问题的例子吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能在页面加载期间将项目绑定到转发器。您正在检查回发吗?
我认为正在发生的事情是,当您单击按钮时,页面将重新加载,并且中继器会填充您的数据,从而覆盖您所做的复选框选择。只需确保在页面加载中执行类似的操作:
现在,当您在单击按钮后读出中继器项目时,您应该看到复选框的实际值,而不是始终为 false。
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:
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.