Code Behind

Dox Warrior

New Member
Good Day <BR><BR>One question I am binding a drop down list from a db column and I have created the code in the .aspx page itself ( using the tut on http://www.4guysfromrolla.com "How to bind a db column to a Drop down list in ASP.NET) eg opening the connection binding etc and it works perferfectly now I would like to put all this code into my code behind page so that my .aspx page does not become clutterd BUT when I enter the code into the page for the code behind e.g webform2.aspx.vb page and I use the tag <BR> <BR><%@ Page language="VB" Codebehind="webform2.aspx.vb" Inherits="time.webform2" %><BR><BR>The control runs but with no information from the db I suspect the aspx page is not reading the webform.aspx.vb code why not and where am I going wrong?<BR><BR>In the tut there where two sections<BR><BR> Sub Page_Load(sender as Object, e as EventArgs)<BR> BindData()<BR> End Sub<BR> <BR> AND<BR><BR> Sub BindData()<BR><BR>Now i have created these in the code behind to simulate the code in the page . I had it working perfectly when the code was in the webform2.aspx page but now how can i use it in the code behind page that way the .aspx page is "cleaner" .<BR><BR><BR><BR>Thank you<BR><BR>Carlos<BR><BR>Any reposnses can also ( prefebly be mailed to [email protected])<BR>Here is my code behind code ( I have modified it to as much as possiable)<BR><BR>Imports System<BR>Imports System.Data<BR>Imports System.Data.SqlClient<BR>Public Class WebForm1<BR><BR> Inherits System.Web.UI.Page<BR> Protected WithEvents DDLP As System.Web.UI.WebControls.ListBox<BR> Dim objDR2 As SqlClient.SqlDataReader<BR> Dim myCommand2 As New SqlClient.SqlCommand(strSQL2, myConnection2)<BR> Dim myConnection2 As New SqlClient.SqlConnection(ConfigurationSettings.AppS ettings("SCon.ConnectionString"))<BR> Const strSQL2 As String = "SELECT *" & _<BR> "From Phase ORDER BY Phase "<BR><BR><BR>#Region " Web Form Designer Generated Code "<BR><BR> 'This call is required by the Web Form Designer.<BR> <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()<BR><BR> End Sub<BR><BR> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init<BR> 'CODEGEN: This method call is required by the Web Form Designer<BR> 'Do not modify it using the code editor.<BR> InitializeComponent()<BR> End Sub<BR><BR>#End Region<BR><BR> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<BR> 'Put user code to initialize the page here<BR> '1. Create a second connection<BR> Dim myConnection2 As New SqlClient.SqlConnection(ConfigurationSettings.AppS ettings("SCon.ConnectionString"))<BR><BR> '2. Create the command object, passing in the SQL string<BR> Const strSQL2 As String = "SELECT *" & _<BR> "From Phase ORDER BY Phase "<BR> Dim myCommand2 As New SqlClient.SqlCommand(strSQL2, myConnection2)<BR><BR> '3. Create the DataReader<BR> myConnection2.Open()<BR><BR> Dim objDR As SqlDataReader<BR> objDR = myCommand2.ExecuteReader(CommandBehavior.CloseConn ection)<BR><BR> End Sub<BR><BR><BR><BR><BR> Private Sub DDLP_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDLP.DataBinding<BR><BR> 'objDR2 = myCommand2.ExecuteReader(CommandBehavior.CloseConn ection)<BR> ' Binding the data to the control<BR> DDLP.DataSource = objDR2<BR> DDLP.DataBind()<BR> End Sub<BR><BR><BR><BR><BR>End Class<BR>
 
Back
Top