动态用户控件在多视图中切换视图时丢失数据

时间:2013-08-03 18:20:05

标签: asp.net viewstate multiview dynamic-usercontrols

我有一个带有两个视图的MultiView(第一个带有表单,第二个用于查看在第一个视图中输入的数据)。第一个视图包含TextBox和PlaceHolder。 PlaceHolder用于保存动态添加的UserControl。 UserControl包含DropDownList(s),TextBox和CheckBox。    工作正常:目前我可以通过添加按钮在页面中添加新的用户控件,这会导致页面返回PostBack,但我不会丢失现有动态UserControl中的任何数据。    问题:当我在视图之间切换(使用SetActiveView)时,我在动态添加的用户控件中丢失数据,但静态TextBox中的数据保持不变。

以下是我的代码段。

<%@ Page Title="" Language="C#" MasterPageFile="~/M.master" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<%@ Reference Control="dynamic_uc.ascx" %>
<%@ Register src="dynamic_uc.ascx" tagname="ucrange" tagprefix="uc1" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<div class="light_blue_background pad10 helvetica innerpad5">
    <asp:MultiView ID="mv_main" runat="server" ActiveViewIndex="0">
            <asp:View ID="vu_add" runat="server">
        <table>
            <tr style="border-bottom: 1px solid #ccc;">
                <td>
                     Name:
                </td>
                <td>
                    <asp:TextBox ID="txt_Name" runat="server" Columns="50" MaxLength="35" />
                </td>
            </tr>

        </table>

            <asp:Panel ID="pnl_TempRange_" runat="server">

    </asp:Panel>
 <asp:PlaceHolder ID="pl_Range" runat="server"></asp:PlaceHolder>
    </asp:View>
    <asp:View ID="vu_review" runat="server">
        review your data
    </asp:View>
    </asp:MultiView>

    <asp:Button ID="btn_addRange" runat="server" Text="Add Range" 
        CausesValidation="False" onclick="btn_addRange_Click" />

    <asp:Button ID="btn_submit" runat="server" Text="Submit" 
        onclick="btn_submit_Click" />

    <asp:Button ID="btn_modify" runat="server" Text="Modify" 
        onclick="btn_modify_Click" Visible="false"/>

</div>

</asp:Content>

C#代码:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Test : System.Web.UI.Page
{
    private static int ControlCount=0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           ControlCount = 0;
        }
        AddControls();
        foreach (Control c in pnl_TempRange.Controls)
        {
            DropDownList ddl_UpLimit = (DropDownList)c.FindControl("ddl_Uplimit");
            PopulateDropdownRange(ddl_UpLimit, 0, 100);
            DropDownList ddl_LowLimit = (DropDownList)c.FindControl("ddl_Lowlimit");
            PopulateDropdownRange(ddl_LowLimit, 0, 100);
        }
        Response.Write(tests);
    }

    public static Control GetPostBackControl(Page page)
    {
        Control control = null;

        string ctrlname = page.Request.Params.Get("__EVENTTARGET");
        if (ctrlname != null && ctrlname != string.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control c = page.FindControl(ctl);
                if (c is System.Web.UI.WebControls.Button)
                {
                    control = c;
                    break;
                }
            }
        }
        return control;
    }
    private void AddControls()
    {
        int ID = 0;
        Control cd =  GetPostBackControl(Page);
        if ((c != null))
        {
            if (c.ID.ToString() == "btn_addRange")
            {
                dynamic_uc++;
            }
        }
        pl_Range.Controls.Clear();
        for (int i = 0; i <= tempControlCount; i++)
        {
            Range DynamicUserControl = (Range)Page.LoadControl("dynamic_uc.ascx");

            while (IsInDeletedLog("uc" + ID) == true)
            {
                ID += 1;
            }
            DynamicUserControl.ID = "dyn_uc" + ID;

            DynamicUserControl.RemoveTempControl += this.HandleRemoveTempRange;
            pl_Range.Controls.Add(DynamicUserControl);
            ID += 1;
        }


    }

    protected void btn_addRange_Click(object sender, EventArgs e)
    {

    }

    private void PopulateDropdownRange(DropDownList ddl, int rangeMin, int rangeMax)
    {
        ddl.Items.Clear();
        ddl.Items.Add("--Select--");
        for (int i = rangeMin; i <= rangeMax; i++)
        {
            ddl.Items.Add(new ListItem(i.ToString(),i.ToString()));
        }
    }
    private void BindDDl(DropDownList ddl,string strQuery)
    {
        ddl.DataSource = GetDataFromQuery(strQuery);
        ddl.DataValueField = "VALUE";
        ddl.DataTextField = "NAME";
        ddl.DataBind();
        ddl.Items.Insert(0,"--Select--");
    }

    protected void btn_submit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {         
            mv_main.SetActiveView(vu_reviewCmpd);
            btn_submitCompound.Visible = false;

            btn_confirmCompound.Visible = true;
            btn_modifyCompound.Visible = true;
        }
    }

    protected void btn_modify_Click(object sender, EventArgs e)
    {
        mv_main.SetActiveView(vu_add);
        btn_submit.Visible = true;

        btn_confirm.Visible = false;
        btn_modify.Visible = false;
    }

感谢您的帮助。

0 个答案:

没有答案