Code Behind and functions

G

Gary

Hello,

I am currently new to .NET, as you can probably tell by my last two or three
threads!!!

Anyway, I am using code behind as much as possible - and am curious about
the way in which functions can be triggered.

It seems if I use an <asp:button...> control, I can assign a function which
exisits on my .CS file, for example:
<asp:button... OnClick="Button_Click">

However, this function is not available on for example a standard <A> tag:
<A href="#" OnClick="Button_Click" runat="server" />

I am *guessing* this is because the button submits the .net form, which
forces the page to read the CS file again, which is when it can execute the
various functions?

My question is, is it possible or sensible to try and access these functions
without using a form button?

Regards,

Gary.
 
M

Mark Rae

I am currently new to .NET, as you can probably tell by my last two or
three
threads!!!

Anyway, I am using code behind as much as possible - and am curious about
the way in which functions can be triggered.

It seems if I use an <asp:button...> control, I can assign a function
which
exisits on my .CS file, for example:
<asp:button... OnClick="Button_Click">

However, this function is not available on for example a standard <A> tag:
<A href="#" OnClick="Button_Click" runat="server" />

Each webcontrol implements its own set of "events" for want of a better
term - lots of controls have very similar events. However, server-side code
can include other methods which these events can call. E.g. you could write
a server-side method which added a record to a database every time a control
initiated a postback - a pretty pointless thing to do - but you wouldn't
need to write the database code in every event - each event would simply
call the method...
I am *guessing* this is because the button submits the .net form, which
forces the page to read the CS file again, which is when it can execute
the
various functions?

Sort of... ASP.NET adds a couple of hidden fields to the Request.Form
collection which tell it which control initiated the postback, and which
event to run.
My question is, is it possible or sensible to try and access these
functions
without using a form button?

Using the traditional model (i.e. not callbacks via AJAX etc), server-side
code is *only* available through a postback, and a control needs to be
contained within a <form> object for it to be able to initiate a postback...

I appreciate you're new to ASP.NET, so maybe you can describe a bit more
about what you're trying to achieve...?
 
G

Gary

I appreciate you're new to ASP.NET, so maybe you can describe a bit more
about what you're trying to achieve...?

Well one example is I want to do a Post Code lookup. I have a server side
function in my Cod Behind file called FindAddress

I currently have an <ASP:Button... onClick="FindAddress" /> which does the
trick, however, I would like to instead use a hyperlink with the words
"Click here to search for an address" - which triggers the same behavior.

When I try and have an onClick="FindAddress" - it seems to be looking for a
client side javascript function which doesnt exist.

Cheers,

G.
 
G

Gary

I appreciate you're new to ASP.NET, so maybe you can describe a bit more
about what you're trying to achieve...?


Another example would be clicking a link which converts the contents of a
Textfield to all caps....in code behind I can use PostCode.Text.ToUper() -
but I have no way of accessing this without a post back?

Gary.
 
M

Mark Rae

Well one example is I want to do a Post Code lookup. I have a server side
function in my Cod Behind file called FindAddress

I currently have an <ASP:Button... onClick="FindAddress" /> which does the
trick, however, I would like to instead use a hyperlink with the words
"Click here to search for an address" - which triggers the same behavior.

When I try and have an onClick="FindAddress" - it seems to be looking for
a client side javascript function which doesnt exist.

Yes, it will do...
 
G

Gary

You're using the wrong control - use an said:
like hyperlink, functions like a button... :)


Bingo! I love usent :eek:)

Much appreciated - thanks for your help.

Gary.
 
G

Gary

You need client-side JavaScript for that:


Excellent, i did this:
<asp:TextBox ID="PostCode" runat="server" CssClass="PostCode"
onKeyUp="javascript:this.value=this.value.toUpperCase();"></asp:TextBox>

This changes your case as you type it, I will add an onSubmit as well to
catch anyone who enters the post code without a keyboard, such as copy and
paste via the mouse, with an "enter button" subit.

Works in IE 7.0 & FF 2.0 - will check earlier browsers later.

G.
 
M

Mark Rae

Excellent, i did this:
<asp:TextBox ID="PostCode" runat="server" CssClass="PostCode"
onKeyUp="javascript:this.value=this.value.toUpperCase();"></asp:TextBox>

This changes your case as you type it, I will add an onSubmit as well to
catch anyone who enters the post code without a keyboard, such as copy and
paste via the mouse, with an "enter button" subit.

Works in IE 7.0 & FF 2.0 - will check earlier browsers later.

According to Danny Goodman's "Dynamic HTML - The Definitive Reference",
onkeyup is supported from version 4 browsers, so you should be fine.
http://www.amazon.com/Dynamic-HTML-..._bbs_sr_1/002-6316549-9361614?ie=UTF8&s=books
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top