【发布时间】:2020-03-26 12:48:13
【问题描述】:
我看过很多关于这方面的帖子,但我仍然不知道为什么我的代码仍然这样做。
我有 1 个带有 2 个按钮的 aspx 页面。在页面加载中,我检查它是否是第一次使用 (!IsPostBack) 加载并设置一些变量,然后我继续在此页面中使用。
由于某种原因,我无法解释这是按预期完美运行的,但是突然之间,每次单击按钮后重新加载页面时都会触发这部分代码。这意味着变量一次又一次地被更新,我现在每次点击按钮都会丢失数据,这不是我想要的。
这是网页表单
<!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 方法只是使用了一个语音合成 API,并且在它完美运行之前也是如此。
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) 也会一次又一次地点击“第一个”页面加载?
【问题讨论】: