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"
------
<%@dotnet.itags.org. Page Language="vb" Inherits="WebForm1" src="http://pics.10026.com/?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\Add ress.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.CloseConne ction)
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,
ChristianChristian,
> 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
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
"Calvin Luttrell/ProjectThunder.com" <calvinl@.projectthunder.com> wrote in message news:<Ov6WqlUQEHA.2520@.TK2MSFTNGP11.phx.gbl>...
> 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
>
>
>
>
> --
No comments:
Post a Comment