473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

user control inside placeholder

Hello,
I am trying to insert multiple instances of a custom user control into a
placeholder on an aspx page, based on the records retrieved from the
database. I use the datareader to loop through the records and I want to
create a new instance of the user control for each record and then change the
properties of the controls within each user control. The problem is that
after I create the new instance of the user control, I get a protected
property error when I try to change the properties of a control within that
instance of the user control. I tried changing the class definition so that
the controls on the user control were declared as public, but that does not
seem to solve the problem. I get a Event.Arg error now. Any suggestions
would be greatly appreciated.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;

namespace MyNamespace
{
/// <summary>
/// Summary description for MyWebPage.
/// </summary>
public class MyWebPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder MyPlaceHolder;

private void Page_Load(object sender, System.EventArgs e)
{
string strSql="SELECT * FROM MyDatabaseTable";

try
{ //Connection string for MyODBC 3.51
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=MyDatabase;" +
"UID=root;" +
"PASSWORD=MyPassword;" +
"OPTION=3";
//Connect to MySQL using MyODBC
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
OdbcCommand MyCommand = new OdbcCommand(strSql,MyConnection);
OdbcDataReader MyDataReader;
MyDataReader = MyCommand.ExecuteReader();

while (MyDataReader.Read())
{
MyUserControl MyUserControl1 = new MyUserControl();

MyUserControl1.Label1.Text="Hello World"; //ERROR ON THIS LINE!!

MyPlaceHolder.Controls.Add(MyUserControl1);
}
//Close all resources
MyDataReader.Close();
MyConnection.Close();
}
catch
{
string ErrorMessage = e.ToString();
string strErrorMessage=ErrorMessage;
}
}
Nov 16 '05 #1
4 8773
you should create a property on your user control that stores the text for
the label and then override the databind method to bind the text to the
label control. This then allows you to create an instance of the controlm
assign the variable to the property and then call databind on the control
after it has been added to the place holder.

e.g.

public class myUserControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label lblName;

private string m_name;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
DataBind();
}

public override void DataBind()
{
lblName.Text = m_name;
base.DataBind();
}
--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer
helping programmers.

"patrick_a" <pa******@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
Hello,
I am trying to insert multiple instances of a custom user control into a
placeholder on an aspx page, based on the records retrieved from the
database. I use the datareader to loop through the records and I want to
create a new instance of the user control for each record and then change the properties of the controls within each user control. The problem is that
after I create the new instance of the user control, I get a protected
property error when I try to change the properties of a control within that instance of the user control. I tried changing the class definition so that the controls on the user control were declared as public, but that does not seem to solve the problem. I get a Event.Arg error now. Any suggestions
would be greatly appreciated.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;

namespace MyNamespace
{
/// <summary>
/// Summary description for MyWebPage.
/// </summary>
public class MyWebPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder MyPlaceHolder;

private void Page_Load(object sender, System.EventArgs e)
{
string strSql="SELECT * FROM MyDatabaseTable";

try
{ //Connection string for MyODBC 3.51
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=MyDatabase;" +
"UID=root;" +
"PASSWORD=MyPassword;" +
"OPTION=3";
//Connect to MySQL using MyODBC
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
OdbcCommand MyCommand = new OdbcCommand(strSql,MyConnection);
OdbcDataReader MyDataReader;
MyDataReader = MyCommand.ExecuteReader();

while (MyDataReader.Read())
{
MyUserControl MyUserControl1 = new MyUserControl();

MyUserControl1.Label1.Text="Hello World"; //ERROR ON THIS LINE!!

MyPlaceHolder.Controls.Add(MyUserControl1);
}
//Close all resources
MyDataReader.Close();
MyConnection.Close();
}
catch
{
string ErrorMessage = e.ToString();
string strErrorMessage=ErrorMessage;
}
}

Nov 16 '05 #2
Thanks that almost works! I created a property on the user control , set the
value and then add the user control to the placeholder, but when I call the
databind method I get an error: Object reference not set to an instance of
an object. The error occurs in the databind method:

public override void DataBind()
{
lblName.Text = m_name; //Error on this line
base.DataBind();
}

The code looks like this now:

MyUserControl MyUserControl1 = new MyUserControl();

MyUserControl1.Name="Hello World";

MyPlaceHolder.Controls.Add(MyUserControl1);

MyUserControl1.DataBind(); //ERROR ON THIS LINE!!


"Ollie Riches" wrote:
you should create a property on your user control that stores the text for
the label and then override the databind method to bind the text to the
label control. This then allows you to create an instance of the controlm
assign the variable to the property and then call databind on the control
after it has been added to the place holder.

e.g.

public class myUserControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label lblName;

private string m_name;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
DataBind();
}

public override void DataBind()
{
lblName.Text = m_name;
base.DataBind();
}
--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer
helping programmers.

"patrick_a" <pa******@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
Hello,
I am trying to insert multiple instances of a custom user control into a
placeholder on an aspx page, based on the records retrieved from the
database. I use the datareader to loop through the records and I want to
create a new instance of the user control for each record and then change

the
properties of the controls within each user control. The problem is that
after I create the new instance of the user control, I get a protected
property error when I try to change the properties of a control within

that
instance of the user control. I tried changing the class definition so

that
the controls on the user control were declared as public, but that does

not
seem to solve the problem. I get a Event.Arg error now. Any suggestions
would be greatly appreciated.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;

namespace MyNamespace
{
/// <summary>
/// Summary description for MyWebPage.
/// </summary>
public class MyWebPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder MyPlaceHolder;

private void Page_Load(object sender, System.EventArgs e)
{
string strSql="SELECT * FROM MyDatabaseTable";

try
{ //Connection string for MyODBC 3.51
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=MyDatabase;" +
"UID=root;" +
"PASSWORD=MyPassword;" +
"OPTION=3";
//Connect to MySQL using MyODBC
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
OdbcCommand MyCommand = new OdbcCommand(strSql,MyConnection);
OdbcDataReader MyDataReader;
MyDataReader = MyCommand.ExecuteReader();

while (MyDataReader.Read())
{
MyUserControl MyUserControl1 = new MyUserControl();

MyUserControl1.Label1.Text="Hello World"; //ERROR ON THIS LINE!!

MyPlaceHolder.Controls.Add(MyUserControl1);
}
//Close all resources
MyDataReader.Close();
MyConnection.Close();
}
catch
{
string ErrorMessage = e.ToString();
string strErrorMessage=ErrorMessage;
}
}


Nov 16 '05 #3
where are you doing this? in the page load event
--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer
helping programmers.
"patrick_a" <pa******@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
Thanks that almost works! I created a property on the user control , set the value and then add the user control to the placeholder, but when I call the databind method I get an error: Object reference not set to an instance of an object. The error occurs in the databind method:

public override void DataBind()
{
lblName.Text = m_name; //Error on this line
base.DataBind();
}

The code looks like this now:

MyUserControl MyUserControl1 = new MyUserControl();

MyUserControl1.Name="Hello World";

MyPlaceHolder.Controls.Add(MyUserControl1);

MyUserControl1.DataBind(); //ERROR ON THIS LINE!!


"Ollie Riches" wrote:
you should create a property on your user control that stores the text for the label and then override the databind method to bind the text to the
label control. This then allows you to create an instance of the controlm assign the variable to the property and then call databind on the control after it has been added to the place holder.

e.g.

public class myUserControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label lblName;

private string m_name;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
DataBind();
}

public override void DataBind()
{
lblName.Text = m_name;
base.DataBind();
}
--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer helping programmers.

"patrick_a" <pa******@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
Hello,
I am trying to insert multiple instances of a custom user control into a placeholder on an aspx page, based on the records retrieved from the
database. I use the datareader to loop through the records and I want to create a new instance of the user control for each record and then change
the
properties of the controls within each user control. The problem is
that after I create the new instance of the user control, I get a protected
property error when I try to change the properties of a control within

that
instance of the user control. I tried changing the class definition so that
the controls on the user control were declared as public, but that
does not
seem to solve the problem. I get a Event.Arg error now. Any

suggestions would be greatly appreciated.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;

namespace MyNamespace
{
/// <summary>
/// Summary description for MyWebPage.
/// </summary>
public class MyWebPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder MyPlaceHolder;

private void Page_Load(object sender, System.EventArgs e)
{
string strSql="SELECT * FROM MyDatabaseTable";

try
{ //Connection string for MyODBC 3.51
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=MyDatabase;" +
"UID=root;" +
"PASSWORD=MyPassword;" +
"OPTION=3";
//Connect to MySQL using MyODBC
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
OdbcCommand MyCommand = new OdbcCommand(strSql,MyConnection);
OdbcDataReader MyDataReader;
MyDataReader = MyCommand.ExecuteReader();

while (MyDataReader.Read())
{
MyUserControl MyUserControl1 = new MyUserControl();

MyUserControl1.Label1.Text="Hello World"; //ERROR ON THIS LINE!!

MyPlaceHolder.Controls.Add(MyUserControl1);
}
//Close all resources
MyDataReader.Close();
MyConnection.Close();
}
catch
{
string ErrorMessage = e.ToString();
string strErrorMessage=ErrorMessage;
}
}


Nov 16 '05 #4
This is what the web form and the user control look like without all the ADO:

WEB FORM:

{
public class MyWebForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder MyPlaceHolder;

private void Page_Load(object sender, System.EventArgs e)
{
MyWebUserControl MyWebUserControl1=new MyWebUserControl();
MyWebUserControl1.Name="Hello World";
MyPlaceHolder.Controls.Add(MyWebUserControl1); //Works fine through here
}

}
And the User Control looks like this:

public class MyUserControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label MyLabel;

private string m_name;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
DataBind();
}

public override void DataBind()
{
MyLabel.Text = m_name; // ERROR ON THIS LINE Object reference not set to
an instance of an object.
// on mouseover, MyLabel=<undefined value>.
// m_name has the correct value.
base.DataBind();
}

}

"Ollie Riches" wrote:
where are you doing this? in the page load event
--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer
helping programmers.
"patrick_a" <pa******@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
Thanks that almost works! I created a property on the user control , set

the
value and then add the user control to the placeholder, but when I call

the
databind method I get an error: Object reference not set to an instance

of
an object. The error occurs in the databind method:

public override void DataBind()
{
lblName.Text = m_name; //Error on this line
base.DataBind();
}

The code looks like this now:

MyUserControl MyUserControl1 = new MyUserControl();

MyUserControl1.Name="Hello World";

MyPlaceHolder.Controls.Add(MyUserControl1);

MyUserControl1.DataBind(); //ERROR ON THIS LINE!!


"Ollie Riches" wrote:
you should create a property on your user control that stores the text for the label and then override the databind method to bind the text to the
label control. This then allows you to create an instance of the controlm assign the variable to the property and then call databind on the control after it has been added to the place holder.

e.g.

public class myUserControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label lblName;

private string m_name;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
DataBind();
}

public override void DataBind()
{
lblName.Text = m_name;
base.DataBind();
}
--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer helping programmers.

"patrick_a" <pa******@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
> Hello,
> I am trying to insert multiple instances of a custom user control into a > placeholder on an aspx page, based on the records retrieved from the
> database. I use the datareader to loop through the records and I want to > create a new instance of the user control for each record and then change the
> properties of the controls within each user control. The problem is that > after I create the new instance of the user control, I get a protected
> property error when I try to change the properties of a control within
that
> instance of the user control. I tried changing the class definition so that
> the controls on the user control were declared as public, but that does not
> seem to solve the problem. I get a Event.Arg error now. Any suggestions > would be greatly appreciated.
>
> using System;
> using System.Collections;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Web;
> using System.Web.SessionState;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.HtmlControls;
> using System.Data.Odbc;
>
> namespace MyNamespace
> {
> /// <summary>
> /// Summary description for MyWebPage.
> /// </summary>
> public class MyWebPage : System.Web.UI.Page
> {
> protected System.Web.UI.WebControls.PlaceHolder MyPlaceHolder;
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> string strSql="SELECT * FROM MyDatabaseTable";
>
> try
> { //Connection string for MyODBC 3.51
> string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
> "SERVER=localhost;" +
> "DATABASE=MyDatabase;" +
> "UID=root;" +
> "PASSWORD=MyPassword;" +
> "OPTION=3";
> //Connect to MySQL using MyODBC
> OdbcConnection MyConnection = new OdbcConnection(MyConString);
> MyConnection.Open();
> OdbcCommand MyCommand = new OdbcCommand(strSql,MyConnection);
>
>
> OdbcDataReader MyDataReader;
> MyDataReader = MyCommand.ExecuteReader();
>
> while (MyDataReader.Read())
> {
> MyUserControl MyUserControl1 = new MyUserControl();
>
> MyUserControl1.Label1.Text="Hello World"; //ERROR ON THIS LINE!!
>
> MyPlaceHolder.Controls.Add(MyUserControl1);
> }
> //Close all resources
> MyDataReader.Close();
> MyConnection.Close();
> }
> catch
> {
> string ErrorMessage = e.ToString();
> string strErrorMessage=ErrorMessage;
> }
> }


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alain | last post by:
Hello, I am trying to dynamically add many instances of a user control inside a placeholder. The user control exposes an event raised when a button is pushed. How can I handle such event for...
7
by: Tom wilson | last post by:
I'm trying to create dynamic controls in ASP.Net. It's driving me nuts. I keep getting the error: Control '16' of type 'RadioButton' must be placed inside a form tag with runat=server. Dim...
4
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
2
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
0
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
3
by: John Hughes | last post by:
I'm trying to add a user control to a form via the pages render method and I get the following error : "Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server" ...
8
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the...
4
by: tshad | last post by:
Is there a way for a User Control to access an object (such as label or textbox) on the .aspx page that calls it? For example: x.aspx ************************************** .... Sub...
0
by: John Smith | last post by:
I still have not gotten this damn thing figured out and I'm asking for help one last time before I give up on it. I have a user control that contains a paged gridview control. The master page...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.