Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 552
  • Last Modified:

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

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
Matt_Du
Asked:
Matt_Du
1 Solution
 
jinalCommented:
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
 
tetorvikCommented:
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now