0

在我的设计部分,我添加了所有标签和下拉列表,但在 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

4 回答 4

1

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

于 2012-06-19T07:00:57.453 回答
0

如果您从不同版本的 vs 复制元素并将其粘贴到您的应用程序中,您可能会遇到此问题。只需删除这些元素并使用 vs 中的工具箱将它们添加回来。

于 2013-10-14T11:47:02.507 回答
0

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

ASP:

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

代码隐藏:

public partial class register : System.Web.UI.Page {}
于 2012-06-19T06:57:25.103 回答
0

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

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

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

于 2012-06-19T07:14:14.163 回答