Asp.net 如何检索服务器控件';返回后,s值放置在用户控件内
我在.aspx页面中放置了用户控件。在按钮单击事件中动态添加用户控件。我的问题是,回发后,我可以检索放置在用户控件中的服务器控件,但无法检索它们的值 这是我的用户控件的.ascx页面的代码Asp.net 如何检索服务器控件';返回后,s值放置在用户控件内,asp.net,Asp.net,我在.aspx页面中放置了用户控件。在按钮单击事件中动态添加用户控件。我的问题是,回发后,我可以检索放置在用户控件中的服务器控件,但无法检索它们的值 这是我的用户控件的.ascx页面的代码 <%@ Control Language="C#" AutoEventWireup="true" odeFile="qualificationControl.ascx.cs" Inherits="qualificationControl" %> &
<%@ 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>
my.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>
教育详情:
my.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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用系统集合;
公共部分类AppApplicationForm2:System.Web.UI.Page
{
控制资格控制;
UserControl usrqualificationControl=新UserControl();
int-CtrlID=0;
ArrayList CtrlList=新的ArrayList();
受保护的无效页面加载(对象发送方、事件参数e)
{
qualificationControl=usrqualificationControl.LoadControl(“~/control/qualificationControl.ascx”);
如果(!IsPostBack)
{
ArrayList CtrlList=新的ArrayList();
qualificationControl.ID=CtrlID.ToString();
UserCtrlHolder.Controls.Add(限定控件);
CtrlList.Add(资格控制);
添加(“质量控制”,CtrlList);
添加(“ControlHolder”,UserCtrlHolder);
}
}
受保护的void addQualificationBtn\u单击(对象发送者,事件参数e)
{
RememeberOldValues();
if(会话[“QualiControl”]!=null)
{
CtrlList=(ArrayList)会话[“QualitControl”];
}
qualificationControl.ID=CtrlList.Count.ToString();
CtrlList.Add(资格控制);
对于(int i=0;i
请指导我如何检索这些值。您必须从用户控件中找到特定控件
((DropDownList)qualificationControl1.FindControl("yearList")).SelectedValue
@穆罕默德·阿克塔……我已经这样做了。通过这样做,可以找到控件,但无法检索其值。请将其放入Page_Init事件中,然后尝试qualificationControl=usrqualificationControl.LoadControl(“~/control/qualificationControl.ascx”)@Muhammad Akhtar..你想让我在page_Init()方法中放哪一部分?qualificationControl=usrqualificationControl.LoadControl(“~/control/qualificationControl.ascx”);为什么不在代码隐藏中显示/不显示用户控件,而不是在运行时加载?
((DropDownList)qualificationControl1.FindControl("yearList")).SelectedValue