大家好:
我写的是网页的程序,在WebPage1中我使用了POST的方法document.forms('register_member_form').submit();把表单里的内容提交给WebPage2,WebPage2的前台没有控件,后台代码如下:
private string[] strErrors;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
bool b=IsPostBack;
bool x=!IsPostBack;
if (!IsPostBack)
{
strErrors=new string[9]; string strUserName=Request.Form["userName"].ToString();
string strPassword=Request.Form["password"].ToString();
string strPasswordConfirm=Request.Form["confirmPW"].ToString();
string strSex=Request.Form["sex"].ToString();
string strRealName=Request.Form["realName"].ToString();
string strEmail=Request.Form["email"].ToString();
string strBirthday=Request.Form["birthday"].ToString();
string strQuestion=Request.Form["question"].ToString();
string strAnswer=Request.Form["answer"].ToString();
User user=new User(strUserName,strPassword,strSex,strEmail,strQuestion,strAnswer,strRealName,strBirthday);
user.AddUser();
}
}
现在的问题是if (!IsPostBack){.....}里的代码运行了2次(我只想他运行1次),而且bool b=IsPostBack的值为false,我想请问一下,问题出在哪里,应该怎么解决,谢谢各位的帮助!
我写的是网页的程序,在WebPage1中我使用了POST的方法document.forms('register_member_form').submit();把表单里的内容提交给WebPage2,WebPage2的前台没有控件,后台代码如下:
private string[] strErrors;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
bool b=IsPostBack;
bool x=!IsPostBack;
if (!IsPostBack)
{
strErrors=new string[9]; string strUserName=Request.Form["userName"].ToString();
string strPassword=Request.Form["password"].ToString();
string strPasswordConfirm=Request.Form["confirmPW"].ToString();
string strSex=Request.Form["sex"].ToString();
string strRealName=Request.Form["realName"].ToString();
string strEmail=Request.Form["email"].ToString();
string strBirthday=Request.Form["birthday"].ToString();
string strQuestion=Request.Form["question"].ToString();
string strAnswer=Request.Form["answer"].ToString();
User user=new User(strUserName,strPassword,strSex,strEmail,strQuestion,strAnswer,strRealName,strBirthday);
user.AddUser();
}
}
现在的问题是if (!IsPostBack){.....}里的代码运行了2次(我只想他运行1次),而且bool b=IsPostBack的值为false,我想请问一下,问题出在哪里,应该怎么解决,谢谢各位的帮助!
次,
你检查一下: WebPage1 是否已经设置成启动页面,如果不是把它设置成“设为起始页”,然后再运行一下程序
如果还不能解决,最好把 WebPage1 中的代码也贴过来。大家共同研究
<%@ Page language="c#" Codebehind="Register1.aspx.cs" AutoEventWireup="false" Inherits="WebDiary.WebPages.Register1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Register1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script src="../dojoroot/dojo/dojo.js" type="text/javascript" djConfig="isDebug:true,parseOnLoad:true"></script>
<style type="text/css">@import url( ../dojoroot/dijit/themes/tundra/tundra.css ); @import url( ../dojoroot/dojo/resources/dojo.css ); @import url( ../dojoroot/dijit/themes/soria/soria.css ); @import url( ../dojoroot/dijit/themes/nihilo/nihilo.css );
</style>
<LINK href="../CSS/cRegister1.css" type="text/css" rel="stylesheet">
<script type="text/javascript">
dojo.require("dojo.parser");
dojo.require("dijit.Dialog");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.Button");
dojo.require("dijit.Tooltip");
dojo.require("dijit.layout.AccordionContainer");
dojo.require("dijit.form.FilteringSelect");
dojo.require("dijit.form.DateTextBox");
dojo.require("dijit.form.Textarea");
dojo.require("dojo.data.ItemFileReadStore");
function RegisterAccount()
{
if (!document.getElementById('agree').checked)
{
alert('You can register after you agree with terms!');
window.event.returnValue=false;
return false;
}
else if(document.getElementById('agree').checked)
{
document.forms('register_member_form').submit();
}
else
{
alert('Unknown Error!');
}
}
</script>
</HEAD>
<body class="tundra" MS_POSITIONING="GridLayout">
<div id="pageheader">Member Register</div>
<div id="subheader"></div>
<div id="content">
<form id="register_member_form" method="post" action="../Operation/AddUser.aspx" target="_self">
<table style="WIDTH: 100%" cellSpacing="0" cellPadding="0" border="0">
<tr>
<td class="title" colSpan="2">Member Register</td>
</tr>
<tr>
<td class="tableCellOne" style="WIDTH: 45%"><span class="obligatory">*</span>UserName:
<em>
<div class="attention">you can create your loginname</div>
</em>
</td>
<td class="tableCellTwo"><input name="userName" id="userName" style="WIDTH: 200px; HEIGHT: 20px" dojoType="dijit.form.TextBox"></td>
</tr>
<tr>
<td class="tableCellOne" style="WIDTH: 45%"><span class="obligatory">*</span>Password:
<em>
<div class="attention">you need create your password to enter webDiary</div>
</em>
</td>
<td class="tableCellTwo"><input name="password" id="password" style="WIDTH: 200px; HEIGHT: 20px" dojoType="dijit.form.TextBox"></td>
</tr>
<tr>
<td class="tableCellOne" style="WIDTH: 45%"><span class="obligatory">*</span>Comfirm Password:
<em>
<div class="attention">please comfirm password</div>
</em>
</td>
<td class="tableCellTwo"><input name="confirmPW" id="confirmPW" style="WIDTH: 200px; HEIGHT: 20px" dojoType="dijit.form.TextBox"></td>
</tr>
<tr>
<td class="tableCellOne" style="WIDTH: 45%"><span class="obligatory">*</span>Your Name:
<em>
<div class="attention">Your Real Name</div>
</em>
</td>
<td class="tableCellTwo"><input name="realName" id="realName" style="WIDTH: 200px; HEIGHT: 20px" dojoType="dijit.form.TextBox"></td>
</tr>
<tr>
<td class="tableCellOne" style="WIDTH: 45%"><span class="obligatory">*</span>Sex:</td>
<td class="tableCellTwo"><select name="sex" id="sex" style="WIDTH: 200px; HEIGHT: 20px" dojoType="dijit.form.FilteringSelect"
value="male">
<option value="male" selected>Male</option>
<option value="female">Female</option>
</select></td>
</tr>
<tr>
<td class="tableCellOne" style="WIDTH: 45%"><span class="obligatory">*</span>Input E-mail Address:
<em>
<div class="attention">We can contact with you pass address you fill in</div>
</em>
</td>
<td class="tableCellTwo"><input name="email" id="email" style="WIDTH: 200px; HEIGHT: 20px" dojoType="dijit.form.TextBox"></td>
</tr>
<tr>
<td class="tableCellOne" style="WIDTH:45%"><span class="obligatory">*</span>Choose your birthday:<em><div class="attention">A litter surprise at that day</div>
</em>
</td>
<td class="tableCellTwo"><input name="birthday" id="birthday" dojoType="dijit.form.DateTextBox" style="WIDTH:200px;HEIGHT:20px"
value="1982-06-03"></td>
</tr>
<tr>
<td class="tableCellOne" style="WIDTH:45%"><span class="obligatory">*</span>Question:<em><div class="attention">To protect your account</div>
</em>
</td>
<td class="tableCellTwo"><div dojoType="dojo.data.ItemFileReadStore" jsId="questionStore" url="../JSON/Question.txt"></div>
<select name="question" id="question" dojoType="dijit.form.FilteringSelect" store="questionStore"
searchAttr="question" style="WIDTH:200px;HEIGHT:20px">
</select></td>
</tr>
<tr>
<td class="tableCellOne" style="WIDTH:45%"><span class="obligatory">*</span>Answer:<em><div class="attention">Answer for question above</div>
</em>
</td>
<td class="tableCellTwo"><input name="answer" id="answer" dojoType="dijit.form.TextBox" style="WIDTH:200px;HEIGHT:20px"></td>
</tr>
<tr>
<td colspan="2" class="tableSingleCell"><p class="obligatory">terms of server</p>
<textarea id="terms" dojoType="dijit.form.Textarea" readonly style="WIDTH:100%">All messages posted at this site express the views of the author, and do not necessarily reflect the views of the owners and administrators of this site.By registering at this site you agree not to post any messages that are obscene, vulgar, slanderous, hateful, threatening, or that violate any laws. We will permanently ban all users who do so. We reserve the right to remove, edit, or move any messages for any reason.</textarea></td>
</tr>
<tr>
<td class="tableSingleCell" colspan="2">
<p><input type="checkbox" id="agree"> <label for="agree" class="obligatory">I agree with the terms of server!</label></p>
<p><input type="submit" class="formSubmit" value="submit" onclick="RegisterAccount()"></p>
</td>
</tr>
</table>
</form>
</div>
<div id="pagefooter">copyright ©chen kankan power by Expression Engine</div>
</body>
</HTML>
前面的例子可能太长了,看着麻烦,我用了简单点的,如下:
WebForm1.aspx 前台代码:
<script type="text/javascript">
function RegisterAccount()
{
if (!document.getElementById('agree').checked)
{
alert('You can register after you agree with terms!');
window.event.returnValue=false;
return false;
}
else if(document.getElementById('agree').checked)
{
document.forms('register_member_form').submit();
}
else
{
alert('Unknown Error!');
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="register_member_form" method="post" action="WebForm2.aspx" target="_self">
<input type="text" name="textName"><input type="checkbox" id="agree"><input type="submit" class="formSubmit" value="submit" onclick="RegisterAccount()">
</form>
</body>WebForm2.aspx后台代码:
if (!IsPostBack)
{
string x="";
}同样的if(!IsPostBack){......}里的代码还是运行了2次,请各位指导我一下,谢谢!
<input type="submit" class="formSubmit" value="submit" onclick="RegisterAccount()">
里面不能有onclick事件,否则就提交了2次,谢谢!