如何在ASP.NET/C#的控件内创建公共类型? [英] How to create a public type within a control in ASP.NET/C#?
问题描述
大家好.我正在使用ASP.NET和C#创建网页.
我在.ascx和.aspx容器中都有一个texteditor控件.
在控件中,我需要几个公共字符串供.aspx页读取,但是我无法创建它们,.aspx不会向我显示控件的字符串.
如何创建它们?
Hi everyone. I''m creating a webpage with ASP.NET and C#.
I have a texteditor control inside a .ascx, and the .aspx container.
In the control, I need a couple public strings for be readed by the .aspx page, but I can''t create them, the .aspx don''t show me the strings of the control.
How I can create them?
推荐答案
我只是尝试了您在上面描述的内容而已,并且有效.我制作了一个带有公共字符串的用户控件.
Hi,
I just tried what you are describing above and it works. I made a user control that has a public string.
using System;
namespace WebApplication1
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public string myString = "myString"; // <-- there it is..
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
将控件包含在.aspx页中.
Included the control in an .aspx page.
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!-- 1. -->
<%@ Register TagPrefix="test" TagName="MyControl" Src="~/WebUserControl1.ascx" %>
<asp:content id="HeaderContent" runat="server" contentplaceholderid="HeadContent" xmlns:asp="#unknown">
</asp:content>
<asp:content id="BodyContent" runat="server" contentplaceholderid="MainContent" xmlns:asp="#unknown">
<!-- 2. -->
<test:mycontrol id="MyControl" runat="server"></test:mycontrol>
<asp:label id="Label1" runat="server" text="Label"></asp:label>
</asp:content>
并在上方设置标签控件以显示公共字符串中的内容.
And set the label control above to show what is in the public string.
using System;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = MyControl.myString;
}
}
}
可以按预期工作..还是我把您的问题弄错了?
干杯!
Works as expected.. or did I get your question wrong?
Cheers!
这篇关于如何在ASP.NET/C#的控件内创建公共类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!