Stopping the Server Side code from running ?

Discussion in 'ASP .Net' started by Jon Delano, May 19, 2006.

  1. Jon Delano

    Jon Delano Guest

    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
    Jon Delano, May 19, 2006
    #1
    1. Advertising

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

    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    "Jon Delano" wrote:

    > 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
    >
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, May 19, 2006
    #2
    1. Advertising

  3. Jon Delano

    Jon Delano Guest

    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)

    "Peter Bromberg [C# MVP]" <> wrote in message
    news:...
    > 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
    >
    > --
    > Co-founder, Eggheadcafe.com developer portal:
    > http://www.eggheadcafe.com
    > UnBlog:
    > http://petesbloggerama.blogspot.com
    >
    >
    >
    >
    > "Jon Delano" wrote:
    >
    >> 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
    >>
    >>
    >>
    Jon Delano, May 19, 2006
    #3
  4. Jon Delano

    Jon Delano Guest

    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


    "Peter Bromberg [C# MVP]" <> wrote in message
    news:...
    > 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
    >
    > --
    > Co-founder, Eggheadcafe.com developer portal:
    > http://www.eggheadcafe.com
    > UnBlog:
    > http://petesbloggerama.blogspot.com
    >
    >
    >
    >
    > "Jon Delano" wrote:
    >
    >> 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
    >>
    >>
    >>
    Jon Delano, May 19, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page