Web controls are not recognized in code

I am working on my first website (using twitter oAuth) through Visual Studio 2012 for the class. I feel idiotic about having to ask this question, because I am sure it is very simple.

I'm having trouble getting code to recognize any web elements on my .aspx page. I'm not sure if I need to change something in the settings or if I just missed something.

I added a text box through the toolbar to "index.aspx" and gave it id = 'txtUserName'. When I try to assign txtUserName.Text to a variable in page_load, I get an error in which "txtUserName" does not exist in the current context.

Hope someone can make me pat my head and that I missed a simple tag or something like that.

index.aspx:

<%@ Page Language="C#" AutoEventWireup="True" CodeBehind="index.aspx.cs" Inherits="TwitterTest.index" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>TwitterTest!!!</title> </head> <body> <form id="form1" runat="server"> <div> Welcome to TwitterTest </div> <asp:TextBox ID="txtUserName" runat="server" Text=""></asp:TextBox> </form> </body> </html> 

index.aspx.cs:

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Mvc; using TweetSharp; namespace TwitterTest { public partial class index : System.Web.UI.Page { static void Page_Load(object sender, EventArgs e) { string UserName = txtUserName.Text; } } } 
+4
source share
4 answers

Your Page_Load must be protected void Page_Load

+2
source

What kind do you use in VS2012? you can try flipping from a code view to a designer view to refresh the pagedesigner.cs page. Also double check designer.cs to make sure your control really exists.

+1
source

Right-click on the page and select Convert to Web Application .

+1
source

Delete the main page and add the wizard again (and copy the existing content, excluding the page directive), then the constructor page will also be added and the problem will be solved.

0
source

Source: https://habr.com/ru/post/1469397/


All Articles