1

I have created a ASP.NET Empty Website in a new install of Visual Studio 2017 Community. I am attempting to replicate the steps shown by the following tutorial: https://youtu.be/5dCAXwhjIYU

I'm simply planting three text boxes on the form, along with a submit button. When i double click the button, The IDE simply highlights the HTML for the button in the source view of the page. It does not create the event handler in the code-behind as shown in the video and as expected.

I manually created the event handler for the button click in the code behind. When I try to run the code, it is telling me that the fields I'm referencing do not exist:

Error CS0103 The name 'UserName' does not exist in the current context

Here's my HTML code:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            UserName<br />
            <input id="UserName" type="text" /><br />
            UserEmail<br />
            <input id="UserEmail" type="text" /><br />
            UserCampus<br />
            <input id="UserCampus" type="text" /><br />
            <input id="SaveUser" type="submit" value="submit" /></div>
    </form>
</body>
</html>

The C# code:

public partial class Users : System.Web.UI.Page
{
    protected void SaveUser_Click(object sender, EventArgs e)
    {
        SqlConnection BIGateConnection = new SqlConnection("Data Source=xxxxxxx;Initial Catalog=xxxxxxx;Integrated Security=True");
        {
            SqlCommand BIGateSQLCmd = new SqlCommand("Insert into [BIGateway].[dbo].[User] (UserName, userEmail, userCampus) VALUES (@userName, @userEmail, @userCampus)", BIGateConnection);
            BIGateSQLCmd.Parameters.AddWithValue("@", UserName.Text);
            BIGateSQLCmd.Parameters.AddWithValue("@", UserEmail.Text);
            BIGateSQLCmd.Parameters.AddWithValue("@", UserCampus.Text);
        }
    }
}

What the heck am I doing wrong?

2
  • Which line of code is being cited in the error message as the cause of the error? Commented Sep 8, 2017 at 18:53
  • I'm getting an error on each of the following lines: BIGateSQLCmd.Parameters.AddWithValue("@", UserName.Text); BIGateSQLCmd.Parameters.AddWithValue("@", UserEmail.Text); BIGateSQLCmd.Parameters.AddWithValue("@", UserCampus.Text); Specifically, it is complaining about the text box items UserName, UserEmail and UserCampus. Commented Sep 8, 2017 at 18:54

3 Answers 3

3

if you have taken HTML text box then code is as follows:

<input id="UserName" type="text" /><br />

In this you will have to add the line on your own as runat="server"

It will look as follows:

<input id="UserName" type="text"  runat="server"/><br />

Then you can use it for serverside.

Hope its helpful.

Sign up to request clarification or add additional context in comments.

6 Comments

Why wouldn't the input tags inherit the runat setting from the form tag?
The runat="server" attribute indicates that the form should be processed on the server. It also indicates that the enclosed controls can be accessed by server scripts:
Yes, I know what the tag does. I'm asking why <input id="UserName" type="text"> isn't already runat="server" by virtue of it being contained within the <form id="form1" runat="server"> tag.
@RobertHarvey That's just how ASP works. You'll have to ask them why they designed their controls that way. Do note however that for just about any ASP application your entire page will virtually always be in a server form, so you're proposing every single HTML element (in a practical sense) should have runat=server, which is more or less the same as just omitting it entirely and just assuming all elements should be server elements.
I'm glad I don't do ASP.NET anymore (I switched to ASP.NET MVC a long time ago and never looked back).
|
1

Video doesn't show the ASPX page. I believe he used TextBox and Button server controls.

<form id="form1" runat="server">
    <div>
        UserName<br />
        <asp:TextBox runat="server" ID="UserName"/><br />
        UserEmail<br />
        <asp:TextBox runat="server" ID="UserEmail"/><br />
        UserCampus<br />
        <asp:TextBox runat="server" ID="UserCampus"/><br />
        <asp:Button runat="server" ID="SaveUser" OnClick="SaveUser_Click" Text="Submit"/>
    </div>
</form>

If he used regular html input with runat="server", he will have to access the value as UserName.Value inside Button1_Click event which he did not.

Comments

0

Afais you didn't find the click event to the button. Add

SaveUser.OnClick += SaveUser_Click;

To the page_load event.

In general I would prefer asp:Button instead of input. In fact it's rendered as a input control. But with more options. But indeed runat="server" is missing for the input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.