G
Guest
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;
}
}
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;
}
}