加载驻留在用户控件中的服务器控件的加载状态

时间:2011-04-14 12:21:20

标签: asp.net

我有用户控件,其中包含两个文本框和两个下拉列表。我是以编程方式首次在page_load事件上加载此控件。之后,在“添加新”按钮的每个按钮单击事件上,动态添加控件,但是当发生这种情况时,先前加载的控件将显示所有服务器控件(在用户控件内)为空。我如何保留这些控件的状态? 我的用户控制代码如下所示。

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="qualificationControl.ascx.cs" Inherits="qualificationControl" %>
<style type="text/css">

.RowHeight
{
    height: 30px;
}
</style>
<table width="100%">
    <tr>
        <td class="RowHeight" width="20%">
            Course Name</td>
        <td width="20%">
            <asp:DropDownList ID="courseList" runat="server" Width="100px">
            </asp:DropDownList>
        </td>
        <td width="20%">
            Year of Passing</td>
        <td width="*">
            <asp:DropDownList ID="yearList" runat="server" Width="100px">
                <asp:ListItem Value="0">2005</asp:ListItem>
                <asp:ListItem Value="1">2006</asp:ListItem>
                <asp:ListItem Value="2">2007</asp:ListItem>
                <asp:ListItem Value="3">2008</asp:ListItem>
                <asp:ListItem Value="4">2009</asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td class="RowHeight" width="20%">
            Percentage</td>
        <td colspan="3">
            <asp:TextBox ID="percentageBox" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="RowHeight" width="20%">
            Instutitute Name</td>
        <td colspan="3">
            <asp:TextBox ID="InstiNameBox" runat="server" Width="350px"></asp:TextBox>
        </td>
    </tr>
</table>

和aspx.cs页面如下所示。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class AppplicationForm2 : System.Web.UI.Page
{
    Control qualificationControl;
    UserControl usrqualificationControl = new UserControl();
    int CtrlID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        qualificationControl = usrqualificationControl.LoadControl("~/control/qualificationControl.ascx");
        if (!IsPostBack)
        {
            ArrayList CtrlList = new ArrayList();
            qualificationControl.ID = CtrlID.ToString();
            UserCtrlHolder.Controls.Add(qualificationControl);
            CtrlList.Add(qualificationControl);
            Session.Add("qualiControl", CtrlList);
        }
        //else 
    }
    protected void addQualificationBtn_Click(object sender, EventArgs e)
    {
        ArrayList CtrlList = null;
        ArrayList CourseList = new ArrayList();
        ArrayList YearList = new ArrayList();
        ArrayList percentageList = new ArrayList();
        ArrayList InstituteList = new ArrayList();
        if (Session["qualiControl"] != null)
        {
            CtrlList = (ArrayList)Session["qualicontrol"];
        }
        qualificationControl.ID = CtrlList.Count.ToString();
        CtrlList.Add(qualificationControl);
        for (int i = 0; i < CtrlList.Count; i++)
        {
            UserCtrlHolder.Controls.Add((Control)CtrlList[i]);
        }
    }
    private void RestoreUserControl(ArrayList CourseList,ArrayList YearList,ArrayList PercentageList,ArrayList InstiNameList,ArrayList CtrlList)
    {
        for (int CtrlCnt = 0; CtrlCnt < CtrlList.Count - 1; CtrlCnt++)
        {
            Control userControl = (Control)UserCtrlHolder.FindControl(CtrlID.ToString());
            DropDownList dlCourseList = (DropDownList)userControl.FindControl("courseList");
            DropDownList dlYearList = (DropDownList)userControl.FindControl("yearList");
            TextBox percentageBox = (TextBox)userControl.FindControl("percentageBox");
            TextBox InstiNameBox = (TextBox)userControl.FindControl("InstiNameBox");

            dlCourseList.ClearSelection();
            dlYearList.ClearSelection();
            dlCourseList.Items.FindByText(CourseList[CtrlCnt].ToString()).Selected = true;
            dlYearList.Items.FindByText(CourseList[CtrlCnt].ToString()).Selected = true;
            percentageBox.Text = PercentageList[CtrlCnt].ToString();
            InstiNameBox.Text = PercentageList[CtrlCnt].ToString();
        }
    }

}

2 个答案:

答案 0 :(得分:2)

使用Page_Init

使用Page_Load事件进行动态用户控制加载为时已晚,因为已加载了视图状态。因此,您的控件始终呈现为空。请尝试在Page_Init事件中添加用户控件。视图状态在此事件之后被加载,这确保您的动态控件应该 +++ 填充先前的状态。

  

注意+++ :如果确定性地添加动态控件一切都会好的,但是如果你有非确定性处理,你的控件在页面生命周期中会有不同的ID,因此视图状态将无法加载在每个回发。您提供的代码不应该有任何问题。

答案 1 :(得分:0)