我的用户控件放在我的.aspx页面中。在按钮单击事件上动态添加用户控件。我的问题是,在回发后,我可以检索放置在用户控件内的服务器控件,但无法检索它们的值。
以下是我的用户控件的.ascx页面
的代码 <%@ Control Language="C#" AutoEventWireup="true" odeFile="qualificationControl.ascx.cs" Inherits="qualificationControl" %>
<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>
以下是我的.axcs.cs页面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class qualificationControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
using (DataOperation oDo = new DataOperation())
{
DataTable dt = oDo.DropDownList("select * from tblqualificationMaster");
foreach (DataRow row in dt.Rows)
{
courseList.Items.Add(new ListItem(row[1].ToString(), row[0].ToString()));
}
}
}
catch (Exception ex)
{
throw;
}
}
}
我的.aspx页面的代码
<%@ Page Title="Application Form Level2" Language="C#" MasterPageFile="~/AppMaster.master" AutoEventWireup="true" CodeFile="AppplicationForm2.aspx.cs" Inherits="AppplicationForm2" %>
<%@ Register src="Control/qualificationControl.ascx" tagname="qualificationControl" tagprefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table width="100%">
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td class="SubTitle">
Education details:</td>
</tr>
<tr>
<td runat="server" id="tdQualificationn">
<%--<uc1:qualificationControl ID="qualificationControl1" runat="server" />--%>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="UserCtrlHolder" runat="server"></asp:PlaceHolder>
</ContentTemplate>
<%-- <Triggers> <asp:AsyncPostBackTrigger ControlID="addQualificationBtn" /></Triggers>--%></asp:UpdatePanel>
</td>
</tr>
<tr>
<td align="center">
<asp:Button ID="addQualificationBtn" runat="server"
Text="Add More Qualifications" Height="40px"
onclick="addQualificationBtn_Click" />
</td>
</tr>
</table>
</asp:Content>
我的.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;
ArrayList CtrlList = new ArrayList();
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);
Session.Add("ControlHolder", UserCtrlHolder);
}
}
protected void addQualificationBtn_Click(object sender, EventArgs e)
{
RememeberOldValues();
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]);
}
}
public void RememeberOldValues()
{
try
{
if (Session["ControlHolder"] != null)
{
ArrayList CourseList = new ArrayList();
ArrayList YearList = new ArrayList();
ArrayList percentageList = new ArrayList();
ArrayList InstituteList = new ArrayList();
ArrayList CtrlList = (ArrayList)Session["qualicontrol"];
PlaceHolder PlaceHolder = (PlaceHolder)Session["ControlHolder"];
for (int intListCnt = 0; intListCnt < CtrlList.Count; intListCnt++)
{
Control userControl = (Control)PlaceHolder.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");
CourseList.Add(dlCourseList.SelectedValue);
YearList.Add(dlYearList.SelectedValue);
percentageList.Add(percentageBox.Text);
InstituteList.Add(InstiNameBox.Text);
}
}
}
catch (Exception ex)
{
throw;
}
}
}
请指导我如何检索这些值。
答案 0 :(得分:1)
您必须从用户控件中找到特定控件
((DropDownList)qualificationControl1.FindControl("yearList")).SelectedValue