簡體   English   中英

沒有runat =“ server”的ASP.NET

[英]ASP.NET without runat=“server”

我必須使用asp.net網絡表單創建一個簡單的網站,但是我必須不使用任何服務器控件,即runat="server"

我有以下幾點:

HTML

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

后面的代碼

protected void myFunction()
{
    // do something
}

我當前正在// do somethingprotected void Page_Load(object sender, EventArgs e)函數中// do something ,但是我想在單擊保存按鈕時調用它。 但是,如果不使用runat="server"我不知道如何執行此操作。 有沒有辦法做到這一點?

提前致謝

這個問題的真正答案在評論中:

使用webforms但不說runat="server"就像在說皮划艇,但不划槳。 聽起來更像是您應該使用ASP.NET MVC

我還將添加ASP.Net網頁以快速完成工作(注意:這並不意味着ASP.Net Web Pages 適用於“簡單”網站-您可以使用它來做任何事情)。

我必須使用asp.net web forms創建一個簡單的網站

但是由於它“必須”成為WebForms所以它仍然是可行的 這是明智的嗎? ,特別是具有上述選項以及有關SPA / Javascript / XHR其他注釋。

一天結束時, 它仍然是HTTP Requests, and Responses ,因此標准HTML表單輸入和類似其他“框架”中的工作:

  • “前端”(從技術上講, Page是一個control但是我們堅持使用WebForms因此這將是唯一的“服務器控件”):

    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> 
  • “后端”(后面的NoServerControls.aspx.cs代碼)

     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"]; } } } 

心連心。

蝙蝠俠 :)

我對此有一個有效的測試項目,請參閱此...

    <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>

WEB方法背后的代碼

[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;
    }

如果您不使用服務器控件來調用函數,則不能直接調用函數,您需要具有靜態功能的Web服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM