我是编程新手。对不起,如果我问一个愚蠢的问题。
我必须显示表的某些列,该表位于datagrid视图中的数据源中。它已连接到数据源,如果我没有记错的话,它将充满数据。但是,当我运行该项目时,在浏览器中查看时不会显示任何内容。

多谢您的协助。先感谢您。

那可能是菜单页面代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Menu.aspx.cs" Inherits="Menu" %>

<asp:Content ID="Content1" ContentPlaceHolderID="title" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="contentbody" Runat="Server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="Left" Runat="Server">
    <asp:Label ID="Label1" runat="server" Text="Menu and Categories"></asp:Label>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="83px" ShowHeader="False" CssClass="myGrid">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server"
                    Text='<%# Eval("BookTypeName")%>'
                    OnCommand="Get_Category"
                    CommandName='<%#Eval("BookTypeID")%>'>LinkButton</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    <asp:Label ID="LableErr" runat="server" Text="An error occured"></asp:Label>
</asp:Content>


这是menu.aspx.cs形式的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Menu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable bookTypes;
        eLibraryServer.DAL oDAL = new eLibraryServer.DAL();
        try
        {
            bookTypes = oDAL.getBookTypes();
            GridView1.DataSource = bookTypes.DefaultView;
            GridView1.DataBind();
        }
        catch (Exception ex) { LableErr.Text = "No Link to data Server"; }

    }
    public void Get_Category(object Src, CommandEventArgs Args)
    {
        Response.Redirect("Menu.aspx?Category=" + Args.CommandName);
    }
}


这是我的CSS代码:

body, div,p,ul,li {
    padding:0 ;
    margin:0;
    background-color:rgb(237,237,237);
    font-family: Arial, Arial, Helvetica, sans-serif;
    font-size: 12px;
}

.body {

    font-family: "Arial", Helvetica, sans-serif;
    font-size: 12px;
}
/*.wrapper {
width:950;
margin:auto;
}*/

.logo1 {
    width: 100%;
    height: 150px;

    top: 40px;
    left: 10px;
    z-index: 1000
}


.content {
width:50%;
background-color: rgb(254,254,254);
border: 1px solid rgb(224,224,224);

float: right;
margin-left: auto;
margin-right:10px;
margin-top: 8px;
margin-bottom: 8px;
min-height: 500px;
}
.leftcontent {
width:30%;
background-color: rgb(254,254,254);
border: 1px solid rgb(224,224,224);

float: left;
margin-left: 10px;
margin-right:auto;
margin-top: 8px;
margin-bottom: 8px;
min-height: 500px;

}

.image {
    width:100%;
    height:200px;
}

.menu {
    background-color: rgb(251, 240, 28);
    width:100%;
    margin: 0px 0px 10px;
    padding: 0px;
    height:20px;
    color: rgb(243, 243, 243);
    border-radius: 5px 5px 5px 5px;
    border: thin outset #A9A9A9 !important;

}
.navigation_first_item {
    border-left: 0px;
    border-radius: 5px 0 0 5px;
}
.navitem_s {
    float:left;
    border-right: 1px solid rgb(10,85,125);
    border-left: 1px solid rgb(67,153,200);
    height: 40px;
    background-color: rgb(14,79,114);

}
.menu ul {

}
.menu ul li {
    float:left;
    display:block;
    list-style: none;
    border-right: 1px solid rgb(10,85,125);
    border-left: 1px solid rgb(67,153,200);


}
    .menu ul li.navigation_first_item:hover {
        border-radius: 5px 0px 0px 5px;
    }


    .menu ul li a {
        font-size: 13px;
        font-weight: bold;
        line-height:40px;
        padding:8px 20px;
        color: rgb(255 255 255);
        text-decoration:none;
    }

menu ul li:hover {
    background-color: rgb(14,79,144);
    border-right:1px solid rgb(14,89,130);
}
.clear {
    clear: both;
}
.footer {
    height:50px;
    background-color: rgb(251, 240, 28);
    color: rgb(255,255,255);
    border-radius: 5px 5px 5px 5px;
}
    .footer.h2 {
        padding:15px;
        text-align: center;
    }
.leftcontent.myGrid {
    width: 30%;
    background-color: #fff;
    margin: 5px 0 10px 0;
    border: solid 1px #525252;
    border-collapse:collapse;
}

最佳答案

我现在看到的问题可能是因为您设置了AutoGenerateColumns="False",因此GridView期望<Columns>部分中的绑定字段。
我建议为数据表中所有必需的绑定列创建节,或者将AutoGenerateColumns="true"设置为自动绑定数据集中的这些列。

其他清单

检查您的DataTable bookTypes是否为空。

我建议添加一个固定的标题,当它为空时至少显示网格

private void FixGridHeader(DataTable bookTypes)
{
     //add blank row to the the resultset
     bookTypes.Rows.Add(dataSource.NewRow());
     bookTypes.Rows[0]["field1"] = 0;
     bookTypes.Rows[0]["field2"] = 0;
     bookTypes.Rows[0]["field3"] = 0;

     GridView1.DataSource = bookTypes;
     GridView1.DataBind();

     //hide empty row
     GridView1.Rows[0].Visible = false;
}


然后进行检查以确定是否可以加载网格,是否可以显示空标题。

if (bookTypes.Rows.Count > 0)
{
    //Load Grid
}
else
{
    FixGridHeader(bookTypes);
    //Can also add an label as alternative
}


我还要检查CssClass="myGrid"以确保样式表中没有设置display:nonevisibility:hidden方法

08-08 07:51
查看更多