2010-01-14 232 views
-1

我有用於數據接受的GUI。 我需要將單擊提交按鈕時表單的所有參數傳遞給在C#中聲明的函數。 請幫忙。提交按鈕點擊

+1

你需要解釋一點*(更多地閱讀)*,並且展示一些我們可以幫助的代碼。 – 2010-01-14 05:54:58

+0

您是否使用ASP.NET,如果是這樣,它可能會有所幫助,如果你張貼一些代碼? – Kane 2010-01-14 05:55:42

+0

在我的GUI中,我已經提供了輸入用戶詳細信息,即姓名,地址,年齡的規定。我已經提供的表格上的按鈕,其上點擊應當分配在C尖銳私人無效btnSubmit_Click所有的細節在類 – Rookie 2010-01-14 05:57:37

回答

0

如果你使用asp.net,你只需要雙擊按鈕(如果它是一個asp按鈕),它應該發出一個點擊事件。

Click事件中,你可以得到你的其他控件像

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

    } 
} 
+0

嘿謝謝你,我得到了我的答案,但這是唯一的方法朋友 ,應該如何我的函數聲明??? – Rookie 2010-01-14 06:04:46

+0

看我的編輯。對於完整的檢查 – chobo2 2010-01-14 06:17:21

+0

嘿我很抱歉沒有提到我希望在c sharp m正在做一個航空公司預訂項目 通過表格1輸入用戶詳細信息說 我有一個班級說,與class變量的客戶詳細現在 點擊按鈕我需要將所有這些文本框值分配給類變量 – Rookie 2010-01-14 06:22:18

0

使用.NET我們有太多類型提交標籤,一個開始<asp:和其他開始<input。 <輸入html標記可以調用javascript,如果添加runat="server"屬性,則可以使其在按鈕後面也具有C#代碼。

+0

嘿朋友我不需要asp.net 在c sharp我想獲得在文本框中輸入的細節到類的變量單擊GUI上的按鈕... – Rookie 2010-01-14 06:14:14

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進行網絡編程了......但是我在這裏寫的這個答案在任何網絡編程語言/框架中都普遍適用。