C# 使用asp.net c中的findcontrol在主页上获取用户控件中使用的ddl、txtbox和日历的值?

C# 使用asp.net c中的findcontrol在主页上获取用户控件中使用的ddl、txtbox和日历的值?,c#,asp.net,calendar,C#,Asp.net,Calendar,我想做一个任务,我想在web窗体上使用webusercontrol的过程中使用findControl来获取主页上webusercontrol的值,我的意思是,我已经创建了一个webusercontrol,并在web窗体上使用了它。在webusercontrol.ascx页面中,我使用文本框、日历和下拉列表。现在我想得到我从txtbox中选择的值,ddl等应该显示在主页上。我的意思是,我想在default.aspx页面上使用一个按钮,在一个变量中存储txtbox、calendar等的值,并使用Fi

我想做一个任务,我想在web窗体上使用webusercontrol的过程中使用findControl来获取主页上webusercontrol的值,我的意思是,我已经创建了一个webusercontrol,并在web窗体上使用了它。在webusercontrol.ascx页面中,我使用文本框、日历和下拉列表。现在我想得到我从txtbox中选择的值,ddl等应该显示在主页上。我的意思是,我想在default.aspx页面上使用一个按钮,在一个变量中存储txtbox、calendar等的值,并使用FindControl在主页上获取这些值。我怎样才能做到这一点?请通过代码帮助我。我是编程新手

This is the code of ascx page

 <%@ Control Language="C#" ClassName="CalendarUserControl" %>
    <asp:TextBox ID="txtData" runat="server"></asp:TextBox> <br />
    <asp:Calendar ID="Calendar1" runat="server" BackColor="Beige" >
    </asp:Calendar> 
    <br/>
    <asp:DropDownList ID="ddlthings" runat="server"> 
    <asp:ListItem> Apple</asp:ListItem>
    <asp:ListItem> Banana</asp:ListItem>
    <asp:ListItem> Mango</asp:ListItem>
    <asp:ListItem> Grapes</asp:ListItem> 
    </asp:DropDownList>
default.aspx page

<div>
    <uc1:CalendarUserControl ID="CalendarUserControl1" runat="server" />
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="btn_Click" Text="Button" />
    <br />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div> 

在用户控件中定义属性,该属性将返回所需控件的值。比如说

public partial class CalendarUserControl: UserControl
{
   // this is code-behind of your user control
   public string Data
   {
      get {return txtData.Text;}
   }

   public DateTime CalendarDate
   {
      get {return Calendar1.SelectedDate;}
   }

   // same approach for drop down list ddlthings
}
然后在aspx页面中,只需读取这些值,例如在btn_Click事件处理程序中

protected void btn_Click(object sender, EventArgs e)
{
   var textBoxValue = CalendarUserControl1.Data;
   var calendarValue = CalendarUserControl1.CalendarDate;
   //....
}
i did it, just write these lines in default.cs

protected void btn_Click(object sender, EventArgs e)
    {
        TextBox myLabel = (TextBox)CalendarUserControl1.FindControl("txtData");
        string x = myLabel.Text.ToString();
        Calendar c = (Calendar)CalendarUserControl1.FindControl("Calendar1");
        string date = c.SelectedDate.ToString();
        DropDownList b = (DropDownList)CalendarUserControl1.FindControl("ddlthings");
        string ddl = b.SelectedValue;
        Label1.Text = "Your text is: " + x + "<br />"+ " Your Selected Date is: " + date + "<br />"+ " Your Selected Item is: " + ddl;
    }