求助。修改newsadd中对findnode方法的递归调用;
using System;using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class newadd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null)
Response.Redirect("login.aspx", true);
if (!IsPostBack)
{
init_treeView();
if (Request.QueryString["id"] != null)
{
test.business.news news = new test.business.news(int.Parse(Request.QueryString["id"]));
this.title.Text = news.Title;
this.author.Text = news.Author;
this.newsbody.Text = news.Body;
this.audit.Checked = news.isAudit;
TreeNode tn=new TreeNode() ;
//改错1、修改方法调用
this.findNode(news.NewsCata,tn, this.TreeView1.Nodes);
this.newscata.Text = tn.Text;
this.hidetext.Text = tn.Value;
if (news.isAudit)
this.Button1.Enabled = false;
}
else
{
this.audit.Visible = false;
}
this.Panel1.Visible = false;
}
}
private void init_treeView()
{
test.data.dataconnection da = new test.data.dataconnection();
DataTable dt = da.readdate("select * from cata");
TreeNode[] node = new TreeNode[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
node[i] = new TreeNode(dt.Rows[i]["cataname"].ToString(), dt.Rows[i]["id"].ToString());
}
for (int i = 0; i < node.Length; i++)
{
DataView dv = dt.DefaultView;
dv.RowFilter = "cataname='" + node[i].Text + "'";
if (dv[0]["topid"].ToString() == "0")
this.TreeView1.Nodes.Add(node[i]);
else
{
dv.RowFilter = "id=" + dv[0]["topid"].ToString();
for (int j = 0; j < node.Length; j++)
if (node[j].Text == dv[0]["cataname"].ToString())
{ node[j].ChildNodes.Add(node[i]); break; }
}
}
}
private void findNode(int id, ref TreeNode tn, TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
if (id.ToString() == node.Value)
{
tn = node;
break;
}
if (node.ChildNodes.Count > 0)
{
//改错1、修改方法调用
findNode(id, tn, node.ChildNodes);
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
this.Panel1.Visible = true;
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
this.newscata.Text = this.TreeView1.SelectedNode.Text;
this.hidetext.Text = this.TreeView1.SelectedValue;
this.Panel1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
//4、添加代码,根据新闻是新发布还是修改的,分别调用news类的publishNews方法和updateNews保存新闻
test.data.dataconnection da = new test.data.dataconnection();
SqlParameter[] sp = new SqlParameter[2];
sp[0] = new SqlParameter("@author", author.Text);
sp[1] = new SqlParameter("@title", title.Text);
DataTable dt=da.readdate ("select count(*) from news where author=@author and title=@title",sp);
if (Convert.ToInt32(dt.Rows[0][0]) == 0)
test.business.news.publishNews();
else test.business.news.updateNews();
}
}
}