;Method"> <HeaderStyle Wrap="False" HorizontalAlign="Center" Width="120px"></HeaderStyle> <ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle> </asp:BoundColumn> </Columns> </asp:datagrid></TD> </TR> </TABLE> </form> </body> </HTML>
下面是后台managertree.aspx.cs后台文件: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.IO; using System.Data.SqlClient; using System.Configuration; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
namespace hualong { /// <summary> /// manager 的摘要说明。 /// </summary> public class manager : System.Web.UI.Page { protected System.Web.UI.WebControls.Button btnTree; protected System.Web.UI.WebControls.TextBox txtId; protected System.Web.UI.WebControls.TextBox txtParentId; protected System.Web.UI.WebControls.Button btnAddOk; protected System.Web.UI.WebControls.TextBox txtHint; protected System.Web.UI.WebControls.TextBox txtIcon; protected System.Web.UI.WebControls.TextBox txtData; protected System.Web.UI.WebControls.TextBox txtUrl; protected System.Web.UI.WebControls.TextBox txtTarget; protected System.Web.UI.WebControls.TextBox txtMethod; protected System.Web.UI.WebControls.TextBox txtText; protected System.Web.UI.WebControls.DataGrid dgTree;
private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 if(!IsPostBack) { BindData(); } }
#region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); }
/// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.btnTree.Click += new System.EventHandler(this.btnTree_Click); this.btnAddOk.Click += new System.EventHandler(this.btnAddOk_Click); this.dgTree.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTree_CancelCommand); this.dgTree.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTree_EditCommand); this.dgTree.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTree_UpdateCommand); this.dgTree.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTree_DeleteCommand); this.Load += new System.EventHandler(this.Page_Load);
} #endregion
private void BindData() { string connString = ConfigurationSettings.AppSettings["connStr1"].ToString(); SqlConnection conn = new SqlConnection(connString); SqlCommand comm = new SqlCommand("select_treeview_pwqzc",conn); comm.CommandType = CommandType.StoredProcedure; conn.Open(); dgTree.DataSource = comm.ExecuteReader(); dgTree.DataBind(); conn.Close(); }
private void dgTree_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { dgTree.EditItemIndex = e.Item.ItemIndex; BindData(); }
private void dgTree_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { dgTree.EditItemIndex = -1; BindData(); }
private void btnTree_Click(object sender, System.EventArgs e) { string str;//用他来包含所有树的节点信息 string connectstring = System.Configuration.ConfigurationSettings.AppSettings["connStr1"].ToString(); System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectstring); System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter("select_treeview_pwqzc",conn); try { System.Text.StringBuilder node = new System.Text.StringBuilder(); conn.Open(); System.Data.DataSet dataset = new System.Data.DataSet(); dataset.Tables.Add("treeview"); adapter.Fill(dataset,"treeview"); foreach(System.Data.DataRow row in dataset.Tables[0].Rows) { node.Append("\r\n tree.nodes[\""+ row["parentId"].ToString() + "_" + row["id"] + "\"] = \""); node.Append("text:" + row["text"].ToString().Replace("\0xE",";") + ";"); node.Append(row["hint"] != DBNull.Value && row["hint"].ToString() != string.Empty ? "hint:" + row["hint"].ToString().Replace("\0xE",";") + ";" : string.Empty); node.Append(row["icon"] != DBNull.Value && row["icon"].ToString() != string.Empty ? "icon:" + row["icon"].ToString().Replace("\0xE",";") + ";" : string.Empty); node.Append(row["data"] != DBNull.Value && row["data"].ToString() != string.Empty ? "data:" + row["data"].ToString().Replace("\0xE",";") +Server.UrlEncode(row["text"].ToString())+ ";" : string.Empty); node.Append(row["url"] != DBNull.Value && row["url"].ToString() != string.Empty ? "url:" + row["url"].ToString().Replace("\0xE",";") + ";" : string.Empty); node.Append(row["target"] != DBNull.Value && row["target"].ToString() != string.Empty ? "target:" + row["target"].ToString().Replace("\0xE",";") + ";" : string.Empty); node.Append(row["method"] != DBNull.Value && row["method"].ToString() != string.Empty ? "method:" + row["method"].ToString().Replace("\0xE",";") + ";" : string.Empty); node.Append("\"");
}
str = node.ToString(); dataset.Dispose(); } finally { conn.Close();
} StreamReader sr = new StreamReader(Server.MapPath(".")+"\\bbs\\treetemplate.htm",System.Text.Encoding.GetEncoding("gb2312"));//读取模板 string str1 = sr.ReadToEnd(); str1 = str1.Replace("abcdefg",str);//替换摸板里面的字符串 sr.Close();//关闭sr StreamWriter sw = new StreamWriter(Server.MapPath(".")+"\\bbs\\left.htm",false,System.Text.Encoding.GetEncoding("gb2312"));//写入文件left.htm sw.Write(str1); sw.Flush(); sw.Close(); Response.Write("<script language=\"javascript\">alert(''''生成树成功!'''');</script>"); }
private void btnAddOk_Click(object sender, System.EventArgs e) { if(txtId.Text.Trim()=="" || txtParentId.Text.Trim()=="") { Page.RegisterStartupScript("","<script>alert(''''节点值与父节点值不能为空!而且只能够是整数!'''');document.getElementById(''''TianJia'''').style.display='''''''';</script>"); return; } else { string connString = ConfigurationSettings.AppSettings["connStr1"].ToString(); SqlConnection conn = new SqlConnection(connString); string selString = "select Count(id) from treeview where ID = @id"; SqlCommand comm = new SqlCommand(selString,conn); comm.Parameters.Add(new SqlParameter("@id",SqlDbType.Int)); comm.Parameters["@id"].Value = Convert.ToInt32(txtId.Text.Trim()); conn.Open(); int i = (int)comm.ExecuteScalar(); conn.Close(); if(i>0) { Page.RegisterStartupScript("","<script>alert(''''已经存在相同ID值的节点,请重新输入!'''');document.getElementById(''''TianJia'''').style.display='''''''';</script>"); return; } else { string insertString = "insert into treeview(ID,parentId,text,hint,icon,data,url,target,method) values(@ID,@parentId,@text,@hint,@icon,@data,@url,@target,@method)"; //插入数据库命令 SqlCommand comm1 = new SqlCommand(insertString,conn); comm1.Parameters.Add(new SqlParameter("@ID",SqlDbType.Int)); comm1.Parameters.Add(new SqlParameter("@parentId",SqlDbType.Int)); comm1.Parameters.Add(new SqlParameter("@text",SqlDbType.VarChar,255)); comm1.Parameters.Add(new SqlParameter("@hint",SqlDbType.VarChar,255)); comm1.Parameters.Add(new SqlParameter("@icon",SqlDbType.VarChar,50)); comm1.Parameters.Add(new SqlParameter("@data",SqlDbType.VarChar,255)); comm1.Parameters.Add(new SqlParameter("@url",SqlDbType.VarChar,255)); comm1.Parameters.Add(new SqlParameter("@target",SqlDbType.VarChar,50)); comm1.Parameters.Add(new SqlParameter("@method",SqlDbType.VarChar,255)); //上面都是添加参数,下面是给参数赋值 comm1.Parameters["@ID"].Value = Convert.ToInt32(txtId.Text.Trim()); comm1.Parameters["@parentId"].Value = Convert.ToInt32(txtParentId.Text.Trim()); comm1.Parameters["@text"].Value = txtText.Text.Trim(); comm1.Parameters["@hint"].Value = txtHint.Text.Trim(); comm1.Parameters["@icon"].Value = txtIcon.Text.Trim(); comm1.Parameters["@data"].Value = txtData.Text.Trim(); comm1.Parameters["@url"].Value = txtUrl.Text.Trim(); comm1.Parameters["@target"].Value = txtTarget.Text.Trim(); comm1.P 上一页 [1] [2] [3] 下一页 没有相关教程
|