Search code examples
asp.netvb.netcode-behinddatalist

Datalist name not declared when using Code Behind. aspx.vb


I'm a totally newbie to .NET programming, and trying to get some stuff off the ground.

I've run some code which extracts data from a database and presents it into a Datalist. It works fine when the script is on the aspx file, but when I transfer it to a code behind I get the following error

Compiler Error Message: BC30451: Name 'showIt' is not declared.

(showIt is the ID of the Datalist)

Obviously, the Datalist markup is in my aspx page, and the script is in the code behind .vb file.

As I say, it all works when the scripts are all on the one .aspx file.

The code in the code behind is: (I've skipped the SQL and connection strings to keep this post concise)

Partial Public Class Data
Inherits System.Web.UI.Page

Sub Page_Load(sender As Object, e As EventArgs)

    Dim objDataReader As OdbcDataReader
    objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

    showIt.DataSource = objDataReader
    showIt.DataBind()
    objDataReader.Close()
end sub
end class

The aspx file looks like this: (again simplified, basic HTML markup etc removed)

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="dataOut.aspx.vb" Inherits="_Default"%>
<form id="form1" runat="server">

<asp:DataList ID="showIt" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">  
<ItemTemplate>    
 <div style="width:300px; display:inline-block; height: 200px; overflow:hidden">
        <div style="width:100%; background:#880000; border-bottom: solid 1px black"><%# DataBinder.Eval(Container.DataItem, "item") %></div>
        <br />
        <%# Data.stockDisplay(Eval("shopstock"))%>
        <div style="clear: both"><%# Left(DataBinder.Eval(Container.DataItem, "description"),150) %></div>
 </div>          
</ItemTemplate> 


Solution

  • Ah ha.... I've fixed it!

    needed to declare the DataList before the function so it became publicly available... or at least I think that was the issue, it works now anyway

    Protected WithEvents showIt As System.Web.UI.WebControls.DataList
    

    I now have other issues... that can go in another question though!