Build your own ASP.NET - Take a survey

Hello all,

I’ve been going through the Build your own ASP.NET 4, and I have to say so far I am learning a good deal.
However I have got stuck on chapter 4 when you have to make a survey. I followed the code exactly as it shows in the book but when ever I run it I get the following error:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1061: 'ASP.survey_aspx' does not contain a definition for 'click' and no extension method 'click' accepting a first argument of type 'ASP.survey_aspx' could be found (are you missing a using directive or an assembly reference?)

Source Error:

 

Line 62:     <!-- Display confirmation button -->
Line 63:     <p>
Line 64:       [COLOR="#FF0000"]<button id="confirmButton" OnServerClick="click" runat="server">Confirm</button>[/COLOR]
Line 65:     </p>
Line 66:     <!-- confirmation label -->

At first I thought I had done something silly so I downloaded the original code from the website and tried that but I still got the same error. I’m not sure what to do next so can anyone help me out?

Thank you

Show your code. Survey.aspx and Survey.aspx.cs

Looks like you either misspelled protected void click(object sender, EventArgs e) or you forgot to put it in altogether. If you’re using C# it is case-sensitive.

servey.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 transitional //EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <script runat="server">
      void Click(Object s, EventArgs e)
      {
          feedbackLabel.Text = "Your name is: " + name.Value + "<br />";
          feedbackLabel.Text += "Your email is: " + email.Value +
              "<br />";
          feedbackLabel.Text += "You like to work with:<br />";
          for (int i = 0; i <= serverModel.Items.Count - 1; i++)
          {
              if (serverModel.Items[i].Selected)
              {
                  feedbackLabel.Text += " - " + serverModel.Items[i].Text +
                      "<br />";
              }
          }
          feedbackLabel.Text += "You like .NET: " + likeDotNet.Value;
      }
  </script>

  <html xmlns= "http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title>Using ASP.NET HTML Server Controls</title>
    </head>
    <body>
      <form id="form1" runat="server">
  <div>
    <h1>Take the survey!</h1>
    <!-- Display username -->
    <p>
     Name:<br />
     <input type="text" id="name" runat="server" />
    </p>
    <!-- Display email -->
    <p>
     Email:<br />
     <input type="text" id="email" runat="server" />
    </p>
    <!-- Display technology options -->
    <p>
     Which server technologies do you use?<br />
     <select id="serverModel" runat="server" multiple="true">
     <option>ASP>NET</option>
     <option>PHP</option>
     <option>JSP</option>
     <option>CGI</option>
     <option>ColdFusion</option>
     </select>
    </p>
    <!-- Display .NET preferences options -->
    <p>
     Do you like .NET so far<br />
     <select id="likeDotNet" runat="server">
       <option>Yes</option>
       <option>No</option>
     </select>
    </p>
    <!-- Display confirmation button -->
    <p>
      <button id="confirmButton" OnServerClick="click" runat="server">Confirm</button>
    </p>
    <!-- confirmation label -->
    <p>
      <asp:Label ID="feedbackLabel" runat="server" />
    </p>
  </div>
  </form>
  </body>
  </html>

survey.aspx.cs

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

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

    }
}

Hmm. So, where is the handler named “click” in survey.aspx.cs? It’s not there, that’s what the error is telling you, right?

There should be something like this in there:

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

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

    }
    protected void click(object sender, EventArgs e)
    {
        // handle click event here
    }
}

[/QUOTE]

the aspx.cs code is inside the .cs code, so the handler named click is inside the survey.cs file.
This:

<script runat="server"> 
      void Click(Object s, EventArgs e) 
      { 
          feedbackLabel.Text = "Your name is: " + name.Value + "<br />"; 
          feedbackLabel.Text += "Your email is: " + email.Value + 
              "<br />"; 
          feedbackLabel.Text += "You like to work with:<br />"; 
          for (int i = 0; i <= serverModel.Items.Count - 1; i++) 
          { 
              if (serverModel.Items[i].Selected) 
              { 
                  feedbackLabel.Text += " - " + serverModel.Items[i].Text + 
                      "<br />"; 
              } 
          } 
          feedbackLabel.Text += "You like .NET: " + likeDotNet.Value; 
      } 
  </script> 

Oh, I’m sorry. I didn’t see that. :slight_smile: I knew you could do that but I’ve never actually seen it done in practice. I’ve always seen the code behind used.

OK, now that I see what I should have seen, it is a case issue.
Change this:

<button id="confirmButton" OnServerClick="click" runat="server">Confirm</button>

to this:

<button id="confirmButton" OnServerClick="Click" runat="server">Confirm</button>

HAHA Thanks so much, I went over that code so many times. I can’t believe it was something so simple as that.

Yeah, it happens. Sometimes an extra eye or two on the code can reveal the issue. :slight_smile: