【发布时间】:2014-06-19 19:32:50
【问题描述】:
我的第二个 ASP.NET 问题。还在摸索……
因此,每次我单击子节点时,根节点都会崩溃,并且根节点和子节点都会重复。我完全不知道为什么。这似乎发生在我目前使用的所有浏览器(IE、Chrome 和 Firefox)中。
子节点的动作是选择。当我单击一个节点时,相邻 div 中的表会使用从 SQL 数据库中获取的信息进行更新。
该站点相当粗糙...在使它看起来漂亮之前尝试学习 ASP.NET 和 Webforms 内部结构。
图片一 - 我已经打开了一个根节点,并且即将选择一个子节点。
图二 - 我选择了一个子节点。根节点已折叠 (?) 并已重复。
图三 - 我重新打开一个根节点,孩子们都复制了。
后台C#代码:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LocalTest
{
public partial class WebForm1 : System.Web.UI.Page
{
//Setup connection string.
SqlConnection conn;
SqlDataReader reader = null;
protected void Page_Load(object sender, EventArgs e)
{
//Table1.Visible = false;
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);
//Initial treeview setup.
TreeInit();
//Initial table setup
TableInit();
try
{
//Open connection and set up reader.
conn.Open();
SqlCommand cmd = new SqlCommand("select BatchNumber, Area from tblBatch;", conn);
reader = cmd.ExecuteReader();
while(reader.Read())
{
//Fill up each nodes with respective Batch Numbers.
if(reader["Area"].ToString().Equals("N"))
{
TreeNode aNode = new TreeNode(reader["BatchNumber"].ToString());
aNode.SelectAction = TreeNodeSelectAction.Select;
TreeView1.Nodes[0].ChildNodes.Add(aNode);
}
else
{
TreeNode aNode = new TreeNode(reader["BatchNumber"].ToString());
aNode.SelectAction = TreeNodeSelectAction.Select;
TreeView1.Nodes[1].ChildNodes.Add(aNode);
}
}
conn.Close();
TreeView1.CollapseAll();
}
catch (Exception er)
{
}
}
protected void Chart1_Load(object sender, EventArgs e)
{
}
protected void Chart1_Load1(object sender, EventArgs e)
{
}
protected void OnSelectNode(object sender, EventArgs e)
{
//Response.Write(TreeView1.SelectedNode.Text);
string batchNumber = TreeView1.SelectedNode.Text;
Response.Write(batchNumber);//Debugging
conn.Open();
SqlCommand cmd = new SqlCommand("select * from tblTemp where BatchId = (select Id from tblBatch where BatchNumber = " + batchNumber + ")", conn);
reader = cmd.ExecuteReader();
while(reader.Read())
{
TableRow aRow = new TableRow();
for (int i = 0; i < 5; i++)
{
TableCell aCell = new TableCell();
aCell.Text = reader[i].ToString();
aRow.Cells.Add(aCell);
}
Table1.Rows.Add(aRow);
}
conn.Close();
//Table1.Visible = true;
}
protected void TableInit()
{
TableRow headerRow = new TableRow();
TableCell headerCell1 = new TableCell();
TableCell headerCell2 = new TableCell();
TableCell headerCell3 = new TableCell();
TableCell headerCell4 = new TableCell();
TableCell headerCell5 = new TableCell();
headerCell1.Text = "Id";
headerCell2.Text = "BatchId";
headerCell3.Text = "Temp1";
headerCell4.Text = "Temp2";
headerCell5.Text = "Seq No.";
headerRow.Cells.Add(headerCell1);
headerRow.Cells.Add(headerCell2);
headerRow.Cells.Add(headerCell3);
headerRow.Cells.Add(headerCell4);
headerRow.Cells.Add(headerCell5);
Table1.Rows.Add(headerRow);
}
protected void TreeInit()
{
//Create nodes.
TreeNode nordonNode = new TreeNode("Nordon");
TreeNode bobyNode = new TreeNode("Boby");
//Set select action to expand. Not doing this resulting in duplication of nodes.
nordonNode.SelectAction = TreeNodeSelectAction.Expand;
bobyNode.SelectAction = TreeNodeSelectAction.Expand;
//Add nodes to treeview.
TreeView1.Nodes.Add(nordonNode);
TreeView1.Nodes.Add(bobyNode);
}
}
}
WebForm HTML/ASP:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="LocalTest.WebForm1" %>
<%@ Register assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.DataVisualization.Charting" tagprefix="asp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Cast Records</title>
<link rel="stylesheet" type="text/css" href="CSS/StyleSheet.css"/>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>" ></asp:SqlDataSource>
<!--SelectCommand="SELECT * FROM [People]"-->
</div>
<!-- header for the page -->
<div id="header">
</div>
<!-- tree view div -->
<div id="treeViewDiv">
<asp:TreeView ID="TreeView1" runat="server" Height="571px" ShowLines="True" Width="135px" OnSelectedNodeChanged="OnSelectNode" style="position: relative">
</asp:TreeView>
</div>
<!-- table div -->
<div id="tableDiv">
<asp:Table ID="Table1" runat="server" Height="96px" Width="459px" style="z-index: 1; left: 343px; top: 238px; position: relative; height: 96px; width: 459px" BackColor="#99CCFF" BorderColor="#FFFF99" BorderStyle="Solid" BorderWidth="2px" GridLines="Both">
</asp:Table>
</div>
</form>
</body>
</html>
样式表:
body {
}
#header{
width: 100%;
height:100px;
background-color: #66FFFF;
}
#treeViewDiv{
float:left;
width: 30%;
height: 500px;
background-color: #ff6a00;
overflow: auto;
}
#tableDiv{
float: right;
width:70%;
height:500px;
background-color: #00ff21;
}
编辑:生命周期和 (!IsPostBack)。明白了!非常感谢你们。我用 (!IsPostBack) 封装了代码,它在某些地方有所帮助,并在其他地方引起了问题。我知道现在该去哪里了。感谢您的回复。
【问题讨论】:
标签: asp.net events webforms treeview duplication