简体   繁体   English

当前上下文中不存在“名称”

[英]The 'name' doesn't exist in the current context

I'm working on a C#, ASP.NET website, and I've encountered a problem. 我正在使用C#,ASP.NET网站,但遇到了问题。 It's also using a SQL database for inserting purposes. 它还将SQL数据库用于插入目的。 What I'm experiencing is when I attempt to write code either in the aspx file using "code here" or in code-behind ie "page.aspx.cs", I still encounter this same problem. 我遇到的是,当我尝试使用“ here here”在aspx文件中或在“ page.aspx.cs”背后的代码中编写代码时,我仍然遇到相同的问题。

So, I've create a project as a "Web Form", and it includes the "Register/Identity" module, and I'm working on the "Register.aspx" page to add extra fields that I require, and on click to register I'm attempting to insert them into a different database. 因此,我已经创建了一个作为“ Web窗体”的项目,它包括“注册/身份”模块,并且我正在“ Register.aspx”页面上添加我需要的其他字段,然后单击注册,我正在尝试将它们插入其他数据库。

<script runat="server">
 protected void Unnamed20_Click(object sender, EventArgs e)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["Users"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        string sql;
        sql = "insert users (username, title, gname, sname, dob, address, suburb, state, postcode, phone, email) values (@Username, @Title, @Gname, @Sname, @Dob, @Address, @Suburb, @State, @Postcode, @Phone, @Email)";
        SqlCommand cmd = new SqlCommand(sql, con);

        cmd.Prepare();

        String suburb = txtSuburb.Text;


        cmd.Parameters.AddWithValue("@Username", RegisterUser.UserName);
        cmd.Parameters.AddWithValue("@Title", txtTitle.Text);

    }

Now the problem occurs where it says '@Title', txtTitle.Text "txtTitle.Text" is the problem, so for me It's called txtTitle.Text in my web page but when I'm trying to access it via code as above it gives me the error of this posts title. 现在问题出现了,它说“ @Title”,txtTitle.Text是“ txtTitle.Text”,所以对我来说,它在我的网页中称为txtTitle.Text,但是当我尝试通过上面的代码访问它时给我这个职位的错误。 It comes up underlined in red, and I don't know how to fix this problem. 它用红色下划线标出,我不知道该如何解决。

Here is the markup for the txtTitle control: 这是txtTitle控件的标记:

<li>
    <asp:Label runat="server" AssociatedControlID="Title">Title</asp:Label>
    <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtTitle" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
</li>

How can I fix this so I'm able to access these controls via code? 如何解决此问题,以便能够通过代码访问这些控件?

As turned out in the comments, txtTitle control lives inside the wizard control. 如注释所示, txtTitle控件位于向导控件内。 Such controls cannot be accessed directly, so you need to make use of FindControl : 此类控件无法直接访问,因此您需要使用FindControl

TextBox txtTitle = (TextBox)WizardControlIdHere.StepNameHere
                                               .ContentTemplateContainer
                                               .FindControl("txtTitle");
cmd.Parameters.AddWithValue("@Title", txtTitle.Text);

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

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