2013-02-04 98 views
1

我有一個與我在使用GridView的工作分配有關的問題。當我運行我的程序時,GridView不工作,我無法弄清楚爲什麼會發生這種情況。我已經配置了我的數據庫,在那裏一切都很好。我真的很感謝一些幫助。當我運行我的程序時,GridView沒有顯示

這裏是我的源代碼:

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

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> 
    <style type="text/css"> 
     .style2 
     { 
      width: 125px; 
     } 
     .style3 
     { 
      width: 182px; 
     } 
    </style> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 
    <p> 
     <h3 style="font-size: larger">Product Maintenance</h3><br /> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      EnableSortingAndPagingCallbacks="True" Width="664px" 
         onrowdeleted="GridView1_RowDeleted" onrowupdated="GridView1_RowUpdated"> 
     </asp:GridView> 
     <br /> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%$ ConnectionStrings:TechSupport_DataConnectionString %>" 
      DeleteCommand="DELETE FROM Products WHERE (ProductCode = @Original_ProductCode) AND (Name = @Original_Name) AND (Version = @Original_Version) AND (ReleaseDate = @Original_ReleaseDate)" 
      InsertCommand="INSERT INTO Products(ProductCode, Name, Version, ReleaseDate) VALUES (@ProductCode, @Name, @Version, @ReleaseDate)" 
      SelectCommand="SELECT ProductCode, Name, Version, ReleaseDate FROM Products" 
      UpdateCommand="UPDATE Products SET Name = @Name, Version = @Version, ReleaseDate = @ReleaseDate WHERE (Name = @original_Name) AND (Version = @original_Version) AND (ReleaseDate = @original_ReleaseDate) AND (ProductCode = @original_ProductCode)"> 
      <DeleteParameters> 
       <asp:Parameter Name="Original_ProductCode" /> 
       <asp:Parameter Name="Original_Name" /> 
       <asp:Parameter Name="Original_Version" /> 
       <asp:Parameter Name="Original_ReleaseDate" /> 
      </DeleteParameters> 
      <UpdateParameters> 
       <asp:Parameter Name="Name" /> 
       <asp:Parameter Name="Version" /> 
       <asp:Parameter Name="ReleaseDate" /> 
       <asp:Parameter Name="original_Name" /> 
       <asp:Parameter Name="original_Version" /> 
       <asp:Parameter Name="original_ReleaseDate" /> 
       <asp:Parameter Name="original_ProductCode" /> 
      </UpdateParameters> 
      <InsertParameters> 
       <asp:Parameter Name="ProductCode" /> 
       <asp:Parameter Name="Name" /> 
       <asp:Parameter Name="Version" /> 
       <asp:Parameter Name="ReleaseDate" /> 
      </InsertParameters> 
     </asp:SqlDataSource> 
     <br /> 
     To add a new product, enter the product information and clck Add product.<br /> 
     <asp:Label ID="lblError" runat="server" ForeColor="Red"></asp:Label> 
     <br /> 
     <table class="style1"> 
      <tr> 
       <td class="style2"> 
        Product code:</td> 
       <td class="style3"> 
        <asp:TextBox ID="txtProductCode" runat="server"></asp:TextBox> 
       </td> 
       <td> 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
         ControlToValidate="txtProductCode" 
         ErrorMessage="Product code is a required field"></asp:RequiredFieldValidator> 
       </td> 
      </tr> 
      <tr> 
       <td class="style2"> 
        Name:</td> 
       <td class="style3"> 
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox> 
       </td> 
       <td> 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
         ControlToValidate="txtName" ErrorMessage="Name is a required field"></asp:RequiredFieldValidator> 
       </td> 
      </tr> 
      <tr> 
       <td class="style2"> 
        Version:</td> 
       <td class="style3"> 
        <asp:TextBox ID="txtVersion" runat="server"></asp:TextBox> 
       </td> 
       <td> 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
         ControlToValidate="txtVersion" ErrorMessage="Version is a required field"></asp:RequiredFieldValidator> 
       </td> 
      </tr> 
      <tr> 
       <td class="style2"> 
        Release date:</td> 
       <td class="style3"> 
        <asp:TextBox ID="txtReleaseDate" runat="server"></asp:TextBox> 
       </td> 
       <td> 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
         ControlToValidate="txtReleaseDate" 
         ErrorMessage="Release date is a required field"></asp:RequiredFieldValidator> 
       </td> 
      </tr> 
     </table> 
     <br /> 
     <asp:Button ID="btnAdd" runat="server" Text="Add Product" Width="130px" 
     onclick="btnAdd_Click" /> 
    </p> 
</asp:Content> 
+0

檢查此... http://stackoverflow.com/questions/10288800/gridview-in-asp-net-is-not-displaying-with-or-without-data –

+0

@ user1976537:你已經定義了AutoGenerateColumns = 「假」並沒有定義任何模板列或綁定字段來顯示這就是爲什麼你沒有在屏幕上獲得任何數據。設置AutoGenerateColumns =「True」或在gridview中定義列以使其顯示在屏幕上。 –

回答

0

起初讓AutoGenerateColumns = true

然後,

protected void Page_Load(object sender, EventArgs e) 
{ 
    GridView1.DataBind(); 
} 
0

你必須給GridView1.DataSource = SqlDataSource1;GridView1.DataBind();Page_Load事件後

您必須將AutoGenerateColumns設置爲true,或將列提供爲要顯示的Bound - 或TemplateField。

AutoGenerateColumns="True"將顯示數據並生成列。您可能需要自定義顯示的內容。要做到這一點,切換到設計視圖,你可以編輯gridview模板。

0

因爲你設置的AutoGenerateColumns =「false」時,你需要添加像

<Columns> 
<asp:BoundField .... 
    . 
    . 
    . 
    . 
</Columns> 

否則列,你必須設置的AutoGenerateColumns =「真」

另一件事:你忘了分配DataSourceID到你的GridView。

你應該有GridView控件標籤內的以下屬性:

DataSourceID = "SqlDataSource1" 

我希望它可以幫助你。

相關問題