简体   繁体   中英

C# Web Form Processing

I'm rather new to using C#, and I've run into an issue with using C# to handle form data once a button is clicked. Basically what I am trying to do is capture the user's information once the form button is clicked, assign it to a session and then display it on another page. Here is my C# code:

using System;
using System.Web.UI;

public partial class Data : Page
{
    protected void btnSubmit(object sender, EventArgs e)
    {
        string name = Request.Form["username"];

        if (string.IsNullOrEmpty(name))
        {
            // string usr_err = "You did not supply a username???";
            return;
        }

        // make the string uppercase
        string upperCase = name.ToUpper();

        string greeting = "Welcome " + upperCase + "! Did you know that October is called Oktober in German?";

        Session["username"] = upperCase;
        Session["greeting"] = greeting;

        Response.Redirect("test.aspx", false);
    }
}

And this is my html (login.apsx):

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

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Login</title>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="styles.css">
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="bootstrap-hover-dropdown.js"></script>

    <style type="text/css">
        body {
            background-color: #fff;
        }

        .form-control {
            border: thin solid #000;
        }

        #main-col {
            border-radius: 20px;
            border: thin solid #000;
            padding: 20px;
            box-shadow: 10px 10px 5px rgba(0,0,0,1);
            color: rgba(50, 99, 149, 1);
        }
    </style>
</head>

<body>
    <div class="navbar navbar-inverse navbar-fixed-top" data-role="navigation">
        <div class="container-fluid">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
                <span class="sr-only">Toggle Navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
        </div>

        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-left">
                <li><p class="navbar-text navbar-left" style="color: #000;"><b> Login</b></p></li>
            </ul>


        </div>
    </div>

    <div class="container-fluid">
        <div class="row-fluid">
            <div class="col-md-12">
                <p class="text-center"><br><br><br>Manipulating data server side<br><br><br></p>
            </div>
        </div>

        <div clas="row-fluid">
            <div id="wrapper">
                <div id="sidebar-wrapper">
                    <ul class="sidebar-nav">
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                    </ul>
                </div>
            </div>

            <div class="col-md-6 col-md-push-3" id="main-col">
                <form id="login" runat="server">
                    <div class="form-group">
                        <label for="username">Username</label>
                        <input type="text" class="form-control" name="username" id="username" placeholder="Enter username here" runat="server" />
                    </div>

                    <div class="form-group">
                        <label for="password">Password</label>
                        <input type="password" class="form-control" name="password" id="password" placeholder="Enter password here" runat="server" />
                    </div>


                    <div class="form-group">
                        <asp:Button id="btnSubmit" Text="Submit" OnClick="btnSubmit" runat="server" />

                    </div> 
                </form>
            </div>
        </div>
    </div>
</body>
</html>

I am running into this compilation error though and I have no idea why:

Line 2:  using System.Web.UI;

Line 3:  

Line 4:  public partial class Data : Page

Line 5:  {

Line 6:      protected void btnSubmit(object sender, EventArgs e)

(Line 4 is where the error occurs). 

If this helps, here is some more details about it via the compiler:

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: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (eg Page or UserControl)

Any help would be appreciated,

Thanks!

Look at the error message as below, it correctly states what's the problem.

Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (eg Page or UserControl)

Your Inherit attribute which is currently CodeFile="Data.cs" Inherits="CS" should be pointed to Data as well like

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

(OR) if you have defined a namespace then qualify with that namespace like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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