473,386 Members | 1,720 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,386 software developers and data experts.

Web Custom Control Part 2

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

Nov 17 '05 #1
3 1399
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" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
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
}
}

Nov 17 '05 #2
I'll give that a try on Monday. THANK YOU very much
William!

-----Original Message-----
You aren't added the control before the PreRender event. During theprerender 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 EnsureChildControlsmethod that you override.

Nov 17 '05 #3
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #4

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

Similar topics

1
by: Chris Newby | last post by:
I have a custom control that derives from the WebControls.Panel class. In a given ASPX document, I have an instance of my custom control with ID set to "MyControl". Then I put a...
1
by: ThunderMusic | last post by:
Hi, I'm making a Custom Web Control and I'm wondering how I can insert Javascript code into the head part of the document from within my web control... I lloked at the methods of the...
3
by: weboweb | last post by:
Hi, I'm new in web parts, I would like to understand a couple of things: Let's say I have a custom control that displays information. That info is fed to the control through an XML string, so...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
3
by: =?Utf-8?B?TWFyYyBXb29sZnNvbg==?= | last post by:
Hi, I think this is a relatively simple problem, but it's starting to annoy me quite a bit. I have two issues with a custom Web Part that I have created, both related to property editing at run...
6
by: forest demon | last post by:
i have a custom control that gets loaded at runtime. i need to be able to access a property of a control thats part of the main form, through the clcik event of the custom control. i may be...
0
by: shane.niebergall | last post by:
Hi - I've been able to create my own webparts as a custom control, by compiling as a class library and then importing the dll on my asp.net page. However, I do appreciate the convenience of the...
5
by: gerry | last post by:
I am trying to create a custom container control that will only ever contain a specific type of control. At design time, when a control of a different type is added to the container I would like...
3
by: RichB | last post by:
I would like to use the AJAX ASP.NET Accordion Control. Can I create a user control for the form and include this within a custom control which inherits from a Pane of the Accordion control? I...
2
by: DaveL | last post by:
Hello, I have a Custome Control which contains TextBox Button DataGridView design is Textbox , button When a user clicks the button i resize the custom control canvasarea to expose the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.