-1

我有用于数据接受的 GUI。我需要在单击提交按钮时将表单的所有参数传递给在 C# 中声明的函数。请帮忙。

4

3 回答 3

0

如果您使用 asp.net,您只需要双击按钮(如果它是 asp 按钮),它应该会产生一个点击事件。

在点击事件中,您可以获得其他控件,例如

default.aspx 代码

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

代码隐藏

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    // you can declare it as a field variable so the entire code behind can use it
    private Passengerdetails myClass;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      // create an instance of the class.
       myClass = new Passengerdetails ();
       // stick textbox1 contents in the property called test.
       myClass.PassengerName = TextBox1.Text;


       int a =  Convert.ToInt32(TextBox1.Text);
       int b = Convert.ToInt32(TextBox2.Text);
       int sum = Add(a, b);

       // do something with it like return it to a lbl.
       Label1.Text = sum.ToString();
    }

    private int Add(int a, int b)
    {
        return a + b;
    }
}

编辑。你只需要上课。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for passengerdetails 
/// </summary>
public class Passengerdetails 
{
    public Passengerdetails ()
    {

       public string PassengerName{ get; set; }

    }
}
于 2010-01-14T05:57:42.170 回答
0

使用 .Net,我们有太多类型的提交标签,一种以 .Net 开头,<asp:另一种以 .Net 开头<input。<input html 标记可以调用 javascript,如果您添加该runat="server"属性,您将使其在按钮后面也有 C# 代码。

于 2010-01-14T06:02:47.997 回答
0

首先,您需要创建一个 aspx 页面(比如submission.aspx),它将接收您的表单的 POST 提交。在该页面中,您可以包含.cs包含要将数据传递到的方法/函数的文件。

接下来,您要提交您提交的数据到submission.aspx. 为此,您需要有一个将其数据提交到的表单submission.aspx

<form action='submission.aspx' method='POST' id='data-submission'>
    <!-- stuff here -->
</form>

如果要执行 ajax 提交,可以使用 jquery 并使用以下代码:

$('#data-submission').submit(function(evt){
    var $form = $(this);
    var url = $form.attr('action');
    $.post(url, $form.serialize(), function(){alert('submission complete!);});
});

我想知道这一切是否对您有所帮助。

PS:我已经很长时间没有使用 .NET 进行 Web 编程了。但是我在这个答案中所写的内容对于任何 Web 编程语言/框架都是普遍适用的。

于 2010-01-14T06:19:49.937 回答