locked
RegisterClientScriptBlock not working , the javascript function is not firing RRS feed

  • Question

  • User-1736034386 posted

    here is my code 

    Private Sub cmdArchive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdArchive.Click
    Try
    Dim archiveMode As Integer = -1
    Dim dDate As DateTime? = Nothing
    Dim today As DateTime = Date.Today
    Dim tomorrow As DateTime = today.AddDays(1)


    If txtArchiveDate.SelectedDate.HasValue Then
    dDate = txtArchiveDate.SelectedDate.Value
    End If

    If dDate = tomorrow Then

    Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "Yes", "confirm()", True)

    End If

    Based on the result from confirm function i need to execute the server side code, if yes means execute the rest of the srever side code if no means go back to the web page to select other values.

    Wednesday, July 24, 2013 4:44 PM

Answers

  • User-760709272 posted

    aspx

    <asp:Calendar ID="txtArchiveDate" runat="server"/>
        
    <asp:Button ID="cmdArchive" runat="server" Text="Archive" OnClick="cmdArchive_Click" />
    <asp:Panel ID="PanelConfirm" runat="server" Visible="false" EnableViewState="false">
        You picked tomorrow
        <asp:Button ID="cmdConfirm" runat="server" Text="OK" OnClick="cmdConfirm_Click" />
    </asp:Panel>

    Code

    protected void cmdArchive_Click(object sender, EventArgs e)
    {
        // Get just the date, not the time
        DateTime tomorrow = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
        // Add a day to get tomorrow's date
        tomorrow = tomorrow.AddDays(1);
    
        DateTime dDate = txtArchiveDate.SelectedDate;
    
        if (dDate == tomorrow)
        {
            // Tomorrow was picked so show the Confirm panel
            PanelConfirm.Visible = true;
        }
        else
        {
            // Just do the work now with no confirmation
            DoWork();
        }
    }
    
    protected void cmdConfirm_Click(object sender, EventArgs e)
    {
        // User has confirmed tomorrow is ok so do the work
        DoWork();
    }
    
    private void DoWork()
    {
        // Your code goes here
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 24, 2013 6:13 PM

All replies

  • User-760709272 posted

    All RegisterClientScriptBlock does is write that javascript to the appropriate place on the page.  It doesn't "execute" it, your server-side code is not running in the browser.  Just because you see javascript in your code-behind doesn't mean it is *running* in your code behind.  You need to take time to first understand how web technology and asp.net works.

    What you have to do is put the code you want to run in a seperate function.  You put a confirm button on your page and you attack a click handler to it.  Pushing the button posts the page back to your server and the click handler runs, and you then run the rest of your code.  Basically it is a two-stage process.  You generate a button in your normal page load, then you react to that button being clicked and run your code that needs run when the user confirms.

    You really have to start from the very beginning and go through a book or online tutorials about how asp.net works as you haven't got the fundamentals understood yet, and without understanding those you will find it hard to code your website.

    Wednesday, July 24, 2013 5:33 PM
  • User-1736034386 posted

    thanks AidyF.  Can you help me to solve this probelm?

    when the user selects the date which is equal to tomorrow then they will click archive button i need to alert them that it is tomorrow date so their document will not be shown tomorrow. 

    my question is how can ia call this alert from serverside on button click event?

    Or shall i call a hjavascript function OnSelectedDateChanged event of Datepicker control? 

    pls help me to decide.

    Wednesday, July 24, 2013 5:46 PM
  • User-760709272 posted

    aspx

    <asp:Calendar ID="txtArchiveDate" runat="server"/>
        
    <asp:Button ID="cmdArchive" runat="server" Text="Archive" OnClick="cmdArchive_Click" />
    <asp:Panel ID="PanelConfirm" runat="server" Visible="false" EnableViewState="false">
        You picked tomorrow
        <asp:Button ID="cmdConfirm" runat="server" Text="OK" OnClick="cmdConfirm_Click" />
    </asp:Panel>

    Code

    protected void cmdArchive_Click(object sender, EventArgs e)
    {
        // Get just the date, not the time
        DateTime tomorrow = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
        // Add a day to get tomorrow's date
        tomorrow = tomorrow.AddDays(1);
    
        DateTime dDate = txtArchiveDate.SelectedDate;
    
        if (dDate == tomorrow)
        {
            // Tomorrow was picked so show the Confirm panel
            PanelConfirm.Visible = true;
        }
        else
        {
            // Just do the work now with no confirmation
            DoWork();
        }
    }
    
    protected void cmdConfirm_Click(object sender, EventArgs e)
    {
        // User has confirmed tomorrow is ok so do the work
        DoWork();
    }
    
    private void DoWork()
    {
        // Your code goes here
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 24, 2013 6:13 PM
  • User-1736034386 posted

    Thanks Aidyf for clearly expalining the code. But I need to show a warning message when the user clicks the tomorrow date. Based on the choice of OK or Cancel button then i ned to perform the rest of the server side code. how to show the warning message in this scenario?

    Please help.

    Thursday, July 25, 2013 8:50 AM
  • User-2133509574 posted

    Hi,

    Try this code posted here

    http://iamharidodda.blogspot.com/2013/07/javascript-alert-box-in-aspnet.html

    Thanks

    Wednesday, September 4, 2013 2:53 AM