Datagrid in codebehind... how?

GregD

Newcomer
Joined
Jun 26, 2004
Messages
15
Hi,

Ive tried to put my datagrid in a usercontrol/codebehind file but it just doesnt seem to work.

It worked fine in a standard aspx file but putting it into a codebehind seems more complicated.

Any good tutorials in VB on this?

This is where im at, there are no errors on the code but the Datagrid doesnt even show up.

I know that the aspx and ascx pages are linked properlly because if i put in some errors on purpose ASP.NET reports them.

secdesc.ascx:
PHP:
<%@ Control Language="VB" SRC="bll/secdesc.vb"%>
<asp:datagrid ShowHeader="False" GridLines="None" id="index_sections" runat="server" EnableViewState="true" AutoGenerateColumns="false"> 
                              <columns>
                              <asp:templatecolumn>
                                <itemTemplate> 
                                  <table width="100%" border="0" cellspacing="0" cellpadding="3">
                                    <tr valign="top"> 
                                      <td width="50" align="top">
									 
									  <img src="<%# container.dataitem( "image") %>" align="top"></td>
                                      <td ><a href="<%# container.dataitem( "titleLink")%>">
                                        <%# container.dataitem( "title")%>
                                        </a> <br> 
                                        <%# container.dataitem( "article") %>
                                      </td>
                                    </tr>
                                  </table>
                                </itemtemplate>
                              </asp:templatecolumn>
        </columns>
</asp:datagrid>

codebehind secdesc.vb:

PHP:
Imports System
Imports System.Configuration
Imports System.DateTime
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.DBNull
Imports System.Web
Imports System.Web.Security
Imports System.Web.SessionState
Imports System.Web.HttpServerUtility
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.Datagrid
Imports System.Web.UI.HtmlControls

namespace CMS
Public Class ModSectionDesc
Inherits System.Web.UI.UserControl

'======================
'Web controls
'======================
    
    Protected WithEvents index_sections As System.Web.UI.WebControls.Datagrid

'======================
'Propertys (set/get)
'======================

	
	

'======================
'Subroutines
'======================
Public Sub Page_Load(ByVal sender as System.Object,ByVal e as System.EventArgs)
	BindData()
End Sub

Public Sub BindData()
	 dim isSecure as String 
'checks if the site uses http:// or https:// so we can build up our where condition in the SQL Stored Procedure
If Request.ServerVariables("HTTPS") = "off" Then
isSecure = "http://"
Else
isSecure = "https://"
End if

'SQL Stored Proc to get the modules for this appropriate site/page.

Dim conn as New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("connectionString"))
'Create new command object and query the database. Open the connection.
	Dim myCommand As SqlCommand = New SqlCommand("sp_getmoddesc", conn)
	
myCommand.CommandType = CommandType.StoredProcedure
conn.open()

'Set up parameter for stored procedure 
Dim prmRootPath As New SqlParameter()
Dim prmPagePath As New SqlParameter()


'parameters	
prmRootPath.ParameterName = "@rootpath"
prmPagePath.ParameterName = "@pagepath"

prmRootPath.Value = isSecure & Request.ServerVariables("HTTP_HOST")
prmPagePath.Value = Request.ServerVariables("PATH_INFO")

'add the parameter to the command
myCommand.Parameters.Add(prmRootPath)
myCommand.Parameters.Add(prmPagePath)
index_sections.DataSource = myCommand.ExecuteReader()
index_sections.DataBind
conn.close()
End Sub

End Class


End namespace

Any help would be great thanks!
 
Thanks but I have already read this tutorial series parts 1-16 and there is absolutely nothing concerning implementing the datagrid into your own ASCX file with a codebehind.
 
Have you placed a populated label or something in the usercontrol to see if the usercontrol is being recognized correctly by the main aspx web form? I'm just guessing that the default web form does not have the correct link to the usercontrol.
 
Hi guys thanks for your recomendations.. I managed to fix it.

Turns out I needed to compile all my VB files and reference it in the user control as:

<%@ Control Language="VB" inherits="CMS.ModSectionDesc"%>

Apparently it didnt work the other way (just referencing the file) because it was trying to call page_load when there already was one in my other VB file CMS.Login

So i had compiled CMS.Login but i was pointing directly at the file for CMS.ModSectionDesc and thus .NET seemed to get confused and used the page_load from Logins rather than the other one.
 
Back
Top