后面的代码无法在其aspx页面上识别我的控制变量

时间:2012-05-29 16:33:06

标签: c# asp.net

我的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>

My Behind代码如下:

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应用程序,下面是我的项目树:

enter image description here

我觉得这很简单,我错过了。我试图重新检查我的命名空间,我的继承子句,重新编译项目。任何帮助或指导将不胜感激。

由于

3 个答案:

答案 0 :(得分:1)

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

谢谢大家。

答案 1 :(得分:0)

要检查的第一件事是:您是否将控件视为相关ascx.designer.cs类中的属性?如果没有,则删除设计器类并保存ASCX,这应该会重新生成它。

如果失败了,我通常会发现它正在播放视觉工作室或重新演绎,重启VS通常会解决问题。

希望有所帮助

答案 2 :(得分:0)

您是否正在启动与SQL实例的连接?除非您直接连接到数据库,否则不会填充gridview。

SQL conn =新的SQL连接(连接字符串)

您可以通过调用查询或更改查询来更改数据库中的行来对此进行测试。如果数据库发生了变化,那么您已经连接了。

可能还想尝试初始化一个新的GridView实例......

这是一个参考......

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

所以这是学校的工作。

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();

    }
}

}