ASP.NET没有runat ="服务器"

时间:2017-11-24 17:12:41

标签: c# asp.net webforms

我必须使用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 something放入protected void Page_Load(object sender, EventArgs e)功能,但我想在点击保存按钮时调用它。但是,我不知道如何在不使用runat="server"的情况下执行此操作。有没有办法实现这个目标?

先谢谢

2 个答案:

答案 0 :(得分:2)

此问题的真实答案在评论中:

  

使用webforms但不说runat="server"就像是说划皮划艇,但没有划桨。这听起来更像是你应该使用ASP.NET MVC

我还要添加bezierCurveTo()以便快速完成工作(注意:此意味着ASP.Net Web Pages for&#34;简单&#34;网站 - 你可以随心所欲地做任何事情。)

  

我必须使用asp.net web forms

创建一个简单的网站

但是因为它&#34;必须&#34; WebForms它仍然可行。是建议吗? nope - 特别是上述选项以及SPA / Javascript / XHR上的其他评论。

一天的结束,它仍然是HTTP Requests, and Responses ,所以标准的HTML表单输入和其他工作就像在任何其他&#34;框架&#34;:

  • &#34;前端&#34; (好的,Page在技术上是control,但我们仍然坚持WebForms所以这将是唯一的服务器控件&#34;):

    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>
    
  • &#34;后端&#34; (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"];
            }
    
        }
    }
    

H个。

蝙蝠侠:)

答案 1 :(得分:0)

我有一个有效的测试项目请参考这个......

package application;

    import java.io.IOException;
    import java.net.URL;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.ResourceBundle;

    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.scene.Node;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.control.cell.PropertyValueFactory;
    import javafx.stage.Stage;

        public class SortareDupaAngajatiController implements Initializable{
            final ObservableList<tableAngajati> data=FXCollections.observableArrayList();
            final ObservableList<String> optiuni=FXCollections.observableArrayList();
            PreparedStatement intabel=null;
            ResultSet rezultate=null;
            String prequery;
            String postquery;

             @FXML
             public ComboBox<String> meniu;
             @FXML
             TableView<tableAngajati> tabelangajati;

             @FXML
             private TableColumn<tableAngajati,String> numesediucol;

             @FXML
             private TableColumn<tableAngajati,Integer> angajaticol;

            @Override
            public void initialize(URL location, ResourceBundle resources) {

                comboMeniu();
                insertInTabel();


            }


            public void comboMeniu(){
                try {
                    String query = "SELECT * FROM maginfo";
                    PreparedStatement dindb = Conexiune.conexiuneBd.prepareStatement(query);
                    ResultSet rezultate = dindb.executeQuery();


                    while (rezultate.next()) {
                        optiuni.add(rezultate.getString("nume"));
                        meniu.setItems(optiuni);
                    }
                    dindb.close();
                    rezultate.close();   
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
            }

            public void insertInTabel(){

        try {
                    prequery="SELECT * FROM sediu where nume='"+meniu.getValue()+"'ORDER BY numarangajati DESC";
                    postquery=prequery;
                    intabel= Conexiune.conexiuneBd.prepareStatement(postquery);
                    rezultate=intabel.executeQuery();

                    while(rezultate.next()){
                        data.add(new tableAngajati(
                                rezultate.getString(2),
                                rezultate.getInt(7)
                                ));


                    }

                    intabel.close();
                    rezultate.close();

                } catch (Exception e) {
                    System.err.println(e);
                }

                numesediucol.setCellValueFactory(new PropertyValueFactory<>("numesediu"));
                angajaticol.setCellValueFactory(new PropertyValueFactory<>("numarangajati"));
                tabelangajati.setItems(null);
                tabelangajati.setItems(data);

            }}

WEB MEthod背后的代码

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

如果您没有使用服务器控件来调用函数,则无法直接调用Function您需要具有静态函数的Web服务。