1

我有具有以下简单代码的 apsx 页面:

<%@ Page Title="" Language="C#" MasterPageFile="~/Team.master" AutoEventWireup="True" Inherits="Lib.team" Codebehind="team.aspx.cs" %>


<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">


    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>

</asp:Content>

我的背后代码如下:

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


namespace Lib
{
  public partial class team : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataReader reader = DBData.ExecuteQuery("SELECT * FROM tblTeam");
        GridView1. //This is where the error is
    }
  }
}

我的问题是我似乎无法让后面的代码识别控制变量 GridView1。我在 VS2010 中使用 Web 应用程序,下面是我的项目树:

在此处输入图像描述

我认为这是我错过的非常简单的事情。我试图重新检查我的命名空间,我的继承子句,重新编译项目。任何帮助或指导将不胜感激。

谢谢

4

3 回答 3

1

我尝试了每个人建议的所有步骤,但没有运气......我不太清楚为什么。但是,正如我写的一条评论中提到的那样,我有一个解决方法。基本上我只是将我的项目转换为网站项目而不是 Web 应用程序。

谢谢大家。

于 2012-06-05T13:17:58.220 回答
0

are you initiating a connection to the SQL instance? Your gridview will not populate unless you are directly connected to the database.

SQL conn = new SQL connection (connection string)

You can test this by calling the query or changing the query to alter a row in the database. If the database changes then you are already connected.

Might want to try initializing a new GridView instance also...

Here's a reference...

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.gridview.aspx

So here is the work from school.

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("Project_ID", Type.GetType("System.Int32"));
        dt.Columns.Add("ProjectName", Type.GetType("System.String"));

        DataSet dsProd = new DataSet();
        DataTable dtProd = new DataTable();
        dtProd.Columns.Add("Product_ID", Type.GetType("System.Int32"));
        dtProd.Columns.Add("Product_Name", Type.GetType("System.String"));

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = @"Data Source=YAZEED-PC\YAZEED;Initial Catalog=ITS364_Project;Integrated Security=True";

        String strSQL = "Select Project_ID,ProjectName from project";
        SqlCommand command = new SqlCommand();
        SqlDataReader dr;
        try
        {
            conn.Open();
            command.Connection = conn;
            command.CommandText = strSQL;
            dr = command.ExecuteReader();
            while (dr.Read())
            {
                DataRow drr = dt.NewRow();
                drr[0] = dr[0];
                drr[1] = dr[1];
                dt.Rows.Add(drr);

            }
            ds.Tables.Add(dt);
            ddProj.DataValueField = "ProjectName";
            ddProj.DataTextField = "Project_ID";
            ddProj.DataSource = ds.Tables[0].DefaultView;
            ddProj.DataBind();
            txtProjName.Text = Convert.ToString(ddProj.SelectedItem.Value);
        }

        finally
        {
            conn.Close();
        }

        string strr = "Select Product_ID, Product_Name from Products";
        try
        {
            conn.Open();
            command.Connection = conn;
            command.CommandText = strr;
            dr = command.ExecuteReader();
            while (dr.Read())
            {
                DataRow drr = dtProd.NewRow();

                drr[0] = dr[0];
                drr[1] = dr[1];

                dtProd.Rows.Add(drr);

            }
            dsProd.Tables.Add(dtProd);
            ddProd.DataValueField = "Product_ID";
            ddProd.DataTextField = "Product_Name";
            ddProd.DataSource = dsProd.Tables[0].DefaultView;
            ddProd.DataBind();

            if (ddProd.SelectedIndex != -1)
            {
                txtProdQuantity.Text = getQuan(Convert.ToInt32(ddProd.SelectedValue));
                fillQtyCombo();
            }
        }
        finally
        {
            conn.Close();
        }

    }

}
protected string getQuan(int val)
{
    string str;
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = @"Data Source=YAZEED-PC\YAZEED;Initial Catalog=ITS364_Project;Integrated Security=True";
    conn.Open();
    String strSQL = "Select Product_Qty from Products where Product_ID = " + val + "";
    SqlCommand command = new SqlCommand();
    command.Connection = conn;
    command.CommandText = strSQL;
    str = Convert.ToString(command.ExecuteScalar());
    return str;
}
protected void btnAssignProducts_Click(object sender, EventArgs e)
{
    if (txtTaskName.Text == string.Empty)
    {
        msg.Text = "";

        this.error.Text = "Please enter Task Name";

    }
    else if (ddProj.SelectedIndex == -1)
    {
        msg.Text = "";
        this.error.Text = "Please Select Project";
    }
    else if (ddProd.SelectedIndex == -1)
    {
        msg.Text = "";
        this.error.Text = "Please select Product";
    }
    else if (Convert.ToInt32(ddProdQuantity.SelectedValue) == 0)
    {
        msg.Text = "";
        this.error.Text = "Please select Product Quantity ";
    }
    else
    {
        int rtn = 0;
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = @"Data Source=YAZEED-PC\YAZEED;Initial Catalog=ITS364_Project;Integrated Security=True";
        conn.Open();
        //String strSQL = "INSERT INTO Product_task (Project_ID,Product_ID,Product_Task_desc,Product_Task_Qty) values ("+ddProj.SelectedValue+","+ddProd.SelectedValue+",'"+txtTaskName.Text+"',"+ddProdQuantity.SelectedValue+")";
        String strSQL = "INSERT INTO Product_task (Project_ID,Product_ID,Product_Task_desc) values (" + Convert.ToInt32(ddProj.SelectedItem.Text) + "," + ddProd.SelectedValue + ",'" + txtTaskName.Text + "')";
        SqlCommand command = new SqlCommand();
        command.Connection = conn;
        command.CommandText = strSQL;
        rtn = Convert.ToInt32(command.ExecuteNonQuery());
        if (rtn > 0)
        {
            error.Text = "";
            msg.Text = "Record Saved Successfully";
        }
        else
        {
            msg.Text = "";
            error.Text = "Record Not saved Successfully, Kindly Try again.";
        }


    }

}
protected void ddProj_SelectedIndexChanged(object sender, EventArgs e)
{
    txtProjName.Text = Convert.ToString(ddProj.SelectedItem.Value);
}
protected void ddProd_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddProd.SelectedIndex != -1)
    {
        txtProdQuantity.Text = getQuan(Convert.ToInt32(ddProd.SelectedValue));
        fillQtyCombo();
    }
}
protected void fillQtyCombo()
{
    if (ddProd.SelectedIndex != -1)
    {
        int val = Convert.ToInt32(getQuan(Convert.ToInt32(ddProd.SelectedValue)));
        DataSet dsQty = new DataSet();
        DataTable dtQty = new DataTable();
        dtQty.Columns.Add("ID", Type.GetType("System.Int32"));
        dtQty.Columns.Add("VAL", Type.GetType("System.String"));


        for (int i = 1; val >= i; i++)
        {

            DataRow drr = dtQty.NewRow();

            drr[0] = i;
            drr[1] = i;

            dtQty.Rows.Add(drr);
        }
        dsQty.Tables.Add(dtQty);
        ddProdQuantity.DataValueField = "ID";
        ddProdQuantity.DataTextField = "VAL";
        ddProdQuantity.DataSource = dsQty.Tables[0].DefaultView;
        ddProdQuantity.DataBind();

    }
}

}

于 2012-05-29T16:48:25.750 回答
0

First thing to check: Are you seeing the control as a property in the relevant ascx.designer.cs class? If not then, delete the designer class and save the ASCX, this should cause it to be regenerated.

If this fails, i usually find it's visual studio or resharper playing up and a restart of VS usually solves the issue.

Hope that helps

于 2012-05-29T16:45:14.207 回答