Use below code to raise messages from javascript and code behind.
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RaisingMessages.aspx.cs"
Inherits="RaisingMessages" %>
<!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>Validation</title>
<script type="text/javascript">
function ValidatePage() {
var fName = document.getElementById("txtFirstName");
var lName = document.getElementById("txtLastName");
if (fName.value == "") {
//client side alert message
alert('First Name should not be empty');
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblLastName" runat="server" Text="Last Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClientClick="return ValidatePage();" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
.ASPX.CS
using System;
using System.Web.UI;
public partial class RaisingMessages : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtLastName.Text.Trim()))
{
string script = "alert('Last Name should not be empty')";
Type csType = GetType();
// Server side alert message
ScriptManager.RegisterStartupScript(Page, csType, "popuscript", script, true);
return;
}
}
}
No comments:
Post a Comment