Tuesday, 18 July 2023

IsPostBack Event in ASP.NET

IsPostBack Event in ASP.NET

IsPostBackEvent.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="IsPostBackEvent.aspx.cs" Inherits="ASPNETALL.IsPostBackEvent" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <strong>First Name :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong>
            <asp:TextBox ID="txtFNM" runat="server"></asp:TextBox>
            <br />
            <strong>Last Name :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:TextBox ID="txtLNM" runat="server"></asp:TextBox>
                <br />
                City :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong>
            <asp:DropDownList ID="ddlCity" runat="server" Height="16px" Width="116px">
            </asp:DropDownList>
            <br />
            <asp:Label ID="lblMsg" runat="server" Font-Bold="True" ForeColor="Red" Text="lblMessage"></asp:Label>
            <br />
            <asp:Button ID="btnReg" runat="server" OnClick="btnReg_Click" Text="Registration" />
        </div>
    </form>
</body>
</html>

IsPostBackEvent.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace ASPNETALL
{
    public partial class IsPostBackEvent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCityDropDownLIst();
            }
        }
        public void LoadCityDropDownLIst()
        {
            ListItem li1 = new ListItem("Uttar Pradesh");
            ddlCity.Items.Add(li1);
 
            ListItem li2 = new ListItem("Bihar");
            ddlCity.Items.Add(li2);
 
            ListItem li3 = new ListItem("Jharkhand");
            ddlCity.Items.Add(li3);
        }
        protected void btnReg_Click(object sender, EventArgs e)
        {
            lblMsg.Text = "Your Name= " + txtFNM.Text.Trim() + " " + txtLNM.Text.Trim() + " and City= " + ddlCity.SelectedItem.ToString() + "";
        }
    }
}

0 comments

Post a Comment