页面控件在当前上下文中不存在

时间:2012-06-19 06:52:13

标签: c# asp.net

在我的设计部分,我添加了所有标签和下拉列表,但在register.aspx.cs中,它显示当前上下文中不存在这些控件。

    using System;    
    using System.Collections;    
    using System.Configuration;    
    using System.Data;    
    using System.Linq;    
    using System.Web;    
    using System.Web.Security;    
    using System.Web.UI;    
    using System.Web.UI.HtmlControls;    
    using System.Web.UI.WebControls;    
    using System.Web.UI.WebControls.WebParts;     
    using System.Xml.Linq;    
    using System.Data.SqlClient;  
    using System.IO;


    public partial class register : System.Web.UI.Page    
    {    
    SqlConnection con = new SqlConnection("server = .\\SQLEXPRESS;AttachDbFileName    =|DataDirectory|\\Database123.mdf;trusted_connection = true;USER Instance =yes");    
    protected void Page_Load(object sender, EventArgs e)
    {

    }



    protected void Button2_Click(object sender, EventArgs e)
    {


        try
        {

            if (CheckBox1.Checked == true)
            {
                String f;
                f = Path.GetFileName(FileUpload1.FileName);
                con.Open();
                if (f != "")
                {

                    FileUpload1.SaveAs(Server.MapPath("~") + "/images/" + f);
                    Label2.Text = "~" + "/images/" + f;

                    SqlCommand cmd1 = new SqlCommand("insert into register values('" + TextBox1.Text + "','" + TextBox8.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + RadioButtonList1.SelectedValue + "','" + TextBox4.Text + "','" + DropDownList3.SelectedItem + "','" + DropDownList2.SelectedItem + "','" + TextBox7.Text + "','" + TextBox10.Text + "','" + RadioButtonList2.SelectedValue + "','" + TextBox12.Text + "','" + Label2.Text + "','" + DropDownList1.Text + "','" + TextBox14.Text + "')", con);
                    cmd1.ExecuteNonQuery();
                    TextBox1.Text = " ";
                    TextBox8.Text = " ";
                    TextBox2.Text = " ";
                    TextBox3.Text = " ";
                    TextBox4.Text = " ";
                    // TextBox15.Text = " ";
                    // TextBox16.Text = " ";
                    TextBox7.Text = " ";
                    TextBox10.Text = " ";
                    TextBox12.Text = " ";
                    TextBox14.Text = " ";

                    Label1.Visible = true;
                    Label1.Text = "submitted successfully";
                }

                else
                {

                    SqlCommand cmd1 = new SqlCommand("insert into register(user_id,password,fname,lname,user_type,address,city,state,pin,contact_no,gender,email,seq_que,seq_ans) values('" + TextBox1.Text + "','" + TextBox8.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + RadioButtonList1.SelectedValue + "','" + TextBox4.Text + "','" + DropDownList3.SelectedItem + "','" + DropDownList2.SelectedItem + "','" + TextBox7.Text + "','" + TextBox10.Text + "','" + RadioButtonList2.SelectedValue + "','" + TextBox12.Text + "','" + DropDownList1.Text + "','" + TextBox14.Text + "')", con);
                    cmd1.ExecuteNonQuery();
                    TextBox1.Text = " ";
                    TextBox8.Text = " ";
                    TextBox2.Text = " ";
                    TextBox3.Text = " ";
                    TextBox4.Text = " ";
                    // TextBox15.Text = " ";
                    //TextBox16.Text = " ";
                    TextBox7.Text = " ";
                    TextBox10.Text = " ";
                    TextBox12.Text = " ";
                    TextBox14.Text = " ";

                    Label1.Visible = true;
                    Label1.Text = "submitted successfully";
                }
                con.Close();

            }
            else
            {
                Label1.Visible = true;
                Label1.Text = " terms & condition must checked";
            }
        }
        catch (Exception e1)
        {
            Response.Write("<script language='javascript'>alert(\"Invalid Data Entry\")</script>");
        }

        }
    protected void  DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {

    if (DropDownList2.SelectedIndex == 1)
        {
            DropDownList3.Items.Clear();
            DropDownList3.Items.Add("jaipur");
            DropDownList3.Items.Add("udaipur");
            DropDownList3.Items.Add("ajmer");
            DropDownList3.Items.Add("bharatpur");
            DropDownList3.Items.Add("tonk");
        }
        else if (DropDownList2.SelectedIndex == 2)
        {
            DropDownList3.Items.Clear();
            DropDownList3.Items.Add("amravati");
            DropDownList3.Items.Add("mumbai");

        }
    }

    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

    }
    }

4 个答案:

答案 0 :(得分:1)

检查aspx页面上的Inherits属性 在Aphelion的回答中提到 并确保在aspx页面上正确关闭标签

答案 1 :(得分:0)

确保您的Aspx页面正确设置了CodeFileInherits属性。

.aspx的:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Register.aspx.cs" Inherits="register" %>

代码隐藏:

public partial class register : System.Web.UI.Page {}

答案 2 :(得分:0)

如果您在page指令中正确指定了文件后面的代码,但如果您遇到同样的问题,可能是您的页面设计器(register.aspx.designer.cs)搞砸了。您可以删除并重新创建它。

1)首先删除register.aspx.designer.cs

2)然后从解决方案资源管理器中右键单击Web项目并选择选项Convert to Web Application(这将重新创建desginer.cs;现在它应该可以正常工作)

答案 3 :(得分:0)

如果您从不同版本的vs复制元素并将其粘贴到您的应用中,则可能会遇到此问题。只需删除这些元素,然后使用vs。

中的工具箱将其添加回来