日期:2014-05-19  浏览次数:20501 次

★50分,如何获取自定控件中,包含的控件值?
在页面上使用自定义控件,该自定义控件中又包含一个DropDownList1控件,我该怎么做才能得到这个自定义控件中DropDownList1的选项值呢?

------解决方案--------------------
你这个是用户控件,不是自定义控件给

你一端最简单的代码
//WebUserControl.ascx
<%@ Control Language= "c# " AutoEventWireup= "false " Codebehind= "WebUserControl.ascx.cs " Inherits= "WebUserControl " TargetSchema= "http://schemas.microsoft.com/intellisense/ie5 "%>
<asp:TextBox id= "TextBox1 " runat= "server "> </asp:TextBox>

//WebFormDemo.aspx
<%@ Register TagPrefix= "uc1 " TagName= "WebUserControl " Src= "WebUserControl.ascx " %>
<%@ Page language= "c# " Codebehind= "WebFormDemo.aspx.cs " AutoEventWireup= "false " Inherits= "WebFormDemo " %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN " >
<HTML>
<HEAD>
<title> WebDemo </title>
<meta name= "GENERATOR " Content= "Microsoft Visual Studio .NET 7.1 ">
<meta name= "CODE_LANGUAGE " Content= "C# ">
<meta name= "vs_defaultClientScript " content= "JavaScript ">
<meta name= "vs_targetSchema " content= "http://schemas.microsoft.com/intellisense/ie5 ">
</HEAD>
<body>
<form id= "Form1 " method= "post " runat= "server ">
<asp:Button id= "Button1 " runat= "server " Text= "Button "> </asp:Button>
<uc1:WebUserControl id= "WebUserControl1 " runat= "server "> </uc1:WebUserControl>
</form>
</body>
</HTML>

//WebFormDemo.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class WebFormDemo : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected WebUserControl WebUserControl1;
private void Page_Load(object sender, System.EventArgs e)
{

}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}


private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
TextBox t = (TextBox) WebUserControl1.FindControl( "TextBox1 ");
if(t != null)
t.Text = "aaa ";
}
}


------解决方案--------------------
如果是DropDownList
可以
DropDownList d = (DropDownList)WebUserControl1.FindControl( "DropDownList1 ");
if(d != null)
d.SelectedIndex = 2;