在做P149页DynamicForm.aspx时我的程序出错,而书上自带的能运行。我把两个程序反复对比,没发现有什么不同啊?请高手帮我看一看,小弟谢了。
这个程序是动态生成表单,点击按钮Add Product Field就增加一表单。
错误程序如下:
<%@Page Language="c#" debug="true" %><Script Runat="Server">
void PageLoad(Object sender,EventArgs e)
{
if(!IsPostBack)
{
ViewState["ProductFieldCount"]=1;
}
else
{
for(int intCounter=2;intCounter<=(int)ViewState["ProductFieldCount"];intCounter++)
{
AddProductField(intCounter.ToString());
}
}
}
void AddProductFieldCount(object sender,EventArgs e)
{
ViewState["ProductFieldCount"]= (int)ViewState["ProductFieldCount"] + 1;
AddProductField(ViewState["ProductFieldCount"].ToString());
}
void AddProductField(string strFieldNum)
{
LiteralControl litLabel;
TextBox txtTextBox;
litLabel=new LiteralControl();
litLabel.Text="<p><b>Product "+strFieldNum+":<b>";
plhProductFields.Controls.Add(litLabel);
txtTextBox=new TextBox();
txtTextBox.ID="txtProduct"+strFieldNum;
plhProductFields.Controls.Add(txtTextBox);
}
void btnSubmit_Click(Object sender,EventArgs e)
{
Response.Redirect("ThankYou.aspx");
}
</Script><html>
<head>
<title>DynamicForm.aspx</title>
</head><body>
<form Runat="Server">
<b>User Name:</b>
<asp:TextBox id="txtCustomer" Runat="Server"/><p>
<div style="background-color:yellow;padding:10px">
<b>Product 1:</b>
<asp:TextBox id="txtProduct1" Runat="Server"/>
<asp:PlaceHolder id="plhProductFields" Runat="Server"/>
</div><p>
<asp:Button Text="Add Product Field" OnClick="AddProductFieldCount" Runat="Server"/>
<asp:Button id="btnSubmit" Text="Submit Complete Order" OnClick="btnSubmit_Click" Runat="Server"/>
</form>
</body>
</html>出错信息:
“/ASP.NET”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 18: void AddProductFieldCount(object sender,EventArgs e)
行 19: {
行 20: ViewState["ProductFieldCount"]= (int)ViewState["ProductFieldCount"] + 1;
行 21: AddProductField(ViewState["ProductFieldCount"].ToString());
行 22: }
源文件: H:\ASP.NET UNLEASHED\Chapter 4\DynamicForm.aspx 行: 20 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
ASP.DynamicForm_aspx.AddProductFieldCount(Object sender, EventArgs e) in H:\ASP.NET UNLEASHED\Chapter 4\DynamicForm.aspx:20
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
这个程序是动态生成表单,点击按钮Add Product Field就增加一表单。
错误程序如下:
<%@Page Language="c#" debug="true" %><Script Runat="Server">
void PageLoad(Object sender,EventArgs e)
{
if(!IsPostBack)
{
ViewState["ProductFieldCount"]=1;
}
else
{
for(int intCounter=2;intCounter<=(int)ViewState["ProductFieldCount"];intCounter++)
{
AddProductField(intCounter.ToString());
}
}
}
void AddProductFieldCount(object sender,EventArgs e)
{
ViewState["ProductFieldCount"]= (int)ViewState["ProductFieldCount"] + 1;
AddProductField(ViewState["ProductFieldCount"].ToString());
}
void AddProductField(string strFieldNum)
{
LiteralControl litLabel;
TextBox txtTextBox;
litLabel=new LiteralControl();
litLabel.Text="<p><b>Product "+strFieldNum+":<b>";
plhProductFields.Controls.Add(litLabel);
txtTextBox=new TextBox();
txtTextBox.ID="txtProduct"+strFieldNum;
plhProductFields.Controls.Add(txtTextBox);
}
void btnSubmit_Click(Object sender,EventArgs e)
{
Response.Redirect("ThankYou.aspx");
}
</Script><html>
<head>
<title>DynamicForm.aspx</title>
</head><body>
<form Runat="Server">
<b>User Name:</b>
<asp:TextBox id="txtCustomer" Runat="Server"/><p>
<div style="background-color:yellow;padding:10px">
<b>Product 1:</b>
<asp:TextBox id="txtProduct1" Runat="Server"/>
<asp:PlaceHolder id="plhProductFields" Runat="Server"/>
</div><p>
<asp:Button Text="Add Product Field" OnClick="AddProductFieldCount" Runat="Server"/>
<asp:Button id="btnSubmit" Text="Submit Complete Order" OnClick="btnSubmit_Click" Runat="Server"/>
</form>
</body>
</html>出错信息:
“/ASP.NET”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 18: void AddProductFieldCount(object sender,EventArgs e)
行 19: {
行 20: ViewState["ProductFieldCount"]= (int)ViewState["ProductFieldCount"] + 1;
行 21: AddProductField(ViewState["ProductFieldCount"].ToString());
行 22: }
源文件: H:\ASP.NET UNLEASHED\Chapter 4\DynamicForm.aspx 行: 20 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
ASP.DynamicForm_aspx.AddProductFieldCount(Object sender, EventArgs e) in H:\ASP.NET UNLEASHED\Chapter 4\DynamicForm.aspx:20
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
<%@ Page Language="C#"%><script runat=server>void Page_Load(Object sender , EventArgs e)
{
if (! IsPostBack )
ViewState[ "ProductFieldCount" ] = 1;
else
for ( int intCounter = 2 ; intCounter <= (int)ViewState ["ProductFieldCount"]; intCounter++)
AddProductField( intCounter.ToString() );
}void AddProductFieldCount( object s, EventArgs e ) {
ViewState[ "ProductFieldCount" ] = (int)ViewState[ "ProductFieldCount" ] + 1;
AddProductField( ViewState[ "ProductFieldCount" ].ToString() );
}void AddProductField( string strFieldNum ) {
LiteralControl litLabel;
TextBox txtTextBox; // Add Literal Control
litLabel = new LiteralControl();
litLabel.Text = "<p><b>Product " + strFieldNum + ":</b> ";
plhProductFields.Controls.Add( litLabel ); // Add TextBox Control
txtTextBox = new TextBox();
txtTextBox.ID = "txtProduct" + strFieldNum;
plhProductFields.Controls.Add( txtTextBox );
}void btnSubmit_Click( object s, EventArgs e ) {
Response.Redirect( "ThankYou.aspx" );
}</Script><html>
<head><title>DynamicForm.aspx</title></html>
<body><form Runat="Server"><b>Customer Name:</b>
<asp:TextBox
ID="txtCustomer"
Runat="Server" /><p>
<div style="background-color: yellow;padding:10px"><b>Product 1:</b>
<asp:TextBox
ID="txtProduct1"
Runat="Server" /><asp:PlaceHolder
id="plhProductFields"
Runat="Server" /></div><p>
<asp:Button
Text="Add Product Field"
OnClick="AddProductFieldCount"
Runat="Server" /><asp:Button
id="btnSubmit"
Text="Submit Complete Order"
OnClick="btnSubmit_Click"
Runat="Server" /></form>
</body>
</html>
ViewState["ProductFieldCount"] = 0;ViewState["ProductFieldCount"]= (int)ViewState["ProductFieldCount"] + 1;
或者这么做public int ProductFieldCount
{
get
{
object o = ViewState["ProductFieldCount"];
if (o==null)
return 0;
else
(int)ViewState["ProductFieldCount"];
} set
{
ViewState["ProductFieldCount"] = value;
}}ProductFieldCount++;
把 void PageLoad(Object sender,EventArgs e)
改为 void Page_Load(Object sender,EventArgs e)