我无法在页面加载中看到我的Gridview

时间:2013-12-29 12:27:41

标签: c# asp.net gridview

以前工作过,我可以在网站上看到Gridview。但现在,我再也看不到了。我不知道我在代码中发生了什么变化。

在页面 SI.aspx 中我有:

<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" 
                style="margin-top: 0px" CssClass="tb" HorizontalAlign="Center" 
               >
        <Columns>
        <asp:BoundField DataField="RowNumber" HeaderText="SI_id" />
        <asp:TemplateField HeaderText="CTNS_NO">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="SEAL_NO">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="PCS">
            <ItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="W.G.">
            <ItemTemplate>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="CBM">
            <ItemTemplate>
                 <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
             <asp:Button ID="ButtonAdd" runat="server" Text="Add New Line" 
                    onclick="ButtonAdd_Click"  CssClass="btn"/>
            </FooterTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:gridview>

我想我已经改变了代码背后的东西,这就是为什么gridview无法显示。 以下是SI.aspx.cs背后的代码:

private void SetInitialRow()
{
    try
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("CTNS_NO", typeof(string)));
        dt.Columns.Add(new DataColumn("SEAL_NO", typeof(string)));
        dt.Columns.Add(new DataColumn("PCS", typeof(string)));
        dt.Columns.Add(new DataColumn("[W.G.]", typeof(string)));
        dt.Columns.Add(new DataColumn("CBM", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["CTNS_NO"] = string.Empty;
        dr["SEAL_NO"] = string.Empty;
        dr["PCS"] = string.Empty;
        dr["[W.G.]"] = string.Empty;
        dr["CBM"] = string.Empty;

        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["SI"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }
    catch
    {
    }
}

private void AddNewRowToGrid()
{
    try
    {
        int rowIndex = 0;

        if (ViewState["SI"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["SI"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["CTNS_NO"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["SEAL_NO"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["PCS"] = box3.Text;
                    dtCurrentTable.Rows[i - 1]["[W.G.]"] = box4.Text;
                    dtCurrentTable.Rows[i - 1]["CBM"] = box5.Text;
                    System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["EPSYLOG_DBBConnectionString"].ConnectionString);

                    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.CommandText = "INSERT into SI ([CTNS_NO],[SEAL_NO],[PCS],[W.G.],[CBM],[name],[bk_nbr],[shipper],[cons],[pol],[pod],[notify],[size]) values('" + box1.Text + "','" + box2.Text + "','" + box3.Text + "','" + box4.Text + "','" + box5.Text + "','" + name.Text + "','" + bn.Text + "','" + shp.Text + "','" + cng.Text + "','" + pol.Text + "','" + POD.Text + "','" + notif.Text + "','" + sz.Text + "')";
                    cmd.Connection = sqlConnection1;

                    sqlConnection1.Open();
                    cmd.ExecuteNonQuery();
                    sqlConnection1.Close();
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["SI"] = dtCurrentTable;

                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }
    catch{ }
}

private void SetPreviousData()
{
    try
    {
        int rowIndex = 0;
        if (ViewState["SI"] != null)
        {
            DataTable dt = (DataTable)ViewState["SI"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");

                    box1.Text = dt.Rows[i]["CTNS_NO"].ToString();
                    box2.Text = dt.Rows[i]["SEAL_NO"].ToString();
                    box3.Text = dt.Rows[i]["PCS"].ToString();
                    box4.Text = dt.Rows[i]["[W.G.]"].ToString();
                    box5.Text = dt.Rows[i]["CBM"].ToString();

                    rowIndex++;
                }
            }
        }
    }
    catch
    { }

}

protected void Page_Load(object sender, EventArgs e)
{
    Gridview1.DataBind();
    if (!Page.IsPostBack)
    {
        SetInitialRow();
    }
    try
    {
        if (Session["MySession"] == "")
        {
            Response.Redirect("authentication.aspx");
        }
        else
        {
            mail.Text = Session["e"].ToString();
        }
    }
    catch
    {
        Response.Redirect("authentication.aspx");
    }
    try
    {
        name.Text = Session["MySession"].ToString();
        bn.Text = Convert.ToString(Request.QueryString["arg1"]);
        shp.Text = Convert.ToString(Request.QueryString["arg2"]);
        cng.Text = Convert.ToString(Request.QueryString["arg3"]);
        pol.Text = Convert.ToString(Request.QueryString["arg4"]);
        POD.Text = Convert.ToString(Request.QueryString["arg5"]);
        notif.Text = Convert.ToString(Request.QueryString["arg6"]);
    }
    catch
    {
        Response.Redirect("authentication.aspx");
    }
}

protected void ButtonAdd_Click(object sender, EventArgs e)
{
    AddNewRowToGrid();
}

这就是Gridview的样子,它是空的,我们应该填充它并将其添加到数据库中: enter image description here

2 个答案:

答案 0 :(得分:1)

当您的数据库(或您正在查找的表)不包含任何内容时,会出现此类问题。尝试向表中添加一些数据,如果这样做无法删除 trycatch 块,那么您将看到问题出在哪里

答案 1 :(得分:1)

(1)。设置ShowHeaderWhenEmpty = "True"

ShowHeaderWhenEmpty是获取或设置一个值,该值指示当列没有数据时GridView控件中列的标题是否可见。

<asp:gridview ID="Gridview1" ShowHeaderWhenEmpty="True" ...
    ...
</asp:gridview>

(2)。检查您的Css类tb并确保它未设置为隐藏。

不确定为什么在DataBind()检查

之前调用Page.PostBack方法
protected void Page_Load(object sender, EventArgs e)
{
    //Gridview1.DataBind(); Remove it
    if (!Page.IsPostBack)
    {
        SetInitialRow();
    }

    ...

}