Stopping the Server Side code from running ?

J

Jon Delano

Hi

I was wondering if it were possible to somehow stop a page from posting back
to the server and running the server side code.
I have a datagrid and the first column is basically a button.

I have added a javascript function to the button :

Dim btnView As LinkButton = e.Item.Cells(0).Controls(0)
btnView.Attributes.Add("onclick", "CheckDictationMode();")

here is the javascript function

function CheckDictationMode() {
if (document.all('lblDictationMode').innerHTML!='') {
alert('Please finish or cancel your edit/creation of the current
dictation before selecting a new patient.');
return false;
}
else {
DisplayDataRequestMessage();
}
}

In the javascript function, I check the contents of a hidden field, if the
field doesn't have what I like then I display a message to the user and I
don't want the codebehind code to run (there is server side code that is
executed when the grid is clicked)

Currently I do get the message, but the form still gets submitted to the
server.

I found a bunch of stuff about simply returning false. I tried having the
onSubmit event of the form run the CheckDictationMode function using
onSubmit="return CheckDictationMode()". That never even seemed to fire at
all as I never received the message.

This is a Visual Studio 2003 web application using vb.net.
Also, this application is an inhouse deal, and only runs in IE .. so if
there is a VBScript alternative. I am all ears.

Any help would be greatly appreciated !!!!!

Thanks
Jon
 
G

Guest

This is "pseudocode" but the pattern would be something like this:



function CheckDictationMode()
{
if (confirm(...)) __doPostBack(...);
}

Dim btnView As LinkButton = e.Item.Cells(0).Controls(0)
btnView.Attributes.Add("onclick", "CheckDictationMode();")


-- if the confim(which shows your message) returns false, the __doPostBack
is not called.

You'll have to experiment a bit, but that pattern should do it. You may have
disable the autopostback on the control, since you'll be deciding whether it
posts back or not now.
Peter
 
J

Jon Delano

Peter

Thanks for the quick response !!
Very interesting approach..... I am using a 3rd party control called
ScrollingGrid 1.1 (Interscape Technologies) I like it a lot.
When I mouse over the linkbuttons in my first coloumn I see each row is
"javascript:__doPostBack(gridname_row_col,'')" calls.

So I am thinking it is already overriding the __doPostBack functionality.
I am emailing them to ask if there is away I can override the call of each
row to my own function that I can then make the decision on whether to
display my message or postback as normal.

Thanks again for the great lead.
Jon

(cool site you have there too)
 
J

Jon Delano

Peter

With you great lead .. here is what I came up with (with some help from a
post in a newsgroup)
AND.. it works (which is always a plus)

var oldPostBack
function window.onload() {
oldPostBack=__doPostBack;
__doPostBack=MyPostBack;
}

function MyPostBack(Param1, Param2) {
if (document.all('lblDictationMode').innerHTML!='') {
alert('Please finish or cancel your edit/creation of the current
dictation before selecting a new patient.');
document.all('lblUserMessage').innerHTML=''; }
else {
if (typeof(oldPostBack)=='function') oldPostBack(Param1,
Param2); } }

Now my site displays the message and doesn't post so everything stays right
where it was and allows the user to click the save button if they whish,
else it acts normally.

This is a better solution, as it doesn't matter what control caused the
postback. I will catch it, before I was just working on the datagrid
(scrollinggrid).

Thanks again, I would have never thought of that __doPostBack function. You
da MAN !!!

Jon
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top