如何解决asp.net webform 找不到用户控件
我不知道如何获取用户控件的函数 GetValue(),我总是收到错误代码 CS1061。 首先,我无法获得用户控件 ID testControl。 我找到了很多例子,但几乎例子可以像我的代码一样使用。 我不知道这有什么问题。
这是我所有的代码。
用户控制部分
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="testControl.ascx.cs" Inherits="MyWeb.UserControl.testControl" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<style type="text/css">
.center {
text-align: center;
}
</style>
<asp:UpdatePanel ID="upUserControl" runat="server">
<ContentTemplate>
<table>
<thead>
<tr>
<td colspan="4">
<div class="center">
<asp:Label ID="lblTitle" runat="server" Text="查詢"></asp:Label>
</div>
</td>
</tr>
</thead>
<tbody>
<tr>
<td><asp:Label ID="lblName" runat="server" Text="姓名"></asp:Label></td>
<td><asp:TextBox ID="txtName" MaxLength="10" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblIdentify" runat="server" Text="身分證字號/統編:"></asp:Label></td>
<td><asp:TextBox ID="txtIdentify" MaxLength="10" runat="server"></asp:TextBox></td>
<td><asp:Button ID="btnQuery" runat="server" Text="查詢" OnClick="btnQuery_Click"/></td>
</tr>
</tbody>
</table>
</ContentTemplate>
</asp:UpdatePanel>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyWeb.UserControl
{
public partial class testControl : System.Web.UI.UserControl
{
public string Name,Identify;
public string GetValue()
{
return txtName.Text + txtIdentify.Text;
}
protected void Page_Load(object sender,EventArgs e)
{ }
protected void btnQuery_Click(object sender,EventArgs e)
{ }
}
}
asp 页面
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="testDynamicSelect.aspx.cs" Inherits="MyWeb.testDynamicSelect" %>
<%@ Register Src="~/UserControl/testControl.ascx" TagPrefix="ucTest" TagName="ctrl" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<ucTest:ctrl runat="server" ID="testControl" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyWeb
{
public partial class testDynamicSelect : Page
{
protected void Page_Load(object sender,EventArgs e)
{ }
protected void Button1_Click(object sender,EventArgs e)
{
//I can't get testControl
Response.Write(testControl.GetValue());
}
}
}
解决方法
我尝试引用命名空间 MyWeb.UserControl,它现在可以工作了。 在我公司的旧项目中,它不会像我的问题一样引用。 但是在我的新项目中不能工作。它必须引用命名空间 MyWeb.UserControl。 谁能告诉我它是如何工作的?
using MyWeb.UserControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyWeb
{
public partial class testDynamicSelect : Page
{
testControl ucl = new testControl();
//UserControl ucl = new UserControl;
protected void Page_Load(object sender,EventArgs e)
{
}
protected void Button1_Click(object sender,EventArgs e)
{
Console.WriteLine(cul.GetValue());
//Button1
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。