How to change public properties of User control?
I have a very simple user control in my web site project开发者_StackOverflow中文版 which has public property declarations as following
public DateTime StartDate
{
get
{
return _startDate;
}
set
{
_startDate = value;
}
}
public DateTime EndDate
{
get
{
return _endDate;
}
set
{
_endDate = value;
}
}
When i drag the ascx file to one of my aspx page and when i go to code behind of aspx page i can access the controls properties through intelisense, but when i run the project through visual studio i get error "The name 'uctTest1' does not exist in the current context " any suggetions to fix the error?
This is the line where Error shows when i run the project uctTest.StartDate = DateTime.Now;
aspx page markup :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="removetest.aspx.cs" Inherits="removetest" %>
<%@ Register src="~/uctTest.ascx" tagname="testCtl" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:testCtl ID="uctTest1" runat="server" />
</div>
</form>
</body>
</html>
aspx page code behind :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
uctTest1.StartDate = DateTime.Now;
}
}
protected void btnEdit_Click(object sender, EventArgs e)
{
}
It seems that you are missing the @Register directive in the .aspx file referring to path of .ascx control.
http://msdn.microsoft.com/en-us/library/c76dd5k1(v=vs.71).aspx
I had another aspx page with different file name in the website project that another developer had copied from my aspx page which had same <%@ Page Language="C#" AutoEventWireup="true" CodeFile="aspxpagename.aspx.cs" Inherits="aspxpageClass" %>
directive in that copied page. Even though that copied paged didnt have user control strangely page I was working reported the error. Changing that page directive's codefile,inherits attributes to different values fixed the issue.
精彩评论