2014-01-18 45 views
0

我建立我的网站的每件事情都很好,但是当我运行我的网站的网格视图不会出现,请任何人都可以帮助我如何解决问题,让网格视图显示在页面网格视图不显示在asp.net网页

这里是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 
+0

尝试调试,看看你是否得到了DT或没有任何数据 –

回答

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