C#中的错误:当前上下文中不存在名称“控件名称”。 :( [英] Error in C#: The name 'control name' does not exist in the current context. :(

查看:132
本文介绍了C#中的错误:当前上下文中不存在名称“控件名称”。 :(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过asp.net c#编写一个网页。当我将文本框添加到我的注册页面时,我无法使用它。错误显示此文本:当前上下文中不存在名称控件名称。

如果你能帮助我,我会非常高兴:)

感谢您提前

 使用 System.Data.SqlClient; 



public partial class sendcomment:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
SqlConnection swehost = new SqlConnection(ConfigurationManager.ConnectionStrings [ RegistrationConnectionString]。ConnectionString);
swehost.Open();
string checkuser = select count(* )来自UserData,其中first_name =' + TextBox1 + ';
swehost.Close();
}
受保护 void Button2_Click( object sender,EventArgs e)
{

}
}



TextBox1有问题。

 <   asp:RequiredFieldValidator  < span class =code-attribute>   ID   =  RequiredFieldValidator1    runat   =   server    

< span class =code-attribute> < span class =code-attribute> < span class =code-attribute> ControlToValidate = TextBox1 ErrorMessage = 检查您的名字 > < / asp:RequiredFieldValidator >
名字
< asp:TextBox ID = TextBox1 runat = 服务器 > < / asp:TextBox >
< br / >
< asp:RequiredFieldValidator ID = RequiredFieldValidator2 runat = server < span class =code-attribute>

< span class =code-attribute> ControlToValidate = TextBox5 ErrorMessage = 检查您的姓氏 > < / asp:RequiredFieldValidator >
姓氏
< asp:TextBox ID = TextBox5 runat = s erver > < / asp :TextBox >
< br / >
< asp:RequiredFieldValidator ID = RequiredFieldValidator3 runat = server

< span class =code-attribute> < span class =code-attribute> ControlToValidate = TextBox6 ErrorMessage = 您的地址是必需的 > < / asp:RequiredFieldValidator >
地址
< asp:TextBox ID = TextBox6 runat = 服务器 > < / asp:TextBox >
< br / >
邮政编码
< asp:TextBox ID = TextBox7 runat = server > < / asp:TextBox >
< br / >
电话号码
< asp:TextBox ID = TextBox8 runat = server > < / asp:TextBox >
< br / >
< asp:RequiredFieldValidator ID < span class =code-keyword> = RequiredFieldValidator4 runat = server < span class =code-attribute>

< span class =code-attribute> < span class =code-attribute> ControlToValidate = TextBox2 ErrorMessage = 输入您的密码 > < / asp:RequiredFieldValidator >
密码
< asp:TextBox ID = TextBox2 runat = server > < / asp:TextBox >
< br / >
< asp:CompareValidator ID = CompareValidator1 runat = server

< span class =code-attribute> ControlToCompare = TextBox2 ErrorMessage = CompareValidator > < / asp:CompareValidator >
< asp:RequiredFieldValidator ID = RequiredFieldValidator5 runat = server

< span class =code-attribute> < span class =code-attribute> ControlToValidate = TextBox3 ErrorMessage = 确认密码 > < / asp:RequiredFieldValidator >
再次重复密码
< ; asp:TextBox ID = TextBox3 runat = server > < / asp:TextBox >
< br / >
< asp:RequiredFieldValidator ID = RequiredFieldValidator6 < span class =code-attribute> runat = server

< span class =code-attribute> ControlToValidate = TextBox4 ErrorMessage = 输入有效的电子邮件地址 > < span class =code-keyword>< / asp:RequiredFieldValidator >
电子邮件
< asp:TextBox ID = TextBox4 runat = 服务器 > < / asp:TextBox >
< br / >
< input id = Reset1 type = reset value = 重置 / >
< asp:Button ID = Button2 runat = server onclick = Button1_Click

< span class =code-attribute> < span class =code-attribute> 文字 = 立即加入 宽度 = 120px / >
< / div >

解决方案

首先不这样做:特别是网页!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。

如果您没有人,世界上任何地方都可以访问您的网站,只需在文本框中输入即可按注册并删除数据库...



其次,您需要使用控件的Text属性,而不是控件的名称才能访问用户输入的内容:

< pre lang =c#> ... name = ' + TextBox1 +' ...





第三,为什么要将注册码添加到sendcomment页面而不是注册页面?我只是猜测将你的代码放在正确的页面中会消除错误信息...





我是关于asp和C#的小学生。感谢您的提示。您能告诉我一课,了解我该怎么做而不是使用字符串?



尝试:

 使用(SqlConnection swehost =  SqlConnection(ConfigurationManager.ConnectionStrings [  RegistrationConnectionString]。ConnectionString))
{
swehost.Open();
使用(SqlCommand cmd = new SqlCommand( SELECT COUNT(*)FROM UserData WHERE first_name = @FN,swehost))
{
cmd.Parameters.AddWithValue ( @ FN,TextBox1.Text);
int count = cmd.ExecuteScalar();
...
}
}


I am coding a web page via asp.net c#. When I add textbox to my registeration page so I am not able to use it. An error show me this text: The name control name does not exist in the current context.
I become so much glad if you can help me :)
Thank you for advance

using System.Data.SqlClient;



public partial class sendcomment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection swehost = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
        swehost.Open();
        string checkuser = "select count(*) from UserData where first_name= '"+TextBox1+"'";
        swehost.Close();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        
    }
}


TextBox1 has problem.

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 

                                                           ControlToValidate="TextBox1" ErrorMessage="Check your First Name"></asp:RequiredFieldValidator>
                                                       First Name 
                                                       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                                                       <br />
                                                       <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 

                                                           ControlToValidate="TextBox5" ErrorMessage="Check your Last Name"></asp:RequiredFieldValidator>
                                                       Last Name 
                                                       <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
 <br />
                                                       <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 

                                                           ControlToValidate="TextBox6" ErrorMessage="Your address is required"></asp:RequiredFieldValidator>
                                                       Address 
                                                       <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
                                                       <br />
                                                       Postal code                                      
                                                       <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
                                                       <br />
                                                       Telephone number                        
                                                       <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
                                                       <br />
                                                       <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 

                                                           ControlToValidate="TextBox2" ErrorMessage="Enter your password"></asp:RequiredFieldValidator>
 Password  
                                                       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                                                       <br />
                                                       <asp:CompareValidator ID="CompareValidator1" runat="server" 

                                                           ControlToCompare="TextBox2" ErrorMessage="CompareValidator"></asp:CompareValidator>
                                                       <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 

                                                           ControlToValidate="TextBox3" ErrorMessage="Confirm your password"></asp:RequiredFieldValidator>
 Repeat your password again
                                                       <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                                                       <br />
                                                       <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" 

                                                           ControlToValidate="TextBox4" ErrorMessage="Enter a valid email address"></asp:RequiredFieldValidator>
 Email 
                                                       <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                                                       <br />
                                                       <input id="Reset1" type="reset" value="reset" />    
                                                       <asp:Button ID="Button2" runat="server" onclick="Button1_Click" 

                                                           Text="Join today" Width="120px" />
                                               </div>

解决方案

Start by not doing it that way: particularly with a web page! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
If you don't anyone, anywhere in the world can go to your site, press to register, and delete your database just by typing in the text box...

Secondly, you would need to use the Text property of the control, rather than the name of the control in order to get access to what the user typed:

...name= '"+TextBox1+"'...



Thirdly, Why are you adding your registration code to the "sendcomment" page instead of the "registration" page? I'm just guessing that putting your code in the right page will get rid of the error message...


"I am elementary about the asp and C#. Thanks for tips. can you show me a lesson to learn me what can I do instead of using string?"

Try:

using (SqlConnection swehost = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString))
    {
    swehost.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM UserData WHERE first_name= @FN", swehost))
        {
        cmd.Parameters.AddWithValue("@FN", TextBox1.Text);
        int count = cmd.ExecuteScalar();
        ...
        }
    }


这篇关于C#中的错误:当前上下文中不存在名称“控件名称”。 :(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆