• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1040
  • Last Modified:

Custom web control parameters.

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
2Cs
Asked:
2Cs
2 Solutions
 
GewgalaCommented:
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
 
GewgalaCommented:
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
 
GewgalaCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
GewgalaCommented:
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
 
2CsAuthor Commented:
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
 
Nash2334Commented:
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
 
tpaynCommented:
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
 
2CsAuthor Commented:
Many thanks for the answer,  Looks like i will have to go about this a diferent way.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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