GridView没有显示

时间:2010-12-15 14:32:24

标签: c#

在这里使用基本的C#Web应用程序,我似乎无法显示GridView。我可以看到它传递代码,因为它将我的图像从一个更改为另一个但由于某种原因Gridview没有显示。使用回发按钮和我的下拉框具有我的存储过程的变量。我是C#网络应用程序的新手(Vb Win app guy),所以我需要一些指导。

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        //This is where my postback begins...
        if (IsPostBack)
        {
            string Quote = null;

            System.Data.SqlClient.SqlConnection myDatabaseConnection = null;
            System.Data.SqlClient.SqlCommand myCommand = null;
            System.Data.SqlClient.SqlDataReader myReader = null;

            // Validate the SP
            Page.Validate();
            if (Page.IsValid)
            {
                Quote = DropDownList1.Text.ToString();

                try
                {
                    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Data1"].ConnectionString;
                    myDatabaseConnection = new System.Data.SqlClient.SqlConnection(connectionString);
                    myCommand = new System.Data.SqlClient.SqlCommand();

                    //Set up to use my stored procedure:
                    myCommand.CommandType = System.Data.CommandType.StoredProcedure;
                    myCommand.Connection = myDatabaseConnection;
                    myCommand.CommandText = "EditDataPage";
                    myCommand.Parameters.AddWithValue("@QuoteNumber", Quote);


                    //Use an SqlDataReader to execute the stored procedure and
                    //get the results into the GridView:
                    myDatabaseConnection.Open();
                    myReader = myCommand.ExecuteReader();
                    GridView1.DataSource = myReader;
                    GridView1.DataBind();
                    myDatabaseConnection.Close();
                    myDatabaseConnection.Dispose();
                    GridView1.Visible = true;
                    Image1.Visible = false;
                    Image2.Visible = true;
                }
                catch (System.Data.SqlClient.SqlException exception)
                {
                    ErrorLabel.Visible = true;
                }
                catch (Exception exception)
                {
                    ErrorLabel.Visible = true;
                    //Do cleanup tasks here:
                }
                finally
                {
                    myCommand = null;

                    if ((myReader != null) && !myReader.IsClosed)
                    {
                        myReader.Close();
                    }

                    myReader = null;

                    if ((myDatabaseConnection != null) && myDatabaseConnection.State == System.Data.ConnectionState.Open)
                    {
                        myDatabaseConnection.Close();
                        myDatabaseConnection.Dispose();
                    }

                    myDatabaseConnection = null;
                }
            }
        }
    }

}

继承我的aspx代码:

    

        编辑数据

    使用控件在数据集中移动,然后编辑并保存所需内容     为您的报告。

<table class="yui-d0f">
    </table>
<table class="yui-main">
    <tr>
        <td>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:Data1 %>" 
                SelectCommand="EditDataPage" SelectCommandType="StoredProcedure">
                <SelectParameters>
                    <asp:FormParameter FormField="DropDownList1" Name="QuoteNumber" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
            <table class="style1">
                <tr>
                    <td align="center">
                        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="DropDownList" 
                            DataTextField="QuoteNumber" DataValueField="QuoteNumber" Height="23px" 
                            Width="188px">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="DropDownList" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:Data1 %>" 
                            SelectCommand="SELECT QuoteNumber FROM SF1411 GROUP BY QuoteNumber">
                        </asp:SqlDataSource>
                    </td>
                    <td>
                        <table class="style1">
                            <tr>
                                <td class="style2">
            <asp:Button ID="ListAllButton" runat="server" Height="32px" Text="Show Data" 
                Width="111px" PostBackUrl="~/EditData.aspx" />
                                </td>
                                <td>
                                    <asp:Image ID="Image1" runat="server" 
                                        ImageUrl="~/_assets/img/lock-disabled-icon.png" />
                                    <asp:Image ID="Image2" runat="server" ImageUrl="~/_assets/img/lock-icon.png" 
                                        Visible="False" />
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
            <asp:Label ID="ErrorLabel" runat="server" Font-Underline="True" 
                ForeColor="Maroon" Text="Error in Processing, try again later." Visible="False"></asp:Label>
        </td>
    </tr>
    </table>

<asp:GridView ID="GridView1" runat="server" CssClass="styled" 
    AutoGenerateColumns="False">
</asp:GridView>

1 个答案:

答案 0 :(得分:1)

我不是百分百肯定,但是你的GridView上有AutoGenerateColumns="false"而没有定义列的事实看起来有点可疑。我想作为一个快速测试,您可以将此属性更改为true,看看会发生什么。

如果您确实想要自己指定列(根据我的经验是常态),那么您需要指定它们,例如:

<asp:GridView ...>
   <Columns>
      <asp:BoundField DataField="Name" HeaderText="Name" />
      <asp:TemplateField>
         <asp:Label Text='<%# Eval("Surname") %>' runat="server" />
      </asp:TemplateField>
   </Columns>
</asp:GridView>

有关详细信息,请参阅MSDN