server side control not visible in webpage

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
 
C

Calvin Luttrell/ProjectThunder.com

Christian,
1) I removed the "Web Form Designer Generated Code" for simplicity

Don't do that! JK! I would suggest trying your example with the web form designer code cause your datagrid object is initialized in there. Also make sure your query is returning rows.


-Calvin Luttrell
ProjectThunder.com
 
K

kaczmar2

Calvin-

I didn't remove the code from the actual aspx file, just in the
example here. My query is returning rows. Like I mentioned in the
previous post, everything works great if I use inline code, it's when
I modify it to code behind that the control doesn't render.

Christian
 

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