如何在回发中保留asp转发器中的数据

时间:2016-08-09 09:59:53

标签: c# asp.net

以下是标记页:

<%@ Page Title="" Language="C#" MasterPageFile="~/Master/MainPage.master" EnableViewState="true" AutoEventWireup="true" CodeBehind="edit.aspx.cs" Inherits="Website.View.edit" %>

...

 <asp:Repeater runat="server" ID="rptSample">
            <ItemTemplate>
                <asp:HyperLink runat="server" ID="refLink" href='http://www.sample.com' Text='Test data' />
                <asp:CheckBox runat="server" ID="chkbxDelete" Text="Delete" />
            </ItemTemplate>
            <SeparatorTemplate>
                <br />
            </SeparatorTemplate>
  </asp:Repeater>

以下是我如何绑定代码背后的数据:

protected void Page_Load(object sender, EventArgs e)
{
            rptSample.DataSource = getData();
            rptSample.DataBind();
}

我也试过这个:

protected void Page_Load(object sender, EventArgs e)
{
       if(!IsPostBack)
        {
            rptSample.DataSource = getData();
            rptSample.DataBind();
        }
}

并试图获取btnSave_Click函数的数据

protected void btn_save_Click(object sender, EventArgs e) 
{
  foreach(RepeaterItem item in rptSample.Items) 
  {
    CheckBox chkbx = (CheckBox) item.FindControl("chkbxDelete");
    if (chkbx.Checked) 
    {
        //Do something
    }
  }
}

如果我没有添加!IsPostBack,则rptSample.Items将为空,但如果我将其删除,则该复选框将始终为false。

问题是什么?

编辑: 根据用户的要求,这里是整页加载功能:

protected void Page_Load(object sender, EventArgs e)
{
      bindData();
}

protected void bindData()
{
    List<sp_SelectAttachments_Result> attachments = DAL.SelectAttachmentsByID(Request["ID"]);
    if (attachments.Count == 0)
        divAttachments.Visible = false;
    else
    {
        divAttachments.Visible = true;
        rptAttachments.DataSource = attachments;
        rptAttachments.DataBind();
    }
}

这里是divAttachments和rptAttachments的标记页

<div runat="server" id="divAttachments" visible="false">
        <asp:Repeater runat="server" ID="rptAttachments">
            <ItemTemplate>
                <asp:HiddenField Value='<%# Eval("ID") %>' runat="server" ID="hidID" />
                <asp:HyperLink runat="server" ID="refLink" href='<%# $"/Utils/getFile.ashx?ID={ID}" %>' Text='<%# ((sp_SelectAttachments_Result)Container.DataItem).FileName %>' />
                <asp:CheckBox runat="server" ID="chkbxDeleteAttachment" Text="Delete" />
            </ItemTemplate>
            <SeparatorTemplate>
                <br />
            </SeparatorTemplate>
        </asp:Repeater>
    </div>

2 个答案:

答案 0 :(得分:2)

我怀疑您的代码中有一些内容会清除复选框或重置转发器控件的数据源。

我建议创建一个干净的页面并使用下面的代码(我已经测试过并且它可以正常工作)。一旦你慢慢地工作,就开始向页面添加任何额外的逻辑,直到你弄清楚它是什么`导致问题:

代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        rptSample.DataSource = getData();
        rptSample.DataBind();
    }
}

private List<string> getData()
{
    return new List<string> { "1", "2", "3" };
}

protected void btn_save_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem item in rptSample.Items)
    {
        CheckBox chkbx = (CheckBox)item.FindControl("chkbxDelete");
        if (chkbx.Checked)
        {
            System.Diagnostics.Debugger.Break();
        }
    }
}

<强> .ASPX:

<form id="form1" runat="server">
    <asp:Repeater runat="server" ID="rptSample">
        <ItemTemplate>
            <asp:HyperLink runat="server" ID="refLink" href='http://www.sample.com' Text='Test data' />
            <asp:CheckBox runat="server" ID="chkbxDelete" Text="Delete" />
        </ItemTemplate>
        <SeparatorTemplate>
            <br />
        </SeparatorTemplate>
    </asp:Repeater>
    <asp:Button ID="btn_save" runat="server" Text="Save" OnClick="btn_save_Click" />
</form>

答案 1 :(得分:1)

使用Viewstate保存数据。在加载时将viewstate绑定到转发器,并使用!IsPostBack用于rptSample.DataBind();