简体   繁体   中英

ASP.NET without runat=“server”

I have to create a simple website using asp.net web forms, but I'm required to not use any server controls ie runat="server"

I have the following:

HTML

<form method="post" action="">
    <label for="name">Name</label>
    <input id="name" name="name" type="text" />
    <input value="Save" type="submit" />
</form>

Code behind

protected void myFunction()
{
    // do something
}

I'm currently putting // do something in the protected void Page_Load(object sender, EventArgs e) function, but I would like to call it when the save button is clicked. However I don't know how to do this without using runat="server" . Is there a way of achieving this?

Thanks in advanced

The real answer to this question is in the comment:

Using webforms but saying no runat="server" is like saying go kayaking, but no paddles. It sounds more like you should be using ASP.NET MVC

I'll add ASP.Net Web Pages as well for getting things done quickly (note: this doesn't mean ASP.Net Web Pages are only for "simple" sites - you can do whatever you want with it).

I have to create a simple website using asp.net web forms

But since it "has to" be WebForms it's still doable . Is it advisable? nope - particularly with aforementioned options as well as other comments on SPA / Javascript / XHR .

End of day, it's still HTTP Requests, and Responses , so standard HTML form inputs and such work just like in any other "framework":

  • the "front end" (well, Page is technically a control but we're sticking to WebForms so this will be the only "server control"):

    NoServerControls.aspx

     <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NoServerControls.aspx.cs" Inherits="WebForms.NoServerControls" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Humor Me Batman</title> </head> <body> <form method="post"> <input type="text" name="wtf"/> <input type="submit" value="Batman"/> </form> <h1>It's "classic ASP" Batman! <%= echo %></h1> </body> </html> 
  • the "back end" ( NoServerControls.aspx.cs code behind)

     public partial class NoServerControls : System.Web.UI.Page { public string echo { get; set; } protected void Page_Load(object sender, EventArgs e) { //Trivial example: skipping all validation checks //It's either a GET or POST end of day if (Request.RequestType == "POST") { //Do something with data, just echoing it here echo = Request["wtf"]; } } } 

Hth.

Batman :)

I have a working test project on this please refer this...

    <table>
                <tr>
                    <td>Name </td>
                    <td>
                        <input type="text" id="custid" class="form-control custname" name="fullname" required />
                    </td>
                </tr>
                <tr>
                    <td>Designation </td>
                    <td>
                        <select id="loading" class="form-control loading">
                            <option value="0">Select </option>
                            <option value="HR">HR </option>
                            <option value="Engg">Engg </option>
                            <option value="Doctor">Doctor </option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Mobile No. </td>
                    <td>
                        <input type="text" id="mobile" class="form-control mobile" onkeypress="return event.charCode >=48 && event.charCode <= 57" name="fullname" required />
                    </td>
                </tr>
                <tr>
                    <td>Email Id </td>
                    <td>
                        <input type="text" id="emailid" class="form-control emailid" name="fullname" required />
                    </td>
                </tr>
                <tr>
                    <td colspan="2" id="btn">
                        <button type="button" onsubmit="return validateForm()" class="btn btn-primary">Save</button>
                    </td>
                </tr>
            </table>
<script>
   $(document).ready(function () {
            $('#btn').click(function () {
                var CustNamevalidate = $('.custname').val();
                if (CustNamevalidate != '') {
                    Name = $(".custname").val();
                    Loading = $(".loading").val();
                    Mobile = $(".mobile").val();
                    EmailId = $(".emailid").val();

                    $.ajax({
                        type: "POST",
                        url: "test.aspx/Complextype",
                        data: JSON.stringify({
                            Nam: Name, Loadin: Loading, Mobil: Mobile, EmailI: EmailId
                        }),
                        contentType: "application/json; charset=utf-8",
                        datatype: "json"
                    }).done(function (result) {
                        console.log(result);
                        alert(JSON.stringify(result));
                    })
                }
                else {
                    alert('Please Enter Customer Name');
                }
            });
        });



    </script>

Code Behind WEB MEthod

[WebMethod]
    public static string Complextype(string Nam, string Loadin, string Mobil, string EmailI)
    {
        string Qtets = "Details are : Name =" + Nam + " And Designation is =" + Loadin + " And Mobileno=" + Mobil + " And EmailI=" + EmailI;
        //  ScriptManager.RegisterStartupScript(Page, typeof(Page), "test", "<script>alert('Sorry This Category Name Already Exist.');</script>", false);

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Constr"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("usp_add_upd_emptb", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@EmpName", Nam);
            cmd.Parameters.AddWithValue("@EmpNo", Mobil);
            cmd.Parameters.AddWithValue("@Desig", Loadin);
            cmd.Parameters.AddWithValue("@Email", EmailI);
            cmd.Parameters.AddWithValue("@id", 0);

            con.Open();
            cmd.ExecuteNonQuery();
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            else
            {
                con.Open();
            }
        }
        //SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Constr"].ConnectionString);
        //{
        //}

        return Qtets;
    }

you Can not call Function Directly if you are not using server controls for function to be called you need to have Web service with static function.

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