You must Sign In to post a response.
  • Category: ASP.NET

    ASPX page postback with JavaScript function included

    I had one problem like this yesterday and I resolved it but NOW ... I want to expand and do a calculation on a value client side and write that to a textbox. This is not possible?

    I want to put todays date and time in one textbox and a date that is 24 hours later in another textbox, do NO POSTBACK (all client side) then submit that to the server for DB storage.

    This is not possible I am told and I found it must be true ... when I use this code, the page does as expected and changes the boxes no problem with NO POSTBACK ... EXCEPT when I add a calculation (see the commented lines inside this block) and then the page posts back.

    I'm going to have the database do the work for now, but if anyone knows a work around or a way to get an ASP page to calculate on the client side so you don't have to keep posting back, please let me know ... nothing to date I have found works.

    function fcnSetMyLongShift(shiftType) {
    var tbxMyStartDate = document.getElementById('tbxTimeStart');
    var tbxMyEndDate = document.getElementById('tbxTimeEnd');
    var lbxMyShiftDay = document.getElementById('lbxDays');

    // -------------------------------------------------------------------------------------
    // WHEN I try to do any calculation inside this function, the page posts back...
    // The next two lines, when uncommented, make the page postback...
    // var dtNextShiftDay = Date(document.getElementById('tbxTimeEnd').value);
    // dtNextShiftDay.setDate(dtNextShiftDay.getDate() + 1);
    // -------------------------------------------------------------------------------------

    if (shiftType == "1") {
    tbxMyStartDate.value = lbxMyShiftDay.value + " 7:00";
    tbxMyEndDate.value = lbxMyShiftDay.value + " 23:00";
    }
    if (shiftType == "2") {
    tbxMyStartDate.value = lbxMyShiftDay.value + " 5:00";
    tbxMyEndDate.value = lbxMyShiftDay.value + " 22:00";
    }
    return false;
    }

    ASPX Markup for the button calling the JavaScript...

    <asp:Button runat="server" ID="btnLongShift1" Width="65" Text = "Long Shift Type 1" OnClientClick="return fcnSetMyLongShift('1');"/>
  • #763499
    Hello Cool,

    You can set the property of PostBack = "false" to button. I think this will help you to stop working postback of button and it will run java script function.

    Or else you can use :

    <asp:Button runat="server" ID="btnLongShift1" Width="65" Text = "Long Shift Type 1" OnClientClick="return fcnSetMyLongShift('1'); return false;"/>

    This is will preventing postback on button click.

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #763502
    Thanks so much for the response ... I added the "return false;" that you recommended and it worked fine, as usual.

    BUT ... when I uncomment the line to add a day to the .value of the date, it posts back. Then I go to the code, comment that single line back out again ... and it works with NO postback. So that single line, with the .getDate and .setDate causes the postback.

    So something to do with having a JavaScript do ANY function call (in this case .getDate and .setDate, causes ASPX page to postback ... any calculations JavaScript does on the client side forces a postback always?

  • #763503
    Hello,

    There is also default postback events added to the textbox. You must false postback events of textbox if you want to change the value by java script code.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #763504
    You can trigger the post back using java script whenever you want.

    Following is the code for trigger the post back for particular control.

    __doPostBack('ctl00_ContentPlaceHolder1_btnAddRows', '');

    By Nathan
    Direction is important than speed

  • #763507
    Thanks so much for the response ... I added the "return false;" that you recommended and it worked fine, as usual.

    BUT ... when I uncomment the line to add a day to the .value of the date, it posts back. Then I go to the code, comment that single line back out again ... and it works with NO postback. So that single line, with the .getDate and .setDate causes the postback.

    So something to do with having a JavaScript do ANY function call (in this case .getDate and .setDate, causes ASPX page to postback ... any calculations JavaScript does on the client side forces a postback always?

  • #763508
    Seems as though it is not possible with JavaScript, to manipulate text and information in a form on the client side with no postback.

    Only with AJAX apparently.


Sign In to post your comments