我需要在 ASP.NET 上执行一个 javascript 函数,但我遇到了一个似乎无法修复的错误...
我已经尝试了一个可行的小例子......在这里......
.aspx 文件
<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文件
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);
}
}
这里的目标是在您第一次单击按钮时显示确认消息(如果),一旦您在确认消息上单击“是”,它将重复按钮功能,但是,它会做一些不同的事情(否则)......
我正在尝试在我的项目中重新创建此功能,但遇到错误...
这就是我所拥有的...
请注意,我不是在复制我的整个项目,这些只是我必须更改的部分......
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 (我知道这太多了,但是,我将其全部复制,因为错误可能就在此处的某个地方。关键是,至少,里面是一个 HiddenField 和一个按钮)
<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>
议程.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
}
}
使用带有 Firebug 的调试器,当它到达时,我可以在 Site.Master 上清楚地看到它。
document.getElementById('hdnExecuteAfterConfirm').value = "1";
它显示以下异常:
TypeError: document.getElementById("hdnExecuteAfterConfirm") is null
我已经尝试在 Agenda.aspx 而不是 Site.Master 上复制 javascript...但它仍然无法正常工作...
有谁知道这里的错误是什么......我是javascript新手,所以我仍然相信这是一个语法错误......
预先感谢,我希望你能帮助我