举个例子
譬如aspx文件中这样写的
<uc1:test id="test1" runat="server"></uc1:test>
在cs代码中就手工加上
protected test test1;接着就可以调用:test1.属性

解决方案 »

  1.   

    expose appropiate properties from the UserControl and use code-behind, for exampleMyDropDown.ascx:
    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="MyDropDown.ascx.cs" Inherits="WebApplication4.MyDropDownList">
    <asp:DropDownList id=DropDownList1 runat="server"></asp:DropDownList>MyDropDown.ascx.cs:namespace WebApplication4
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls; public class MyDropDownList : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.DropDownList DropDownList1; public ListItem SelectedItem
    {
    get {return DropDownList1.SelectedItem;}
    } public int SelectedIndex
    {
    get {return DropDownList1.SelectedIndex;}
    set {DropDownList1.SelectedIndex = value;}
    }
    .........
                }
    }
    webform1.aspx:
    <%@ Register TagPrefix="uc1" TagName="MyDropDown" Src="MyDropDown.ascx" %><form id=Form1 method=post runat="server">
    <uc1:MyDropDown id=MyDropDown1 runat="server">
    </form>webform1.aspx.cs:protected WebApplication4.MyDropDownList MyDropDown1;//....
    Response.Write(MyDropDown1.SelectedItem.Value);