Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Custom web control parameters.

Posted on 2011-03-15
8
Medium Priority
?
1,034 Views
Last Modified: 2012-05-11
Hello All,

I have an issue with an asp.net custom control i have created and after much googling i am still no further. I have created a custom class which inherits from composite controll and have added a public property called QuestionnaireId which i am populating when i declare the control within markup, however when i attempt to use the value in the constructor it is not populated. Is there something i am missing or is it just not possible to do it in this way? Code is below
public class QuestionnaireControl : CompositeControl
{
        
    private int questionnaireId;
        
    public int QuestionnaireId
    {
        get { return questionnaireId; }
        set { questionnaireId = value; }
   }

    public QuestionnaireControl()
    {
        int i = questionnaireId;// allways comes out at zero
    }
}

//declared in markup as follows
<CNControls:QuestionnaireControl ID="qQues" QuestionnaireId="1" runat="server" />

Open in new window

0
Comment
Question by:2Cs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 7

Expert Comment

by:Gewgala
ID: 35143048
Since this is ASP.NET, are you attempting to access that value between page post-backs?  It sounds like your value is not persisting between post-backs.
0
 
LVL 7

Expert Comment

by:Gewgala
ID: 35143073
Here is a website that talks about getting data to persist between postbacks with composite controls:
http://forums.asp.net/t/914574.aspx/1

Here is an MSDN tutorial of composite controls.
http://msdn.microsoft.com/en-us/library/aa479016.aspx
0
 
LVL 7

Expert Comment

by:Gewgala
ID: 35143104
I believe specifically something you'll want to try is the following (this will persist your data to the ViewState):

public int QuestionnaireId
{
    get
    {
        object o = ViewState["QuestionnaireId"];
        return (o == null)? String.Empty : (string)o;
    }
    set
    {
        ViewState["QuestionnaireId"] = value;
    }
}
0
Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

 
LVL 7

Expert Comment

by:Gewgala
ID: 35143344
Sorry I forgot to fix part of the above example for your scenario, this is what you'll want:

public int QuestionnaireId
{
    get
    {
        object o = ViewState["QuestionnaireId"];
        return (o == null)? 0 : int.Parse(o.ToString());
    }
    set
    {
        ViewState["QuestionnaireId"] = value;
    }
}
0
 
LVL 5

Author Comment

by:2Cs
ID: 35145847
Hello,

Thanks for the quick response, the issue i am having however is on the first page load. The value does not seem to be there when the control is instantiated for the first time.
0
 
LVL 10

Assisted Solution

by:Nash2334
Nash2334 earned 1000 total points
ID: 35148494
You are going to get hosed by the order of events here.  If you want to set a property on a user control, you should do so programmatically in the Page_Load, then add the control to a containing ASP.Net Panel:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                QuestionnaireControl control = new QuestionnaireControl();
                control.QuestionnaireId = 1234;
                MyContainingPanel.Controls.Add(control);
            }            
        }
0
 
LVL 3

Accepted Solution

by:
tpayn earned 1000 total points
ID: 35305839
The quick answer to your original question is that the value has not been set when you try to use it in the constructor and it never will be unless passed into the constructor. The constructor is a method that gets executed when your object is created.

For a quick review of constructors refer to:

http://www.c-sharpcorner.com/UploadFile/neerajsaluja/ConstructorsInCSharp11152005233222PM/ConstructorsInCSharp.aspx
0
 
LVL 5

Author Closing Comment

by:2Cs
ID: 35324979
Many thanks for the answer,  Looks like i will have to go about this a diferent way.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

722 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