如何在ContentPlaceHolder中使用Calendar Extender?

时间:2014-03-27 10:56:50

标签: c# asp.net

我在我的项目内部使用CalenderExtender内容占位符,日历扩展程序位于UserControl。此控件在普通的aspx页面中工作,但是当我在ContentPlaceHolder中拖动此控件时,它无效。实际上,Calender中没有出现textBox是我在项目中使用的代码。

ASPX:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Calender1.ascx.cs" Inherits="Facultymanagement.Calender1" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"  TagPrefix="asp" %>
<asp:TextBox ID="txtcalender" runat="server"> 
</asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format="MM/dd/yyyy" 
TargetControlID="txtcalender" PopupButtonID="txtcalender"></asp:CalendarExtender>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
    {
        if (CalendarExtender1.SelectedDate.ToString() != "")
        {
            txtcalender.Text = CalendarExtender1.SelectedDate.ToString();
        }
    }
    public string TextBox1Value
    {

        get
        {
            return txtcalender.Text;
            // return Convert.ToString(Calendar.SelectedDate);
        }

        set { txtcalender.Text = value; }

    }       
}

这是我尝试访问该值的地方:

protected void Button1_Click1(object sender, EventArgs e)
{
    Label1.Text = calender1.TextBox1Value.ToString();
}

2 个答案:

答案 0 :(得分:1)

您需要先找到控件

    ContentPlaceHolder mpContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("ContentPlacename");
    if (mpContentPlaceHolder1 != null)
    {
        Button btn_searsh;
        btn_searsh = (Button)mpContentPlaceHolder1.FindControl("main_search");
        btn_searsh.CssClass += " " + "btn-primary";//to pass the rentpage class

    }

答案 1 :(得分:0)

我认为你必须做错事。

以下代码有效: WebUserControl1.ascx:

<%@ Control Language="C#" ClassName="WebUserControl" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<script runat="server">

</script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;
<asp:Image ID="Image1" runat="server" />
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1" PopupButtonID="Image1">
</cc1:CalendarExtender>

Default.aspx的:

<%@ Page Language="C#" %>

<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

    </div>
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
        <uc1:WebUserControl ID="WebUserControl2" runat="server" />
        <uc1:WebUserControl ID="WebUserControl3" runat="server" />
        <uc1:WebUserControl ID="WebUserControl4" runat="server" />
    </form>
</body>
</html>