开发者

submit button click

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


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.


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.


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

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

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

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜