Content

  • Client-side validation using Javascript
  • Master Pages in asp.net
  • IsPostBack - Page Property

Client-side validation using Javascript

Example : JS Function to validate ASP.NET Webform
function valid() {
var name = document.getElementById('<%= this.txtname.ClientID %>').value;
var email = document.getElementById('<%= this.txtemail.ClientID %>').value;
var phone = document.getElementById('<%= this.txtmobile.ClientID %>').value;
var city = document.getElementById('<%= this.txtcity.ClientID %>').value;
mobilecon = /^\d{10}$/;
emailcon = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([com\co\.\in])+$/;
if (name == "" || email == "" || phone == "" || city == "") {
    swal("Please fill all details to proceed..!", "", "warning");
    return false;
}
if (phone != '') {
    if (!phone.match(mobilecon)) {
        swal("Please Enter Valid Contact Number", "", "info");
        return false;

    }
}
if (email != '') {
    if (!email.match(emailcon)) {
        swal("Please Enter Valid Email-Id", "", "info");
        return false;
    }
}
                        
        return true;
}

Master Pages in asp.net

  • ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application.
  • You can then create individual content pages that contain the content you want to display.
  • When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page.

Add Master Page from Add New Item --> Master Page then we can add child pages from Add New Item-->Web form with Master Page


IsPostBack - Page Property

IsPostBack Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback.

It returns boolean value-true if the page is being loaded in response to a client postback; otherwise, false.

Example :

We need to bind values to city dropdown list on page load

Frond-End code
     <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>IsPostBack Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="margin-top:50px">
            <h3>IsPostBack Example</h3>
        <asp:Button runat="server" OnClick="btnrefresh_Click" ID="btnrefresh" Text="Postback Request" />
            <br />
            <asp:DropDownList runat="server" style="margin-top:15px" ID="ddlcity"></asp:DropDownList>
        </div>
        </form>
    </body>
    </html>
Back-End code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
    
    namespace Training
    {
        public partial class ispostback : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Response.Write("Page is loaded for first time..!");
                    bindddl();
                }
    
            }
    
            protected void bindddl()
            {
                ListItem li1 = new ListItem("Kolhapur");
                ddlcity.Items.Add(li1);
    
                ListItem li2 = new ListItem("Pune");
                ddlcity.Items.Add(li2);
    
                ListItem li3 = new ListItem("Mumbai");
                ddlcity.Items.Add(li3);
    
                ListItem li4 = new ListItem("Delhi");
                ddlcity.Items.Add(li4);
            }
    
            protected void btnrefresh_Click(object sender, EventArgs e)
            {
    
            }
        }
    }