小弟初学ASP.net 正在学习ASP.NET揭秘(第二版)遇到了问题了,请各位大哥指点下。给分100

maomingming 2005-10-22 03:47:30
在做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
...全文
104 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
maomingming 2005-10-22
  • 打赏
  • 举报
回复
谢谢楼上两位,我真是憨的要命啊,原来是Page_Load少写了_,哈哈,问题解决了,给分。
gagool 2005-10-22
  • 打赏
  • 举报
回复

把 void PageLoad(Object sender,EventArgs e)
改为 void Page_Load(Object sender,EventArgs e)
saucer 2005-10-22
  • 打赏
  • 举报
回复
确认ViewState["ProductFieldCount"]先前有值

if (ViewState["ProductFieldCount"] == null)
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++;
maomingming 2005-10-22
  • 打赏
  • 举报
回复
显示这一行错,可我把两个程序对着看了几个小时,就没找到错误的原因。我可是一个字母一个字母对着看的。真是怪事,一台机器上,第一个程序运行报错,第二个却可以,哪位高手指点一二嘛。
maomingming 2005-10-22
  • 打赏
  • 举报
回复
正确的程序:
<%@ 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>

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧