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