Solved

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

Posted on 2009-06-30
2
541 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

679 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