0

This is driving me insane. I want to do something simple but have no idea because I am an absolute beginner. I want to take whats in the text boxes, and put them into 1 label, then make that label visible. I keep getting this error no matter what I try, "The name 'submittedData' does not exist in this context. What am I doing wrong?

Here is the code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void submit_Click(object sender, EventArgs e) 
{
    submittedData.Text = name.Text + " " + email.Text + " " + phone.Text;
    submittedData.Visible = true;
}
}

And the aspx:

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

 <!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link rel="stylesheet" type="text/css" href="StyleSheet.css"></link>

<title>Coding Club</title>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <h1>Coding club registration form</h1>

        <label for="name">Your Name:</label>
        <asp:TextBox ID="name" runat="server" TextMode="SingleLine"></asp:TextBox>
    </div>

    <div>
        <label for="email">Your Email:</label>
        <asp:TextBox ID="email" runat="server" TextMode="SingleLine"></asp:TextBox>
    </div>

    <div>
        <label for="phone">Your Phone:</label>
        <asp:TextBox ID="phone" runat="server" TextMode="SingleLine"></asp:TextBox>
    </div>
    <div>
        <asp:Button ID="submit" runat="server" Text="Submit" onclick="submit_Click" />

        <asp:Button ID="clear" runat="server" Text="Clear Form" />
    </div>
    <div>
        <label for="submittedData" runat="server"></label>
    </div>

</form>
</body>
</html>
1
  • 1
    You don't have an id on the last label, which seems to be the one you are targeting. You have a 'for' attribute. Commented Sep 9, 2018 at 21:19

2 Answers 2

1

You are missing id attribute on label element in aspx file

<label id="submittedData" runat="server"></label>
Sign up to request clarification or add additional context in comments.

Comments

0

You can change your code like below:

<asp:Label id="submittedData" runat="server"></asp:Label>

Or asp:literal.

As your current label isnt a asp server control, you maynot set it via the label for thing.

Your underlying html will still generate <label for> tag.

Also I notice you have used:

<label for="name"> next to your text input box.

You can use <asp:Label id=“labelName” AssociatedControlId=“name” runat=“server”>

This way if you click on label, your textbox will get focus

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.