从SQL Server数据库加载页面时看不到我的gridview

时间:2014-10-31 00:56:10

标签: c# sql asp.net sql-server gridview

我做了一个简单的gridview示例和dunno为什么,但网站上根本不存在gridview。

以下是我在浏览器中获得的图片 - 没有... http://ipic.su/img/img7/fs/example.1414716455.jpg

有人可以看到任何错误吗?

<%@Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeFile="InvoiceEdit.aspx.cs" Inherits="InvoiceEdit" EnableEventValidation="false" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
  <style>
.gridview 
{
    background: red;
    border:dashed 1px blue;
}
</style>
  <asp:GridView ID="gvInvoice" runat="server"            
    AutoGenerateColumns="False" 
    OnRowDataBound="gvInvoice_RowDataBound"
    DataKeyNames="InvoiceID"   
    AutoGenerateEditButton="False" 
    CellPadding="1" Width="100%" 
    datasourceid="SqlDataSource1"
    CssClass ="gridview">
<Columns >
    <asp:BoundField HeaderText="InvoiceID" DataField="InvoiceID" ReadOnly="true" /> 
    <asp:BoundField HeaderText="RechnNummer" DataField="RechnNummer" ReadOnly="true" />
    <asp:BoundField HeaderText="Kunde" DataField="Kunde" ReadOnly="true" />
    <asp:BoundField HeaderText="Text" DataField="Text" ReadOnly="true"/>            
</Columns>
</asp:GridView>
----TEST TEXT----
<asp:sqldatasource id="SqlDataSource1"          
    selectcommand="Select * from Invoice where InvoiceID = @InvoiceID"
    updatecommand="Update Invoice set RechnNummer = @RechnNummer, Kunde=@Kunde, Text=@Text, RechnDatum=@RechnDatum, Netoo=@Netoo,
                  Umsatzsteuer=@Umsatzsteuer, Brutto=@Brutto, Eingang=@Eingang where InvoiceID = @InvoiceID"        
    connectionstring="<%$ ConnectionStrings:Invoice%>"
    runat="server" >    
    <SelectParameters> 
        <asp:ControlParameter ControlID="gvInvoice" PropertyName="SelectedValue" Name="InvoiceID" />
    </SelectParameters>
    <UpdateParameters>
        <asp:ControlParameter ControlID="gvInvoice" PropertyName="SelectedValue" Name="InvoiceID" />
    </UpdateParameters>
  </asp:sqldatasource>
</asp:Content>

代码隐藏几乎是空的,但我认为没关系...也许某些库丢失了?或者有什么问题?谢谢你的答案!

public partial class InvoiceEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)  
    {      
    }

    protected void gvInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        { 
            e.Row.Attributes.Add("onmouseover",    "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#C2C2C2'");
            e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=this.originalstyle;");
            e.Row.Attributes["style"] = "cursor:pointer";
        }
   }
}

1 个答案:

答案 0 :(得分:0)

尝试this

添加文字字段:

 <asp:TextBox ID="txtInvoiceID" runat="server"></asp:TextBox>

在数据源代码上更改Select语句和Select Parameter:

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Invoice %>"
    SelectCommand="Select * from Invoice where InvoiceID = @InvoiceID OR @InvoiceID IS NULL" CancelSelectOnNullParameter="false">
    <SelectParameters>
        <asp:QueryStringParameter Name="InvoiceID" DbType = "String" Direction = "Input" QueryStringField="Id" DefaultValue="" ConvertEmptyStringToNull="True" />
    </SelectParameters>
</asp:SqlDataSource>

代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        txtInvoiceID.Text = Request.QueryString["Id"];
    }
}

protected void btnRedirect_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Default.aspx?Id=" + txtInvoiceID.Text.Trim());
}

您必须添加SQL参数..数据网格中没有数据,因此数据网格不可见。