Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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?
Posted
Comments
AspDotNetDev 16-Feb-12 19:41pm    
I am having trouble understanding your question. Will you edit it to provide some short sample code?
TANicox 16-Feb-12 19:52pm    
Sorry about that, my english isn't so good...

Let me explain in other way:

I have 1 control (.ascx) and a Page (.aspx).

I need a public string in the control, for it can be read by the page.

But when i register the control in the page, the page code (.cs) don't show me the string located in the control.

Can understand me?
Johannes Hillert 16-Feb-12 19:57pm    
Please, rebuild the project and try again.

1 solution

Hi,

I just tried what you are describing above and it works. I made a user control that has a public string.

C#
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)
        {
        }
    }
}


Included the control in an .aspx page.

ASP.NET
<%@ 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.

C#
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!
 
Share this answer
 
v4
Comments
TANicox 16-Feb-12 20:14pm    
I read the provided code and it was a little error in the call of the name of the control.
It's solved, thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900