Web Custom Control Part 2

S

Steve R

How come my drop down won't bind?

** ddlbTest.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace smc.Vehicles
{
/// <summary>
/// Summary description for ddlbTest.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:ddlbTest runat=server></
{0}:ddlbTest>")]
public class ddlbTest :
System.Web.UI.WebControls.WebControl
{
SqlConnection _connection;

// child controls
DropDownList ddlbProduct = new
DropDownList();

[Bindable(false),
Category("Data"),
DefaultValue("")]
public SqlConnection Connection
{
get
{
return _connection;
}

set
{
_connection = value;
}
}


/// <summary>
/// Render this control to the output
parameter specified.
/// </summary>
/// <param name="output"> The HTML writer
to write out to </param>
protected override void Render
(HtmlTextWriter output)
{
this.Controls.Add(ddlbProduct);
this.RenderChildren(output);
}

public override void DataBind()
{
this.EnsureChildControls();

if (_connection.State !=
ConnectionState.Open)
{
_connection.Open();
}

DataSet theDataSet = new DataSet
();

SqlDataAdapter theSqlAdapter =
new SqlDataAdapter("usp_LIST_ProductLine", _connection);


theSqlAdapter.SelectCommand.CommandTimeout = 180;

theSqlAdapter.Fill(theDataSet);

ddlbProduct.DataSource =
theDataSet.Tables[0];
ddlbProduct.DataValueField
= "product_line";
ddlbProduct.DataTextField
= "product_line_name";
ddlbProduct.DataBind();

_connection.Close();
}
}
}

** xTest2.aspx (HTML)

<%@ Register TagPrefix="cc2" Namespace="smc.Vehicles"
Assembly="smc" %>
<%@ Register TagPrefix="cc1" Namespace="Common.Controls"
Assembly="Common" %>
<%@ Page language="c#" Codebehind="xTest2.aspx.cs"
AutoEventWireup="false" Inherits="smc.Vehicles.xTest2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN" >
<HTML>
<HEAD>
<title>xTest2</title>
<meta name="GENERATOR" Content="Microsoft
Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript"
content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="xTest2" method="post"
runat="server">
<cc2:ddlbTest id="MyControl2"
runat="server" />
<cc2:VehicleSearch id="MyControl"
runat="server" />
</form>
</body>
</HTML>


** xTest2.aspx.cs

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

namespace smc.Vehicles
{
/// <summary>
/// Summary description for xTest2.
/// </summary>
public class xTest2 : System.Web.UI.Page
{
protected smc.Vehicles.ddlbTest
MyControl2 = new ddlbTest();
protected smc.Vehicles.VehicleSearch
MyControl = new VehicleSearch();

private void Page_Load(object sender,
System.EventArgs e)
{
// Put user code to initialize
the page here

if (!Page.IsPostBack)
{
SqlConnection
theConnection = new SqlConnection("server=ntsm-master-
db;uid=pmcs_webui;pwd=webui;database=PMCS");

theConnection.Open();
MyControl2.Connection =
theConnection;
MyControl2.DataBind();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs
e)
{
//
// CODEGEN: This call is required
by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support -
do not modify
/// the contents of this method with the
code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new
System.EventHandler(this.Page_Load);

}
#endregion
}
}
 
W

William F. Robertson, Jr.

You aren't added the control before the PreRender event. During the
prerender event all the the control's ViewState is saved.

Try changing the Control.Add to the prerender event before calling base.

protected override OnPreRender( EventArgs e )
{
EnsureChildControls();
Controls.Add( ddlbProducts )
}

And also, you should create the DropDownList in the EnsureChildControls
method that you override.


Steve R said:
How come my drop down won't bind?

** ddlbTest.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace smc.Vehicles
{
/// <summary>
/// Summary description for ddlbTest.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:ddlbTest runat=server></
{0}:ddlbTest>")]
public class ddlbTest :
System.Web.UI.WebControls.WebControl
{
SqlConnection _connection;

// child controls
DropDownList ddlbProduct = new
DropDownList();

[Bindable(false),
Category("Data"),
DefaultValue("")]
public SqlConnection Connection
{
get
{
return _connection;
}

set
{
_connection = value;
}
}


/// <summary>
/// Render this control to the output
parameter specified.
/// </summary>
/// <param name="output"> The HTML writer
to write out to </param>
protected override void Render
(HtmlTextWriter output)
{
this.Controls.Add(ddlbProduct);
this.RenderChildren(output);
}

public override void DataBind()
{
this.EnsureChildControls();

if (_connection.State !=
ConnectionState.Open)
{
_connection.Open();
}

DataSet theDataSet = new DataSet
();

SqlDataAdapter theSqlAdapter =
new SqlDataAdapter("usp_LIST_ProductLine", _connection);


theSqlAdapter.SelectCommand.CommandTimeout = 180;

theSqlAdapter.Fill(theDataSet);

ddlbProduct.DataSource =
theDataSet.Tables[0];
ddlbProduct.DataValueField
= "product_line";
ddlbProduct.DataTextField
= "product_line_name";
ddlbProduct.DataBind();

_connection.Close();
}
}
}

** xTest2.aspx (HTML)

<%@ Register TagPrefix="cc2" Namespace="smc.Vehicles"
Assembly="smc" %>
<%@ Register TagPrefix="cc1" Namespace="Common.Controls"
Assembly="Common" %>
<%@ Page language="c#" Codebehind="xTest2.aspx.cs"
AutoEventWireup="false" Inherits="smc.Vehicles.xTest2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN" >
<HTML>
<HEAD>
<title>xTest2</title>
<meta name="GENERATOR" Content="Microsoft
Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript"
content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="xTest2" method="post"
runat="server">
<cc2:ddlbTest id="MyControl2"
runat="server" />
<cc2:VehicleSearch id="MyControl"
runat="server" />
</form>
</body>
</HTML>


** xTest2.aspx.cs

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

namespace smc.Vehicles
{
/// <summary>
/// Summary description for xTest2.
/// </summary>
public class xTest2 : System.Web.UI.Page
{
protected smc.Vehicles.ddlbTest
MyControl2 = new ddlbTest();
protected smc.Vehicles.VehicleSearch
MyControl = new VehicleSearch();

private void Page_Load(object sender,
System.EventArgs e)
{
// Put user code to initialize
the page here

if (!Page.IsPostBack)
{
SqlConnection
theConnection = new SqlConnection("server=ntsm-master-
db;uid=pmcs_webui;pwd=webui;database=PMCS");

theConnection.Open();
MyControl2.Connection =
theConnection;
MyControl2.DataBind();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs
e)
{
//
// CODEGEN: This call is required
by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support -
do not modify
/// the contents of this method with the
code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new
System.EventHandler(this.Page_Load);

}
#endregion
}
}
 
W

William Robertson

Steve,

I forgot to include the base.OnPreRender( e ); line in the overridden
prerender event. This should be the last line of the overriden method.

bill
 

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

Top