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