Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8328113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:20:21+00:00 2026-06-09T01:20:21+00:00

I need to execute a javascript function on ASP.NET, but I’ve encountered an error

  • 0

I need to execute a javascript function on ASP.NET, but I’ve encountered an error that I can’t seem to fix…

I have alreay tried a small example that works…
here it is…

Aspx file

<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
    function ExecuteConfirm() {
    debugger
        var returnValue = confirm('Do you want to proceed further?');
        if (returnValue) {
            document.getElementById('hdnExecuteAfterConfirm').value = "1";
            document.getElementById('btnClickMe').click();
        }


    }
</script>


<body>
<form id="form1" runat="server">
<asp:Label runat="server" ID="lblMessage">
</asp:Label>

<div>
    <asp:Button ID="btnClickMe" runat="server" Text="Click me" 
        onclick="btnClickMe_Click" />
</div>
<asp:HiddenField runat="server" ID="hdnExecuteAfterConfirm" />
</form>
</body>

CS file

protected void btnClickMe_Click(object sender, EventArgs e)
    {
        //Do server side processing before confirmation
        if (hdnExecuteAfterConfirm.Value != "1")
        {
            lblMessage.Text = "You clicked the button";
            ClientScript.RegisterStartupScript(this.Page.GetType(), btnClickMe.ID, "ExecuteConfirm()", true);

        }
        //Do server side processing after confirmation
        else
        {
            //Proceed further with server side processing

            //Reset the value of hidden field
            hdnExecuteAfterConfirm.Value = "";
            //Notify the user that processing is complete.
            ClientScript.RegisterStartupScript(this.Page.GetType(), btnClickMe.ID, "alert('Processing is complete.')", true);
        }
    }

The goal here is to display a confirm message the first time you click the button (if), and once you click YES on the confirm message it will repeat the button function, however, it’ll do something different (else)…

I’m trying to recreate this function on my project, but I am encountering an error…

Here’s what I have…

Note, I’m not copying my entire project, those are just the parts that I had to change…

Site.Master

<script type="text/javascript" language="javascript">
        function ConfirmarRegistroFecha(strAdvertencia) {
            debugger
            var returnValue = strAdvertencia;

            if (returnValue) {
                try{
                document.getElementById('hdnExecuteAfterConfirm').value = "1";  
                document.getElementById('btnGuardarEvento').click();
                }

                catch(error){
                    alert(error);
                }
            }
        }
    </script>

Agenda.aspx (I know that is to much, however, I’m copying it all, since maybe the error is around here somewhere. The point is that at least, inside is a HiddenField, and a button)

<asp:View ID="ViewOperacionesAgenda2" runat="server">
            <asp:HiddenField runat="server" ID="hdnExecuteAfterConfirm" />
            <br />

            <asp:Panel ID="pnlRegistrarEvento" runat="server" DefaultButton="btnGuardarEvento">
                <table>
                    <tr>
                        <td align="left">
                            Seleccione Vendedor:
                            <br />

                            <asp:DropDownList ID="dpdLstVendedoresAgenda" runat="server" Width="150px">
                            </asp:DropDownList>
                        </td>

                        <td align="left" rowspan="3">
                            Descripcion:
                            <br />

                            <asp:TextBox ID="tbxAlAgendaDescripcion" runat="server" Height="150px" TextMode="MultiLine" ValidationGroup="gpAgendaAlta" Width="150px">
                            </asp:TextBox>
                        </td>

                        <td align="left" rowspan="3">
                            Fecha:
                            <br />

                            <asp:Calendar ID="Calendar1" runat="server" BackColor="White" 
                                BorderColor="#3366CC" BorderWidth="1px" CellPadding="1" 
                                DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="7pt" 
                                ForeColor="#003399" Height="150px" Width="150px">
                                <DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
                                <NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
                                <OtherMonthDayStyle ForeColor="#999999" />
                                <SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                                <SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
                                <TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px" Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" />
                                <TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
                                <WeekendDayStyle BackColor="#CCCCFF" />
                            </asp:Calendar>
                        </td>

                        <td rowspan="3">
                            <table>
                                <tr>
                                    <td align="left">
                                        Hora Inicio:
                                        <br />

                                        <asp:TextBox ID="tbxAlAgendaHoraInicio" runat="server" ValidationGroup="gpAgendaAlta" Width="130px">0:00 am</asp:TextBox>

                                        <br />

                                        <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" 
                                            ControlToValidate="tbxAlAgendaHoraInicio" 
                                            ErrorMessage="Hora Invalida: use 00:00 pm" 
                                            ValidationExpression="^((0?[1-9]|1[012])(:[0-5]\d)((\ )?[ap](.)?m(.)?))" 
                                            ValidationGroup="vgpRegistrarEvento" Font-Bold="True" ForeColor="Red">
                                        </asp:RegularExpressionValidator>

                                        <br />

                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" 
                                            ControlToValidate="tbxAlAgendaHoraInicio" ErrorMessage="Campo Requerido" 
                                            ValidationGroup="vgpRegistrarEvento" Font-Bold="True" ForeColor="Red">
                                        </asp:RequiredFieldValidator>
                                    </td>
                                </tr>

                                <tr>
                                    <td align="left">
                                        Hora Final:
                                        <br/>

                                        <asp:TextBox ID="tbxAlAgendaHoraFinal" runat="server" ValidationGroup="gpAgendaAlta" Width="130px">0:00 am</asp:TextBox>

                                        <br />

                                        <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" 
                                            ControlToValidate="tbxAlAgendaHoraFinal" 
                                            ErrorMessage="Hora Invalida: use 00:00 pm" 
                                            ValidationExpression="^((0?[1-9]|1[012])(:[0-5]\d)((\ )?[ap](.)?m(.)?))" 
                                            ValidationGroup="vgpRegistrarEvento" Font-Bold="True" ForeColor="Red">
                                        </asp:RegularExpressionValidator>

                                        <br />

                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
                                            ControlToValidate="tbxAlAgendaHoraFinal" ErrorMessage="Campo Requerido" 
                                            ValidationGroup="vgpRegistrarEvento" Font-Bold="True" ForeColor="Red">
                                        </asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                    <tr>
                        <td align="left">
                            Seleccione Cliente:
                            <br />

                            <asp:DropDownList ID="dpdLstClientesAgenda" runat="server" AutoPostBack="True" onselectedindexchanged="dpdLstClientesAgenda_SelectedIndexChanged" Width="150px">
                            </asp:DropDownList>
                        </td>
                    </tr>

                    <tr>
                        <td align="left">
                            Tipo:
                            <br />

                            <asp:DropDownList ID="dpdLstAlAgendaTipo" runat="server" Width="150px">
                                <asp:ListItem>Reunion</asp:ListItem>
                                <asp:ListItem>Entrevista</asp:ListItem>
                                <asp:ListItem>Venta</asp:ListItem>
                                <asp:ListItem>Soporte</asp:ListItem>
                                <asp:ListItem>Instalacion</asp:ListItem>
                            </asp:DropDownList>
                        </td>
                    </tr>
                </table>

                <br />
                <asp:Button ID="btnCancelarEvento" runat="server" CausesValidation="False" onclick="btnCancelarEvento_Click" Text="Cancelar" Width="90px" />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="btnGuardarEvento" runat="server" onclick="btnGuardarEvento_Click" Text="Guardar" ValidationGroup="vgpRegistrarEvento" Width="90px" />
            </asp:Panel>

Agenda.aspx.cs

protected void btnGuardarEvento_Click(object sender, EventArgs e)
    {

            if (hdnExecuteAfterConfirm.Value != "1")
            {
                strAdvertencia = "MESSAGE I WANT TO DISPLAY";
                ClientScript.RegisterStartupScript(this.Page.GetType(), btnGuardarEvento.ID, "ConfirmarRegistroFecha(" + strAdvertencia + ")", true);

            }

            else
            {
                hdnExecuteAfterConfirm.Value = "";
                //Rest of process
            }
    }

Using a debugger with Firebug, I can clearly see that on Site.Master, when it reaches..

document.getElementById('hdnExecuteAfterConfirm').value = "1";

It displays the following exception:

TypeError: document.getElementById("hdnExecuteAfterConfirm") is null

I have already tried to copy the javascript on Agenda.aspx instead of Site.Master… and it still doesn’t work…

Does anyone know what the mistake here is…
I’m new to javascript, so I’m still believing that is a syntax error…

Thanks in advance and I hope you can help me

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:20:23+00:00Added an answer on June 9, 2026 at 1:20 am

    Use this script:

    document.getElementById('<%= hdnExecuteAfterConfirm.ClientID %>').value = "1";  
    document.getElementById('<%= btnGuardarEvento.ClientID %>').click();
    

    As you use master page, client id of hdnExecuteAfterConfirm not the same as server id.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP textbox control where I need to execute some javascript function
I need to execute javascript before Page load in ASP.NET application. My function returns
How do I execute an JavaScript function right when an ASP.NET text box control
I have a javascript function that I would like to execute after every asynchronous
I need to compare two calendar dates. I wrote the javascript function too. But
Okay, so I have an javascript function that retrieves some HTML... function updateQuestions(i){ var
I have an asp.net page that needs to populate a treeview based on a
In ASP.NET upon making a button click I need one JavaScript to make its
I have a flex application that needs the ability to generate and execute JavaScript.
I have a javascript function that reformats a link. When a HREF link is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.