Solved

Custom web control parameters.

Posted on 2011-03-15
8
1,033 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to dynamically set the form action using jQuery.
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).

636 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