Speed up development with full-stack environments for every branch.

Learn More

Submit a Form In ASP.NET

4294 Runs 43693 Views 7614 Copies
Saved

Saved

mirang 126

mirang
published 4 years ago

<%@ Page Language="C#" %>
 
<%@ Import Namespace="System.Collections.Generic" %>
<!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>
    <title>Submit a Form In ASP.NET</title>
    <script language="C#" runat="server">
        public void Page_Load(Object sender, EventArgs e)
        {
            //Print the time when the page loaded initially
            Response.Write("The Page Loaded at: " + DateTime.Now.ToLongTimeString());
            //For an ASP.NET form to  be submitted it is mandatory to have the form runat="server" otherwise it will not be submitted when a button is clicked
            //Also when you want to submit a form on button click, just attach the ServerClick event on the button, so that when the button is clicked
            //the form is submitted and the event is handled by the handler
            btnSubmit.ServerClick += new EventHandler(btnSubmit_ServerClick);
        }
        
        /// <summary>
        /// Button Click Event which is fired when the 'Save' button is clicked and the form is submitted
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnSubmit_ServerClick(object sender, EventArgs e)
        {
            divResultSubmitted.InnerHtml = "Value '<b>" + txtCount.Value + "</b>' Successfully Submitted !!!";
        }
    </script>
    <style type="text/css">
        body
        {
            background-color: #32373a;
            color: #FFFFFF;
        }
        #mainBody
        {
            background-color: #FFFFFF;
            height: 100%;
            color: #32373a;
        }
        #divResultSubmitted
        {
            background-color: #fdd136;
            width:350px;
        }
        .button
        {
          border-radius:4px 4px 4px 4px;
          height:30px;
          padding:5px;
          font-size:14px;
          background-color:#6ca21e;
          color:#FFFFFF;
          width:100px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id='mainBody'>
        <h1>
            Submit a Form in ASP.NET
        </h1>
        <br />
        <div id="divGETHTML" runat="server">Enter your number and click on <b>Save</b> to submit the value : 
        <input type="text" maxlength="20" id="txtCount" runat="server" /><input runat="server" type="button" value="Save" class='button' id="btnSubmit" /></div>

        <br /><br />

        <div id="divResultSubmitted" runat="server" />
    </div>
    </form>
</body>
</html>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).