PC Review


Reply
Thread Tools Rate Thread

Code Behind and functions

 
 
Gary
Guest
Posts: n/a
 
      26th Jan 2007
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.

 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      26th Jan 2007
"Gary" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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...?


 
Reply With Quote
 
Gary
Guest
Posts: n/a
 
      26th Jan 2007
> 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.

 
Reply With Quote
 
Gary
Guest
Posts: n/a
 
      26th Jan 2007
> 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.

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      26th Jan 2007
"Gary" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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.


You're using the wrong control - use an <asp:LinkButton> instead - looks
like hyperlink, functions like a button... :-)

> 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...


 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      26th Jan 2007
"Gary" <(E-Mail Removed)> wrote in message
news:e%(E-Mail Removed)...

> 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?


You need client-side JavaScript for that:
http://www.google.co.uk/search?sourc...pt+toUpperCase



 
Reply With Quote
 
Gary
Guest
Posts: n/a
 
      26th Jan 2007
> You're using the wrong control - use an <asp:LinkButton> instead - looks
> like hyperlink, functions like a button... :-)



Bingo! I love usent )

Much appreciated - thanks for your help.

Gary.
 
Reply With Quote
 
Gary
Guest
Posts: n/a
 
      26th Jan 2007
> You need client-side JavaScript for that:
> http://www.google.co.uk/search?sourc...pt+toUpperCase



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.

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      26th Jan 2007
"Gary" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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-D...e=UTF8&s=books


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert cell formula functions to code functions Adnan Microsoft Excel Misc 1 1st Oct 2008 08:30 PM
code instead of functions pls123 Microsoft Excel Programming 3 22nd Sep 2008 12:37 PM
Using Functions in VBA code =?Utf-8?B?Q2hyaXMgR29yaGFt?= Microsoft Excel Programming 2 15th Nov 2005 12:05 PM
not sure what functions/code to use? s&d Microsoft Excel Programming 6 8th Jul 2005 01:28 AM
How to call code in aspx webform from functions defined in code behind modules CW Microsoft ASP .NET 3 2nd May 2004 03:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:14 PM.