大家好:
      我写的是网页的程序,在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,我想请问一下,问题出在哪里,应该怎么解决,谢谢各位的帮助!

解决方案 »

  1.   

    刚才我试了一下你给的 WebPage2的代码,没有发现你说的问题,if (!IsPostBack){.....}里面的代码只走了一
    次,
    你检查一下: WebPage1 是否已经设置成启动页面,如果不是把它设置成“设为起始页”,然后再运行一下程序
    如果还不能解决,最好把 WebPage1 中的代码也贴过来。大家共同研究
      

  2.   

    先谢谢,我已经把WebPage1设置为起始页了,当跳转到WebPage2时if (!IsPostBack){.....}还时走了2次,下面是我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&nbsp;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&nbsp;Register</td>
    </tr>
    <tr>
    <td class="tableCellOne" style="WIDTH: 45%"><span class="obligatory">*</span>UserName:
    <em>
    <div class="attention">you&nbsp;can&nbsp;create&nbsp;your&nbsp;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&nbsp;need&nbsp;create&nbsp;your&nbsp;password&nbsp;to&nbsp;enter&nbsp;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&nbsp;Password:
    <em>
    <div class="attention">please&nbsp;comfirm&nbsp;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&nbsp;Name:
    <em>
    <div class="attention">Your&nbsp;Real&nbsp;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&nbsp;E-mail&nbsp;Address:
    <em>
    <div class="attention">We&nbsp;can&nbsp;contact&nbsp;with&nbsp;you&nbsp;pass&nbsp;address&nbsp;you&nbsp;fill&nbsp;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&nbsp;your&nbsp;birthday:<em><div class="attention">A&nbsp;litter&nbsp;surprise&nbsp;at&nbsp;that&nbsp;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&nbsp;protect&nbsp;your&nbsp;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&nbsp;for&nbsp;question&nbsp;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&nbsp;of&nbsp;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">&nbsp;<label for="agree" class="obligatory">I&nbsp;agree&nbsp;with&nbsp;the&nbsp;terms&nbsp;of&nbsp;server!</label></p>
    <p><input type="submit" class="formSubmit" value="submit" onclick="RegisterAccount()"></p>
    </td>
    </tr>
    </table>
    </form>
    </div>
    <div id="pagefooter">copyright&nbsp;&copy;chen&nbsp;kankan&nbsp;power&nbsp;by&nbsp;Expression&nbsp;Engine</div>
    </body>
    </HTML>
      

  3.   

    大家好:
          前面的例子可能太长了,看着麻烦,我用了简单点的,如下:
    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次,请各位指导我一下,谢谢!
      

  4.   

    新给出的代码我也运行了,只有连续点击按钮的时候,才会运行2次,否则if(!IsPostBack){......}只走一次
      

  5.   

    没有啊,新的代码刚刚我在同事的机子上跑也不行,if(!IsPostBack){......}也走了2次,你断点再试试看
      

  6.   

    解决了,是我自己的问题
    <input type="submit" class="formSubmit" value="submit" onclick="RegisterAccount()"> 
    里面不能有onclick事件,否则就提交了2次,谢谢!