【问题标题】:I can not see my Gridview in page load我在页面加载中看不到我的 Gridview
【发布时间】:2014-01-16 11:45:44
【问题描述】:

它以前工作过,我可以在网站上看到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 的样子,它是空的,我们应该填充它并将其添加到数据库中:

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    当您的数据库(或您正在寻找的表)不包含任何内容时,就会出现这种问题。如果这不起作用,请尝试将一些数据添加到您的表中删除您的 trycatch 块,然后您会看到问题出在哪里

    【讨论】:

      【解决方案2】:

      (1)。设置ShowHeaderWhenEmpty = "True"

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

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

      (2)。检查您的 Css Class tb 并确保它没有设置为隐藏。

      不知道为什么在Page.PostBack 检查之前调用DataBind() 方法

      protected void Page_Load(object sender, EventArgs e)
      {
          //Gridview1.DataBind(); Remove it
          if (!Page.IsPostBack)
          {
              SetInitialRow();
          }
      
          ...
      
      }
      

      【讨论】:

      • 我已经尝试了你所说的,但仍然没有显示 Gridview .. 我已经编辑了我的帖子,看看我的 gridview 在页面加载中应该是什么样子..
      猜你喜欢
      • 1970-01-01
      • 2011-07-18
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多