views:

72

answers:

3

I have GUI for data acceptance. I need to pass all the parameters of the form on click of a submit button to a function declared in C#. Please help.

A: 

If your using asp.net you just need to double click the button(if it is an asp button) and it should make a click event.

In the click event you could get your other controls like

default.aspx code

<%@ 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"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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>

Codebehind

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

Edit. You just make a class.

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

    }
}
chobo2
hey thank you i got my answer but is this the only way friendand how should be my function declaration???
Rookie
See my edit. For full examle
chobo2
hey i m sorry didnt mention i want that in c sharpm making an airlines reservation project taking in input of the user details via form1 sayi have a class say passengerdetails with class variables nowon click of the button i need to assign all those text box values to the class variables
Rookie
ic what will you do once you have them in the class? Ok see my post I made a class and added something that would be in a textbox to a class variable.
chobo2
A: 

Hi Abhijit

Using .Net us have too types of submit tags, one starting with <asp: and the other starting with <input. the <input html tag can call javascript and if you add the runat="server" attribute, you will enable it to also have C# code behind the button.

Yo Momma
hey friend i don't need for asp.net in c sharp i want to get the details entered in the text boxes into the variables of the class on click of a button on the GUI...
Rookie
A: 

First of all, you will need to create an aspx page (say submission.aspx) that will receive the POST submission of your form. In that page, you can include your .cs file that contains the method/function you want to pass the data to.

Next, you want to submit you submit your data to submission.aspx. To do that, you will need to have a form which will submit its data to submission.aspx.

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

If you want to perform ajax submission, you can use jquery and use this code:

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

I wonder if all that helped you.

PS: I haven't used .NET for web programming in a long time now.. but what I've written here in this answer hold universally true for any web programming language/framework.

Here Be Wolves