Solved

Custom web control parameters.

Posted on 2011-03-15
8
1,028 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
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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now