我已经看过很多关于这方面的文章,但我仍然不明白为什么我的代码还在做这件事。
我有一个有2个按钮上的aspx页面。在页面加载中,我检查它是否是第一次使用(!IsPostBack)加载,并设置了一些变量,然后继续在此页面中使用。
出于某种原因,我不能解释这是预期的和完美的工作,但突然之间,这段代码现在每次点击按钮后页面重新加载时都会被点击。这意味着变量被一次又一次地更新,我现在每次点击按钮都会丢失数据,这不是我想要的。
这是web表单
<!DOCTYPE html>
<%-- --%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="BrowserTalking.js"></script>
<link href="LandingPage.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="#"/>
<title>Home Page</title>
</head>
<body onload="speaking();">
<form id="form1" runat="server" class="wholeScreenDiv">
<div class="mainColumn">
<asp:Label ID="questionLabel" runat="server" CssClass="questionText"></asp:Label>
<div class="answerRow">
<asp:Button ID="Button1" runat="server" Text="Yes" CssClass="myButton1" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="No" CssClass="myButton2" OnClick="Button2_Click" />
</div>
</div>
<asp:HiddenField runat="server" ID="questionNumber" />
<asp:HiddenField runat="server" ID="SpeechSynthNeeded" />
</form>
</body>
</html>名为onload的JavaScript方法只是使用了一个语音合成应用程序接口,同样,这也是以前它完美工作时的情况。
function speaking() {
var questionNum = document.getElementById("questionNumber").value;
var actualQuestion = "";
var utterance = new SpeechSynthesisUtterance();
utterance.rate = 0.7;
if (document.getElementById("SpeechSynthNeeded").value == "1") {
if (questionNum == 0) {
actualQuestion = 'Can you read and understand this text clearly';
} else if (questionNum == 1) {
actualQuestion = 'Would you still like the information spoken to you?';
} else if (questionNum == 2) {
actualQuestion = "Do you suffer from Aphasia?";
} else if (questionNum == 3) {
actualQuestion = "Do you suffer from Hemianopia";
} else if (questionNum == 4) {
actualQuestion = "Would you like to setup an account?";
}
utterance.text = actualQuestion;
speechSynthesis.speak(utterance);
}
}后面的代码如下所示
public partial class WebForm1 : System.Web.UI.Page
{
public ApplicationQuestion _Question;
public ApplicationUser _User;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) // Do all of this on the first time this page lods
{
_User = new ApplicationUser();
_Question = new ApplicationQuestion();
questionLabel.Text = _Question.GenerateQuestion(0);
Session["Question"] = _Question;
Session["User"] = _User;
questionNumber.Value = "0";
SpeechSynthNeeded.Value = "1";
}
else // Do all within this else every time a question is answered
{
_Question = (ApplicationQuestion)Session["Question"];
_User = (ApplicationUser)Session["User"];
if (int.TryParse(questionNumber.Value, out int number))
{
number++;
questionNumber.Value = number.ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if(int.TryParse(questionNumber.Value, out int number))
{
//Generate Next Question
questionLabel.Text = _Question.GenerateQuestion(number);
}
// Add 1 to the questions results list meaning the user answered Yes
_User._QuestionResults.Add(1);
if (int.TryParse(questionNumber.Value, out int number2))
{
// if it is on the first question and they answer yes then set SpeechSynthe Value to 0 so it isnt used in javascript for next question
if (number2 == 1)
{
SpeechSynthNeeded.Value = "0";
}
// if it is on the second question and they answer yes then set SpeechSynthe Value will be set back to 1 so its reactivated
if (number2 == 2)
{
SpeechSynthNeeded.Value = "1";
}
// After Final question redirect to User login page
if (number2 == 5)
{
Response.Redirect("UserLogin.aspx?synthNeeded=" + SpeechSynthNeeded.Value);
}
}
}在调试时,我现在得到的问题是,当我第一次单击按钮时,它会进入pageLoad和Else (这就是我想要的),但随后按钮单击方法被调用,一旦完成,它就会返回到页面加载,并进入If/Else的第一部分,因此再次设置所有变量,我会丢失所需的数据。
我不知道这是不是我已经改变的很小的东西,或者我只是因为看太久而失去了理智,但我想要做的就是在页面加载中设置一个对象,然后每次单击按钮时,我都会为该对象添加一个值,然后在以后的行中使用该值。我将这个对象放在会话中,以便在需要时将其拉回。
谁能告诉我为什么我一次又一次地点击“第一次”页面加载,即使使用了(!IsPostBack),即使是在点击按钮之后?
发布于 2020-03-26 22:39:56
解决了,
这是迄今为止发生在我身上的最奇怪、最恼人的事情之一。
从aspx页面中删除行停止了双页面加载,现在它可以正常工作。
多亏了https://forums.asp.net/t/1004939.aspx?Page_Load+called+twice+,我发现其他人已经解决了src="“图像标签的问题。
我为一个语音识别API添加了这个,并不认为这会导致这种情况的发生,我仍然不知道为什么会发生这种情况。
https://stackoverflow.com/questions/60867663
复制相似问题