简体   繁体   English

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

[英]Code behind does not recognize my control variable on its aspx page

I have apsx page that has the following simple code: 我有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 code is as follow: 我的背后代码如下:

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
    }
  }
}

My problem is I can't seem to get the behind code to recognize the control variable GridView1. 我的问题是我似乎无法获得后面的代码来识别控制变量GridView1。 I am using a web application in VS2010 and below is my project tree: 我在VS2010中使用Web应用程序,下面是我的项目树:

在此处输入图片说明

I think it is something very simple that I missed. 我认为这是我很想念的事情。 I tried to recheck my namespace, my inherit clause, recompile the project. 我试图重新检查我的命名空间,继承子句,重新编译项目。 Any help or guidance would be greatly appreciated. 任何帮助或指导将不胜感激。

Thanks 谢谢

I tried all of the steps everyone suggested but no luck... I'm not quite sure why. 我尝试了所有人建议的所有步骤,但是没有运气...我不太确定为什么。 However, I have a work around as mentioned in one of the comment I wrote. 但是,正如我写的评论之一所述,我已经解决了。 Basically I just convert my project into a Web Site Project and not a web application. 基本上,我只是将项目转换为网站项目而不是Web应用程序。

thank you everyone. 谢谢大家。

First thing to check: Are you seeing the control as a property in the relevant ascx.designer.cs class? 首先要检查:您是否在相关的ascx.designer.cs类中将控件视为属性? If not then, delete the designer class and save the ASCX, this should cause it to be regenerated. 如果不是,请删除设计器类并保存ASCX,这将导致重新生成它。

If this fails, i usually find it's visual studio or resharper playing up and a restart of VS usually solves the issue. 如果失败,我通常会发现它是Visual Studio或Resharper,而VS的重新启动通常可以解决问题。

Hope that helps 希望能有所帮助

are you initiating a connection to the SQL instance? 您正在启动与SQL实例的连接吗? Your gridview will not populate unless you are directly connected to the database. 除非您直接连接到数据库,否则不会填充gridview。

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

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... 可能还想尝试初始化新的GridView实例...

Here's a reference... 这是参考...

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.gridview.aspx http://msdn.microsoft.com/zh-CN/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();

    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM