function samples

M

Mark Kurten

in the visual studio .NET samples that come with the product, why do
javascript functions all have the parameters below. Even though, the event
(like Onclick) which is calling it doesn't have any parameters.

private function somename(sender:Object, e:EventArgs)

thanks.
 
S

Showjumper

I belive you are confusing a server OnClick event with a client side onclick
event. The OnClick(s as object, e as eventargs) is for server side stuff.
 
S

Steven Cheng[MSFT]

Hi Mark,


Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, you'd like to know why there are some script
function in the page's source file and they have some parameters declared,
however, in the control's event attribute, the calling function don't take
any parameter such as:
private function somename(sender:Object, e:EventArgs)
{
......
}

and we use this fuction for a control 's "OnClick" event like:
<asp:Button id="btn" OnClick="somename" />

If there is anything I misunderstood, please feel free to let me know.

As for this question, I think it i because the different program style of
clientside script block and serverside code block:
1.Generally, if we develop the ASP.NET page in VS.NET, when we create a web
page, the IDE will help use create a code-hehind class file together with
the page. Then, we can write the serverside event handler, such as server
control's click even's handler in the code-behind class file(.aspx.cs or
..aspx.vb files). For example:
private void Page_Load(object sender, System.EventArgs e) // page's click
event handler
{
................

}

private void btnPrint_Click(object sender, System.EventArgs e) // a certain
button's click handler
{
................

}

these serverside event handlers normally all take two paramteres,one
(sender) is the source of the event, and the other (e, EventArgs or its
derived class) contains some infos of the event. When we add the handler to
a certain control's event's handler collection, we only need to provide the
function's name , not argument list needed, for example:

this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
this.Load += new System.EventHandler(this.Page_Load);

However, if we don't use the code-behind page class file, then we just
write these handler functions in the aspx file and in such code blocks as
below:
<script language="C#" runat="server">
.....
private void btnPrint_Click(object sender, System.EventArgs e) // a certain
button's click handler
{
................

}
</script>

and we also speify the handler for a ASP.NET server control in the file
like:
<asp:button id="btnPrint" runat="server" Text="Print"
OnClick="btnPrint_Click"></asp:button>
Just the function name, no parameter needed. Since the ASP.NET runtime will
help you to generate the "sender" and "e" object from the control
automaitcally.
For more detailed info on event handler in ASP.NET serverside , you may
view the following reference in MSDN:
http://msdn.microsoft.com/library/en-us/vbcon/html/vborieventhandlers.asp?fr
ame=true

#Creating Event Handlers in Web Forms Pages
http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskCreatingEventHandler
sInWebFormsPages.asp?frame=true


2. The function I discussed in 1. is the server side code in ASP.NET. In
addition, we can write client side script code such as javascript in the
aspx page source file, for example:
<script language="javascript">
function jsfn()
{
alert("hello world!");
}

function sayHello(name)
{
alert("hello world" + name);
}
</script>

But be careful, such client side script is quite different from the
serverside code, they only runat the client's browser. You can see the
<script ..> block doesn't have "runat=server". And the client side script's
function could also take paramter or doesn't take parameter. I've shown the
two condition in the above example.
If you need more information on clientside scripting, you may view the
following link for reference:

#javascript overview and examples
http://msdn.microsoft.com/library/en-us/dninvbs/html/javascript.asp?frame=tr
ue


Please check out the above items to see whether they help. If you need any
further assistance, please feel free to let me know.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top