在转发器控件中使用复选框

时间:2021-09-30 10:04:02

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?

你可以看到它只是一个逻辑问题......谁能给我一个如何解决这个问题的例子?

1 个解决方案

#1


3  

You are probably binding items to the repeater during the Page Load. Are you checking for PostBack?

您可能在页面加载期间将项目绑定到转发器。你在检查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.

现在,当您在按钮单击后读出转发器项目时,您应该看到复选框的实际值,而不是始终为false。

#1


3  

You are probably binding items to the repeater during the Page Load. Are you checking for PostBack?

您可能在页面加载期间将项目绑定到转发器。你在检查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.

现在,当您在按钮单击后读出转发器项目时,您应该看到复选框的实际值,而不是始终为false。