Solved

Custom web control parameters.

Posted on 2011-03-15
8
1,032 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 250 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 250 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
c# ftp code 3 56
Building an object from several other objects 4 39
Timeouts during development 3 29
modify font on ninja form 1 27
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

751 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