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" />
<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
Use this script:
As you use master page, client id of
hdnExecuteAfterConfirmnot the same as server id.