StackOverflowException可能是因为EquimentBind方法在无限循环中被递归调用。当由于数据绑定过程导致回发而重复触发Page_Load事件时,可能会发生这种情况。
当station_id为null时,从Page_Load事件中删除数据绑定逻辑,因为这是不必要的,并且会导致递归调用。您可以简化Page_Load事件,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string station_id = Request.QueryString["station_id"];
if (station_id != null)
{
DropDownList1.Visible = false;
int machineid = int.Parse(station_id);
try
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
EquimentBind(machineid);
}
finally
{
connection.Close();
}
}
else
{
try
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
SqlDataAdapter adapter = //My sql command
DataTable stalist = new DataTable();
adapter.Fill(stalist);
DropDownList1.DataSource = stalist;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
}
finally
{
connection.Close();
}
DropDownList1.SelectedValue = "0";
}
}
}
修改DropDownList1_SelectedIndexChanged事件以处理这两种情况,即当从下拉列表更改选择时和从URL加载选择时:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int machineid = int.Parse(DropDownList1.SelectedValue);
try
{
connection.Open();
EquimentBind(machineid);
}
finally
{
connection.Close();
}
}