好奇怪的错误:System.StackOverflowException

stonec 2003-10-19 11:08:52
在写用户自定义控件的时候,编译发生错误,如下,其他错误都没有:

发生类型为 System.StackOverflowException 的异常。
......
异常详细信息: System.StackOverflowException: 发生类型为 System.StackOverflowException 的异常。

源错误:
执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。

堆栈跟踪:
[StackOverflowException: 发生类型为 System.StackOverflowException 的异常。]


...全文
718 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
stonec 2003-10-21
  • 打赏
  • 举报
回复
我就是用你的第一种方法做的,发生上面的类型转换错误。用你的第二种方法同样发生相同的错误。下面是源码:
1、UControl.aspx,只有一个label
<%@ Page language="c#" Codebehind="UControl.aspx.cs" AutoEventWireup="false" Inherits="codebehind.WebForm1" %>
.....
<asp:label id="soonToBeUserControl" runat="server"></asp:label>
.....
UControl.aspx.cs:
......
namespace codebehind
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label soonToBeUserControl;

public void Page_Load(object sender, System.EventArgs e)
{
Control tempUserControl = LoadControl("UC1019uc.ascx");
PropertyInfo pi = tempUserControl.GetType().GetProperty("myProp");
((UC1019uc)tempUserControl).myProp = "This property was set in Code Behind";
soonToBeUserControl.Controls.Add(tempUserControl);
}
......
}
}

2、UC1019uc.ascx:
<%@ Control Language="c#" ClassName="UC1019ucCS" AutoEventWireup="false" Codebehind="UC1019uc.ascx.cs" Inherits="codebehind.UC1019uc" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:Label id="myLabel" runat="server"></asp:Label>
<asp:DataGrid id="myDataGrid" runat="server"></asp:DataGrid>

UC1019.ascx.cs:
namespace codebehind
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public class UC1019uc : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label myLabel;
protected System.Web.UI.WebControls.DataGrid myDataGrid;

public String myProp
{
get
{
return myLabel.Text;
}
set
{
myProp = value;
myLabel.Text = myProp;
}
}


public void Page_Load(object sender, System.EventArgs e)
{
SqlDataAdapter dsc = new SqlDataAdapter("select companyName,contactName,contactTitle from Customers",
"server=localhost;database=Northwind;uid=sa;pwd=");
DataSet ds = new DataSet();

dsc.Fill(ds,"customer");
myDataGrid.DataSource = ds.Tables["customer"].DefaultView;
myDataGrid.DataBind();

}
......
}
}
saucer 2003-10-20
  • 打赏
  • 举报
回复
use a code behind for your user control, and then do

Control tempUserControl = LoadControl("UC1019uc.ascx");
((YourCodeBehindClass)tempUserControl).myProp = "This property was set in Code Behind";

or

using System.Reflection;

PropertyInfo pi = tempUserControl.GetType().GetProperty("myProp");
if (pi != null)
pi.SetValue(tempUserControl,"This property was set in Code Behind",null);
yaotiebing 2003-10-20
  • 打赏
  • 举报
回复
或者类型转换的时候出现了溢出
guoyan19811021 2003-10-20
  • 打赏
  • 举报
回复
死循环,超出界限了
stonec 2003-10-20
  • 打赏
  • 举报
回复
是什么问题!?
gOODiDEA 2003-10-20
  • 打赏
  • 举报
回复
StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion
saucer 2003-10-20
  • 打赏
  • 举报
回复
most likely, you have an infinite loop in your code
stonec 2003-10-20
  • 打赏
  • 举报
回复
在web form中有一个label,他的.cs里面这样写的:
......
protected System.Web.UI.WebControls.Label soonToBeUserControl;
......
Control tempUserControl = LoadControl("UC1019uc.ascx"); //UC1019uc.ascx为我的用户控件。
((UC1019uc)tempUserControl).myProp = "This property was set in Code Behind";//将tempUserControl强制类型为UC1019ucCS,编译时这里出错,该为UC1019uc(如上)时,就出现System.StackOverflowException的错误?
soonToBeUserControl.Controls.Add(tempUserControl);

我在用户控件中强制类型<%@ ClassName="UC1019ucCS" %>也试过了,不行。

rgbcn 2003-10-19
  • 打赏
  • 举报
回复
检查一下你的代码。是不是数组超出了索引

62,025

社区成员

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

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

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

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