网格视图未在asp.net网页中显示

时间:2014-01-18 10:08:22

标签: html asp.net css vb.net web

我正在构建我的网站,每件事情都很顺利,但是当我运行我的网站时,网格视图没有出现,请任何人帮助我如何解决问题并让网格视图出现在页面上< / p>

这是aspx代码:

<%@ Page Title="" Language="VB" MasterPageFile="~/Member.master" AutoEventWireup="false" CodeFile="Order.aspx.vb" Inherits="Order" %>

<asp:Content ID="Content1"     ContentPlaceHolderID="HeadContent" Runat="Server">


     <script type="text/javascript">
         var oldgridcolor;
         function SetMouseOver(element) {
             oldgridcolor = element.style.backgroundColor;
             element.style.backgroundColor = '#ffeb95';
             element.style.cursor = 'pointer';
             element.style.textDecoration = 'underline';
         }
         function SetMouseOut(element) {
             element.style.backgroundColor = oldgridcolor;
             element.style.textDecoration = 'none';
         }
</script>


</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">


  <div style="margin-right: 0cm; margin-left: 4.1cm; height: 679px; background-color: #CC0000; width: 1038px; margin-bottom: 0px; margin-top: 109px;">

  <fieldset style="width:230px;">
            <legend style="font-family: 'Times New Roman', Times, serif; font-size: xx-large; color: #FFFF00; font-weight: normal; font-style: normal">Menu<asp:SqlDataSource 
                    ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:conStr %>" 
                    SelectCommand="SELECT * FROM [Menu]">
                </asp:SqlDataSource>
                <br />
            </legend>
            <asp:GridView ID="MenuGrid" runat="server" AutoGenerateColumns="false" 
                BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" 
                CellPadding="4" DataKeyNames="MealNo" DataSourceID="SqlDataSource1" 
                Width="990px"   GridLines="None" OnRowDataBound="EmpGridView_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="MealNo" HeaderText="MealNo"  />
                    <asp:BoundField DataField="MealName" HeaderText="MealName"  />
                    <asp:BoundField DataField="Desp" HeaderText="Desp" />
                    <asp:BoundField DataField="Price" HeaderText="Price"  />
                    <asp:BoundField DataField="MealCateID" HeaderText="MealCateID"/>
                </Columns>
        <EditRowStyle BackColor="#2461BF" />
         <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
         <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
         <RowStyle BackColor="#EFF3FB" />
         <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
         <SortedAscendingCellStyle BackColor="#F5F7FB" />
         <SortedAscendingHeaderStyle BackColor="#6D95E1" />
         <SortedDescendingCellStyle BackColor="#E9EBEF" />
         <SortedDescendingHeaderStyle BackColor="#4870BE" />
            </asp:GridView>
            </fieldset>
            </div>        
</asp:Content>

vb代码:

    Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Partial Class Order
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            BindEmpGrid()
        End If
    End Sub
    Private Sub BindEmpGrid()
        Dim dt As New DataTable()
        Try
            Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("conStr").ConnectionString)
            Dim adp As New SqlDataAdapter("Select * from Menu", con)
            adp.Fill(dt)

            If dt.Rows.Count > 0 Then
                MenuGrid.DataSource = dt
                MenuGrid.DataBind()
            Else
                MenuGrid.DataSource = Nothing
                MenuGrid.DataBind()
            End If
        Catch ex As Exception
            Response.Write("Error Occured: " & ex.ToString())
        Finally
            dt.Clear()
            dt.Dispose()
        End Try
    End Sub

    Protected Sub EmpGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles MenuGrid.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes("onmouseover") = "javascript:SetMouseOver(this)"
            e.Row.Attributes("onmouseout") = "javascript:SetMouseOut(this)"
        End If
    End Sub

End Class

1 个答案:

答案 0 :(得分:1)

您无法在任何地方打开连接。打开它或更好地换行Using ... End Using,试试这样:

Using con As New sqlConnection(ConfigurationManager.ConnectionStrings("conStr").ConnectionString)
            Dim adp As New SqlDataAdapter("Select * from Menu", con)
            adp.Fill(dt)
End Using