System.Web.HttpException: 类型“TextBox”的控件“ctl02_TextBox2”必须放在具有 runat=server 的窗体标记内。
目的:加入用户自定义控件
错误提示:System.Web.HttpException: 类型“TextBox”的控件“ctl02_TextBox2”必须放在具有 runat=server 的窗体标记内。
沒有ctl02_TextBox2这个控件啊?
WebUserControl.ascx 代码
<%@ Control Language= "C# " AutoEventWireup= "true " CodeFile= "WebUserControl.ascx.cs " Inherits= "WebUserControl " %>
<asp:TextBox ID= "TextBox2 " Text= "tb2 " runat= "server "> </asp:TextBox>
<asp:Button ID= "Button1 " runat= "server " Text= "Button " />
<asp:TextBox ID= "TextBox1 " Text= "tb1 " runat= "server "> </asp:TextBox>
<asp:Button ID= "Button2 " runat= "server " Text= "Button " />
WebUserControl.ascx.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "text1 ";
TextBox2.Text = "text2 ";
}
}
.aspx代码:
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "User_Control.aspx.cs " Inherits= "User_Control " %>
<%@ Reference Control= "~/WebUserControl.ascx "%>
<!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>
</div>
</form>
</body>
</html>
.aspx.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class User_Control : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserControl uc =(UserControl)LoadControl( "WebUserControl.ascx ");