sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
Actually here is an example with an alert on a server side control. This will cause an alert then a postback:
Page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Alerter.aspx.cs" Inherits="daniweb.web.Alerter" %>
<!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:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
Code behind:
using System;
namespace daniweb.web
{
public partial class Alerter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "javascript:alert('hello');");
}
}
}
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
hey sknake,can u tell me one thing,that instead of writing Button1.Attributes.Add("onclick", "javascript:alert('hello');"); on button click event,Y we write it in Page Load event!
What?
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
ya i know that we should use alert function to display client side script in asp.net
but
i have msgbox function in my website and when I RUN the webpage it gives no error ..it works fine... ..
I want to know will that will it problems in real time deployment..??
I my PC that page runs well with MSGBOX function... but will it cause problems in realtime when website will be deployed to SERVER?
I don't know the answer to that -- go ahead and test it out. The MsgBox() call lies in the System.VisualBasic namespace and I do my best to avoid that namespace for compatibility reasons.
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
i agree with snake...
but i'm curious why do u need a feature like MessageBox..?
or there is another way but it's a long way..
dnanetwork
Practically a Master Poster
633 posts since May 2008
Reputation Points: 28
Solved Threads: 106
The form_load event fires server side to create the page -- not when the form loads at the browser end. Its just adding a client side event to the button -- the asp:button's click event is normally server side.
Is this what you are asking?
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
Create a class and add these two methods to it :
// for postback events
public static void ShowMessageBox(string _message)
{
Page page = HttpContext.Current.Handler as Page;
page.RegisterStartupScript("key2", "<script>alert('" + _message + "');</script>");
}
// for async postback events
public static void ShowMessageBoxForAsync(string _message)
{
Page page = HttpContext.Current.Handler as Page;
ScriptManager.RegisterClientScriptBlock(page,page.GetType(), "key2", "alert('" + _message + "');", true);
}
// Then you can call this in, let's say , button_click event handler like this ( i assume that your class name is Utilities without any namespace definition) :
protected void myButton_Click(object sender, EventArgs e)
{
Utilities.ShowMessageBox("you entered a wrong password");
}
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127