У меня есть функция javascript, которая вызывает событие щелчка на кнопке, которое активирует функцию в коде программной части. Я хочу, чтобы этот щелчок / функция выполнялась один раз при первой загрузке страницы, однако я не могу понять, почему событие onload происходит постоянно.
Код Figure1.aspx показан ниже.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Figure1.aspx.cs" Inherits="Figure1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Page 1</title>
</head>
<body style='margin: 0px; overflow: hidden;' onload="getLocation()">
<script>
function getLocation() {
document.getElementById('insertMyLocationButton').click();
}
</script>
<form id="form1" runat="server">
<div>
<asp:Button ID="insertMyLocationButton" runat="server" Text="insertMyLocation"
onclick="insertMyLocation2" ClientIDMode="Static" style="display:none;"/>
<p><asp:Literal runat="server" Id="insertResult">1</asp:Literal></p>
</div>
</form>
</body>
</html>
Мой Figure1.aspx.cs выглядит следующим образом
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Figure1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void insertMyLocation2(object sender, EventArgs e)
{
insertResult.Text = Convert.ToString(Convert.ToInt16(insertResult.Text)+1);
}
}
Серверная функция insertMyLocation2 в этом коде обновляет строку в теге абзаца с помощью id insertResult, и видно, что она постоянно обновляется, даже если единственный вызов shoudl будет из события onload.
Я ожидаю только одного вызова события onload.
Спасибо за помощь новичку.