Control not getting value

Discussion in 'ASP .Net Building Controls' started by Jon, Feb 8, 2005.

  1. Jon

    Jon Guest

    Hello all,

    I've written the below server control which only half works! When I complie
    and include the server control into a project I can get connectionString but
    not storedProcedure, it always errors saying it's null, yet the code (seems)
    is the same. Can anyone shed any light on this? Thanks:

    [DefaultProperty("Text"),
    ToolboxData("<{0}:TH_NKServerControl
    runat=server></{0}:TH_NKServerControl>")]
    public class TH_NKServerControl : System.Web.UI.WebControls.WebControl
    {
    private string storedProcedure;
    private string connectionString;

    DataGrid dgrdQuestions;
    DataSet dsQuestions;

    protected override void CreateChildControls()
    {
    Controls.Clear();
    dgrdQuestions = new DataGrid();

    dsQuestions = new DataSet();
    dsQuestions = SqlHelper.ExecuteDataset(connectionString, storedProcedure);

    dgrdQuestions.DataSource = dsQuestions;
    dgrdQuestions.DataBind();

    Controls.Add(dgrdQuestions);
    }

    [Bindable(true), Category("Data"), DefaultValue("")]
    public string StoredProcedure
    {
    get
    {
    EnsureChildControls();
    return storedProcedure;
    }

    set
    {
    storedProcedure = value;
    EnsureChildControls();
    }
    }

    [Bindable(true), Category("Data"), DefaultValue("")]
    public string ConnectionString
    {
    get
    {
    EnsureChildControls();
    return connectionString;
    }

    set
    {
    connectionString = value;
    EnsureChildControls();
    }
    }
    }
    }
     
    Jon, Feb 8, 2005
    #1
    1. Advertising

  2. Jon

    Jon Guest

    It seems that the code may not be the error but the usercontrol in the aspx
    page. Whichever property comes second seems to cause the error, in tis case
    connectionstring. Why's that?

    <cc1:servercontrol id="TH_NKServerControl2" runat="server"
    storedprocedure="sp_SelectAllFAQs" connectionstring="Persist Security
    Info=False;Data Source=marge;Initial Catalog=Test;User
    ID=webUser;Password=SQLS3rv3r"></cc1:servercontrol>

    "Jon" wrote:

    > Hello all,
    >
    > I've written the below server control which only half works! When I complie
    > and include the server control into a project I can get connectionString but
    > not storedProcedure, it always errors saying it's null, yet the code (seems)
    > is the same. Can anyone shed any light on this? Thanks:
    >
    > [DefaultProperty("Text"),
    > ToolboxData("<{0}:TH_NKServerControl
    > runat=server></{0}:TH_NKServerControl>")]
    > public class TH_NKServerControl : System.Web.UI.WebControls.WebControl
    > {
    > private string storedProcedure;
    > private string connectionString;
    >
    > DataGrid dgrdQuestions;
    > DataSet dsQuestions;
    >
    > protected override void CreateChildControls()
    > {
    > Controls.Clear();
    > dgrdQuestions = new DataGrid();
    >
    > dsQuestions = new DataSet();
    > dsQuestions = SqlHelper.ExecuteDataset(connectionString, storedProcedure);
    >
    > dgrdQuestions.DataSource = dsQuestions;
    > dgrdQuestions.DataBind();
    >
    > Controls.Add(dgrdQuestions);
    > }
    >
    > [Bindable(true), Category("Data"), DefaultValue("")]
    > public string StoredProcedure
    > {
    > get
    > {
    > EnsureChildControls();
    > return storedProcedure;
    > }
    >
    > set
    > {
    > storedProcedure = value;
    > EnsureChildControls();
    > }
    > }
    >
    > [Bindable(true), Category("Data"), DefaultValue("")]
    > public string ConnectionString
    > {
    > get
    > {
    > EnsureChildControls();
    > return connectionString;
    > }
    >
    > set
    > {
    > connectionString = value;
    > EnsureChildControls();
    > }
    > }
    > }
    > }
     
    Jon, Feb 8, 2005
    #2
    1. Advertising

  3. Jon

    Chad Guest

    Try putting EnsureChildControls(); as the 1st line in all you're
    property setter code.

    IE:
    set
    {
    EnsureChildControls();
    connectionString = value;
    }
     
    Chad, Feb 8, 2005
    #3
  4. Jon

    Jon Guest

    Nope Chad, same error afraid.

    Jon

    "Chad" wrote:

    > Try putting EnsureChildControls(); as the 1st line in all you're
    > property setter code.
    >
    > IE:
    > set
    > {
    > EnsureChildControls();
    > connectionString = value;
    > }
    >
    >
     
    Jon, Feb 8, 2005
    #4
  5. Jon

    Chad Guest

    Also make sure you are Implementing INamingContainer I did this it
    solved some problems for me. Also make sure EnsureChildControls is
    everywhere in all subs/functions ... except for CreateChildControls
    like you already had it.

    You're code snippet did show where you were Overriding the base Render
    code. Make sure to do that and make sure to put EnsureChildControls
    before calling base.Render(output).

    This is what I've done and I have 2 string property's and enum property
    that all work great.

    You got any thoughts on my problem. I'm also trying to add a property
    of string array type. In the IDE I can fill in the property using the
    property window. When I run it though I lose the property it behaves
    just like what you are seeing the property is null. I'm also trying to
    have a property that points to another control but I'm losing that at
    run-time too. My guess is that a string array and a control are
    non-native data types so there might be a trick to maintain the
    settings between design time and run time.

    Hope it works for you and hope you might a suggestion for me.... Chad


    Jon wrote:
    > Nope Chad, same error afraid.
    >
    > Jon
    >
    > "Chad" wrote:
    >
    > > Try putting EnsureChildControls(); as the 1st line in all you're
    > > property setter code.
    > >
    > > IE:
    > > set
    > > {
    > > EnsureChildControls();
    > > connectionString = value;
    > > }
    > >
    > >
     
    Chad, Feb 9, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?YmFzdWxhc3o=?=

    Getting value from a control in header templete?

    =?Utf-8?B?YmFzdWxhc3o=?=, Jul 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    378
    Scott Allen
    Jul 29, 2005
  2. Nathan Sokalski
    Replies:
    5
    Views:
    1,495
    Nathan Sokalski
    Oct 3, 2005
  3. Rock

    Getting values from control to control to control

    Rock, Oct 19, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    133
  4. Tim Fröglich

    Getting ID, calling url, search for value, return value

    Tim Fröglich, Jan 7, 2006, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    154
    Josh Twist
    Jan 10, 2006
  5. Dwizz
    Replies:
    10
    Views:
    412
Loading...

Share This Page