HTML page

  • Thread starter Thread starter Ramesh
  • Start date Start date
R

Ramesh

hi,
I have some basic questions. Can anybody explain me the
following doubt:

1. In my asp page, right click and selected HTML source
code.
Added following method.
<script language="javascript">
private void Page_Load(object sender, System.EventArgs
e)
{
alert('hi');
}
when i run the application, i am not getting the
message "hi" and it is not displaying any error message.
but, if righ client and added this source code in "View
code" then i am getting this message. why? If we write
Page_Load method in HTML source code, then it won't
execute??

2. What is the difference between writting the above
method in HTML source code and View Code. What is the
purpose of View code? any why i am not getting error
message or "hi" message?

Thanks,
Ramesh
 
Page_Load is a server side event. object sender and System.EventArgs make
no meaning for the client.

You would write things that happen on the web server in source code and
things that happen on the client side in html script.

Page_Load is run when a page is about to be sent to the client, but the
page isn't sent just yet, whereas script is run after the page is sent to
the client. As far as the server know, the page doesn't exist anymore and
is destroyed on the server (unless you take special precautions).

My guess is that you won't receive any error because the script function
just waits for anything calling Page_Load, but nothing will call this
function on the client.
 
hi,
Thanks for your reply. So, if i write the following
method in HTML source code, then it won't execute? and the
connection object wound't be initialised in
AddAuthor_Click event? is it correct? If so, how to do it
in the HTML source code page (instead of View code).

<script language="C#" runat="server">
SqlConnection myConnection;
protected void Page_Load(Object Src, EventArgs E)
{
// Create a connection to the "pubs" SQL database
located on
// the local computer.
myConnection = new SqlConnection("Server=Pluto;User
ID=sa;Password=sa;Database=pubs");
myConnection.Open();
// Check to see whether this page is a postback. If
it is not
// a postback, call a custom BindGrid function.
if (!IsPostBack) BindGrid();
}

public void AddAuthor_Click(Object sender, EventArgs E)
{
Command object initialisation code here...
myConnection.Open();
Exectuio statements goes here...
}

</script>

Thanks,
Ramesh
 
I'm not sure what happens if you put runat="server" in a script, but
I think you would want something like this in html code

<asp:Button id="AddAuthor" runat="server" Text="Add Author"></asp:Button>

and then have this in your webpage.aspx.cs file

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TestWebApp
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

protected void Page_Load(Object Src, EventArgs E) {
// Create a connection to the "pubs" SQL database // located on the
local computer.
myConnection = new SqlConnection("Server=Pluto;User
ID=sa;Password=sa;Database=pubs");
myConnection.Open();
// Check to see whether this page is a postback. If it is not // a
postback, call a custom BindGrid function.
if (!IsPostBack) BindGrid();
}
public void AddAuthor_Click(Object sender, EventArgs E) {
//Command object initialisation code here...
myConnection.Open();
//Exectuio statements goes here...
}

override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{ this.Load += new System.EventHandler(this.Page_Load);

}
}
}
 
Back
Top