K
kaczmar2
Hey there-
I have a newbie question regarding code behind:
Using inline code, I can run a simple page that binds data to a
datagrid. Now, I try to take the same code and use code behind, and
the page compiles fine, but no HTML is rendered to the page.
Here is the code from "WebForm1.aspx"
-------------------
<%@ Page Language="vb" Inherits="WebForm1" Src="WebForm1.aspx.vb" %>
<html>
<head>
<title>WebForm1</title>
</head>
<body>
<asp:datagrid id="datGrid" runat="server"/>
</body>
</html>
--------------------
And here is the code for "WebForm1.aspx.vb"
--------------------
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
BindData()
End Sub
Sub BindData()
'1. Create a connection
Dim AccessConn As System.Data.OleDb.OleDbConnection
Dim AccessCommand As System.Data.OleDb.OleDbCommand
Dim AccessReader As System.Data.OleDb.OleDbDataReader
Dim strConn As String
Dim datGrid As New DataGrid
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\inetpub\wwwroot\AddressBook\database\Address.mdb;"
AccessConn = New System.Data.OleDb.OleDbConnection(strConn)
'2. Create the command object, passing in the SQL string
Const strSQL As String = "SELECT ID, LastName, FirstName FROM
tblAddress ORDER BY LastName"
Dim myCommand As New System.Data.OleDb.OleDbCommand(strSQL,
AccessConn)
'Set the datagrid's datasource to the datareader and databind
AccessConn.Open()
datGrid.DataSource =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
datGrid.DataBind()
End Sub
End Class
---------------------
A couple of notes:
1) I removed the "Web Form Designer Generated Code" for simplicity
2) Why do I need to declare datGrid? None of the examples I have seen
don't declare the server controls.
Regards,
Christian
I have a newbie question regarding code behind:
Using inline code, I can run a simple page that binds data to a
datagrid. Now, I try to take the same code and use code behind, and
the page compiles fine, but no HTML is rendered to the page.
Here is the code from "WebForm1.aspx"
-------------------
<%@ Page Language="vb" Inherits="WebForm1" Src="WebForm1.aspx.vb" %>
<html>
<head>
<title>WebForm1</title>
</head>
<body>
<asp:datagrid id="datGrid" runat="server"/>
</body>
</html>
--------------------
And here is the code for "WebForm1.aspx.vb"
--------------------
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
BindData()
End Sub
Sub BindData()
'1. Create a connection
Dim AccessConn As System.Data.OleDb.OleDbConnection
Dim AccessCommand As System.Data.OleDb.OleDbCommand
Dim AccessReader As System.Data.OleDb.OleDbDataReader
Dim strConn As String
Dim datGrid As New DataGrid
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\inetpub\wwwroot\AddressBook\database\Address.mdb;"
AccessConn = New System.Data.OleDb.OleDbConnection(strConn)
'2. Create the command object, passing in the SQL string
Const strSQL As String = "SELECT ID, LastName, FirstName FROM
tblAddress ORDER BY LastName"
Dim myCommand As New System.Data.OleDb.OleDbCommand(strSQL,
AccessConn)
'Set the datagrid's datasource to the datareader and databind
AccessConn.Open()
datGrid.DataSource =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
datGrid.DataBind()
End Sub
End Class
---------------------
A couple of notes:
1) I removed the "Web Form Designer Generated Code" for simplicity
2) Why do I need to declare datGrid? None of the examples I have seen
don't declare the server controls.
Regards,
Christian