Solved

Exposing properties of an ascx control so they can be set in the aspx element tag?

Posted on 2009-06-30
2
539 Views
Last Modified: 2012-05-07
I created a control ascx file. In its code behind file I added a public string called my_prop. I had hoped that when I declared an element of the type defined in my ascx file that I could set my_prop from the aspx tag. This doesn't seem to be the case, are there additional steps that need to be taken?
0
Comment
Question by:Matt_Du
2 Comments
 
LVL 15

Expert Comment

by:jinal
ID: 24751004
so you have .ascx like

public partial class Myascx : UserControl
{
    public string my_prop = string.empty;
}

No in aspx:

<uc1:Control ID="MyAscx1" my_prop="test" />

if you want like this then In

public partial class Myascx : UserControl
 {
     public string my_prop = string.empty;

    public string MYProp
    {
         get
         {
              return my_prop;
         }
         set
          {
                my_prop=  value;
          }
    }
 }

Declaration should be like
<uc1:Control ID="MyAscx1" MYProp ="test" />.

Overall idea is to create property.
0
 
LVL 9

Accepted Solution

by:
tetorvik earned 500 total points
ID: 24751442
You should be able to use public variables in control declaration in aspx page like :
    <uc1:WebUserControl1 ID="myId" runat="server" my_prop="Print this text" />
 then on ascx.cs you can try to use that for example:

        public string my_prop;
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(my_prop);
        }

works perfectly, however VS intellisense doesn't recognize my_prop when writing control in aspx.

hope this helps.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticallā€¦
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Siā€¦

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question