【问题标题】:ASP.NET loses data from form after postback - repeater inside updatepanel inside repeaterASP.NET 在回发后从表单中丢失数据 - 中继器内的更新面板内的中继器
【发布时间】:2013-10-08 10:27:26
【问题描述】:

我有一个 ASP.NET 网络表单,其中我有几个复选框等来制作 SearchCriteria 对象。当我单击提交按钮时,我要求repeater1 根据 SearchCriteria 显示一些记录。在中继器1 中,我使用更新面板和另一个中继器(ChildRepeater)进行用户控制。

我的问题:当我在回发后单击按钮上的发送时,我丢失了表单中的所有数据(例如,复选框被选中)。页面正在加载默认值,但在我的代码中,我仅在(!IsPostBack)时才加载默认值(选中所有复选框)。 当然,我在 .aspx 中有“EnableViewState=true”,我也尝试在会话中保存表单中的数据,但它也不起作用(默认值重写此设置)。

当我只有一个中继器 (repeater1) 时,我在回发后的表单中包含了我放入的所有数据,所以我认为 updatepanel 中存在一些问题,但我不知道具体在哪里。 你能解释一下为什么我的表单在页面重新加载后会丢失数据吗?我看不出有什么错误..

底部是我的文件,但我有 n 层架构,所以...我只从 View/BLL 中放置必要的文件(DAL 和 sql 连接工作正常) 这里有一个屏幕外观:screen

ShowAgreements.aspx(它包含控件和父中继器):

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
        <HeaderTemplate>
            <table style="border: 1px solid #0000FF; width: 1250px">
                <tr style="background-color: #3d6db0; color: #000000; font-weight: bold;">

                    <td style="width: 150px;">Numer</td>
                    <td style="width: 150px;">Przedmiot umowy</td>
                    <td style="width: 150px;">Odbiorca</td>
                    <td style="width: 150px;">Dostawca</td>
                    <td style="width: 150px;">Rodzaj umowy</td>
                    <td style="width: 150px;">Charakter umowy</td>
                    <td style="width: 150px;">Status wypożyczenia</td>
                    <td style="width: 150px;">Data podpisania</td>
                    <td style="width: 150px;">Status</td>
                </tr>
        </HeaderTemplate>

      <itemtemplate>
        <uc1:FilesRepeaterControl runat="server" id="FilesRepeaterControl" />
          </itemtemplate>
        <FooterTemplate>         
            </table>
        </FooterTemplate>
    </asp:Repeater>

ShowAgreements.aspx.cs:

AgreementsSearchCriteria AgreementSearchCriteriaObj = new AgreementsSearchCriteria();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Controller.ReadDostawcy();
            Controller.ReadOdbiorcy();
            Controller.ReadPracownikOdp();
        }

        Controller.ReadAllAgreements();
    }
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
             Agreement umowa = e.Item.DataItem as Agreement;
             BazaUmow.Controls.FilesRepeaterControl test = e.Item.FindControl("FilesRepeaterControl") as FilesRepeaterControl;
             test.umowa = umowa;
        }

    }


protected void szukaj_Click(object sender, EventArgs e)
    {
        if (IsValid == true)
        {
            AddNaviPoint();
            Controller.ReadAllAgreements();
        }

    }
public AgreementSearchCollection BindUmowyResults
    {
        set 
        {
            Repeater1.DataSource = value;
            Repeater1.DataBind();
        }
    }

FilesRepeaterControl.ascx

            <tr>
                <td style="width: 150px">
                    <asp:Button ID="btnRozwin" runat="server" Text="+" OnClick="btnRozwin_Click" />
                    <asp:LinkButton ID="lbl_numerUmowy" runat="server" OnClick="lbl_numerUmowy_Click"></asp:LinkButton>
                </td>

                <td style="width: 150px">
                    <asp:Label ID="lbl_przedmiotUmowy" runat="server" /></td>
                <td style="width: 150px">
                    <asp:Label ID="lbl_odbiorca" runat="server" /></td>
                <td style="width: 150px">
                    <asp:Label ID="lbl_dostawca" runat="server" /></td>
                <td style="width: 150px">
                    <asp:Label ID="lbl_rodzajUmowy" runat="server" /></td>
                <td style="width: 150px">
                    <asp:Label ID="lbl_charakterUmowy" runat="server" /></td>
                <td style="width: 150px">
                    <asp:Label ID="lbl_statusWypozyczenia" runat="server" /></td>
                <td style="width: 150px">
                    <asp:Label ID="lbl_dataPodpisania" runat="server" /></td>
                <td style="width: 150px">
                    <asp:Label ID="lbl_statusUmowy" runat="server" /></td>

            </tr>
            <tr>
                <td>
                    <asp:UpdatePanel ID="UpdatePanelFilesInfo" runat="server">
                        <ContentTemplate>
                <asp:Repeater ID="filesRptr" runat="server" OnItemDataBound="filesRptr_ItemDataBound">
                <HeaderTemplate>
                    <table style=" width:170px">
                        <tr style="font-weight: bold;">
                            <td style="width:170px;">Pliki umowy</td>

                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                       <td>
                            <asp:LinkButton ID="lblPlikUmowy" runat="server" OnCommand="lblPlikUmowy_Command"></asp:LinkButton></td>

                    </tr>

                </ItemTemplate>
                <FooterTemplate>
                    </table>

                </FooterTemplate>
            </asp:Repeater>


                <asp:Repeater ID="aneksyRptr" runat="server" OnItemDataBound="aneksyRptr_ItemDataBound">

                    <HeaderTemplate>
                    <table style=" width:170px">
                        <tr style="font-weight: bold;">
                            <td style="width:170px;">Aneksy do umowy</td>

                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                       <td>
                            <asp:LinkButton ID="lblAneksUmowy" runat="server" OnCommand="lblAneksUmowy_Command"></asp:LinkButton></td>

                    </tr>

                </ItemTemplate>
                <FooterTemplate>
                    </table>

                </FooterTemplate>
            </asp:Repeater>
            </ContentTemplate>

                    </asp:UpdatePanel>
                </td>
            </tr>

FilesRepeaterControl.ascx.cs(这里我们添加数据到父转发器并在我们单击按钮 btnRozwin 时填充两个子转发器) 公共协议 umowa { 获取;放; }

    protected void Page_Load(object sender, EventArgs e)
    {
                lbl_numerUmowy.Text = umowa.numer_umowy.ToString();
                lbl_przedmiotUmowy.Text = umowa.przedmiot_umowy.ToString();
                lbl_odbiorca.Text = umowa.odbiorca_tekst.ToString();
                lbl_dostawca.Text = umowa.dostawca_tekst.ToString();
                lbl_rodzajUmowy.Text = umowa.rodzaj_umowy_tekst.ToString();
                lbl_charakterUmowy.Text = umowa.charakter_umowy_tekst.ToString();
                lbl_statusWypozyczenia.Text = umowa.status_wypozyczenia_tekst.ToString();
                lbl_dataPodpisania.Text = umowa.data_podpisania.ToString("yyyy-MM-dd");
                lbl_statusUmowy.Text = umowa.status_umowy.ToString();
    }

    protected void filesRptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Plik plik = e.Item.DataItem as Plik;

            LinkButton lblPlikUmowy = e.Item.FindControl("lblPlikUmowy") as LinkButton;
            lblPlikUmowy.Text = plik.nazwa_pliku.ToString();
            lblPlikUmowy.CommandArgument = plik.guid + plik.rozszerzenie;

        }

    }


    protected void aneksyRptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Plik plik = e.Item.DataItem as Plik;

            LinkButton lblAneksUmowy = e.Item.FindControl("lblAneksUmowy") as LinkButton;
            lblAneksUmowy.Text = plik.nazwa_pliku.ToString();
            lblAneksUmowy.CommandArgument = plik.guid + plik.rozszerzenie;

        }

    }

    protected void btnRozwin_Click(object sender, EventArgs e)
    {
        if (btnRozwin.Text == "+")
        {
            filesRptr.Visible = true;
            aneksyRptr.Visible = true;

            filesRptr.DataSource = Controller.ReadFilesByAgreement(Convert.ToInt32(lbl_numerUmowy.Text), 1);
            filesRptr.DataBind();

            aneksyRptr.DataSource = Controller.ReadFilesByAgreement(Convert.ToInt32(lbl_numerUmowy.Text), 2);
            aneksyRptr.DataBind();

            btnRozwin.Text = "-";

        }

        else
        {
            filesRptr.Visible = false;
            aneksyRptr.Visible = false;
            btnRozwin.Text = "+";

        }

    }

    protected void lbl_numerUmowy_Click(object sender, EventArgs e)
    {
        string numer_umowy = ((LinkButton)sender).Text;
        Page.Response.Redirect("~/SeeAgreementData.aspx?numer=" + numer_umowy + "&email=" + Request.QueryString["email"]);
    }

}

【问题讨论】:

  • 尝试在您的代码、aspx 和代码后面发布一个简化版本。
  • 能否请您发布您尝试过的代码?

标签: c# asp.net


【解决方案1】:

这是因为当您回发更新面板时,它会刷新页面的一部分(被更新面板包围),因此该面板上的所有数据都会丢失,您可以将所需的所有数据保存在 ViewState

protected void UpdatePanelClick_OnClick(object sender, EventArgs e)
{
    List<int> checkedIds = new List<int>();
    //working with grid saving ids on list
    ViewState["saveUpdatePanelData"] = checkedIds;
}

比回发后的页面加载,您可以获取此数据并进行初始化

protected void Page_Load(object sender, EventArgs e)
{
    if (ViewState["saveUpdatePanelData"]!= null)
    {
        checkedIds = ViewState["saveUpdatePanelData"] as List<int>;
    }
}

这就是 ViewState 的工作方式。

【讨论】:

  • 所以如果我在 .aspx 网站上只使用 EnableViewState="true" 是不够的,我应该使用这样的列表吗?而且我觉得很奇怪,但是来自表单的数据(复选框的状态等 - 请参阅我添加的屏幕)不在更新面板中,所以我认为它应该保存在 enableviewstate 中,我错了吗?
  • @guitargirl 顺便说一句,当您单击 btnRozwin 时,您会在服务器上回发整个页面(而不是其中的一部分)。这就是它从您的页面加载初始化的原因。尝试将您的 btnRozwin 放入 updatepanel。您可以在 ViewState obj 上保存任何信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多