Control not getting value

J

Jon

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();
}
}
}
}
 
J

Jon

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

Chad

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

IE:
set
{
EnsureChildControls();
connectionString = value;
}
 
C

Chad

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top