子页面给父页面传值
我做了个主页面其中有个textbox 和hyberlink。hyberlink的连接是个子页面,子页面中有个treeview用来读取数据库中的表内数据。现在想在打开子页面后,在treeview中选择某个节点并获取选择节点的text并返回给父页面 赋值给textbox。
父页面代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Model
{
public partial class Mailbox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["location"].ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = Session["location"].ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using BLL;
namespace Model
{
public partial class WebForm3 : System.Web.UI.Page
{
private dataOperate dataoperate;
protected void Page_Load(object sender, EventArgs e)
{
dataoperate = new dataOperate();
if (!Page.IsPostBack)
{
tree.ShowLines = true;
tree.ShowExpandCollapse = true;
TreeNodeCollection tnc = new TreeNodeCollection();
tnc = tree.Nodes;
//开始调用tncXunHuan,数据库中fuid=9999为根节点
tncXunHuan(tnc,0);
}
}
//树形菜单
public void tncXunHuan(TreeNodeCollection tnc, int ParentNumber)
{
try
{
DataSet ds = new DataSet();
string sql = "select ID,ParentNumber,LocationNumber from EP_TblLocation where ParentNumber='" + ParentNumber + "'";
ds = dataoperate.getDataSet(sql);
// ----------------end------------数据库获取数据
//------------------start-----------根据数据结构向treeView添加节点
foreach (DataRow dr in ds.Tables[0].Rows)
{
int ID = Convert.ToInt16(dr["ID"]);
int parentNumber = Convert.ToInt16(dr["ParentNumber"]);
//int parentNumber = Convert.ToInt16(dr["ParentNumber"]);
string LocationNumber = dr["LocationNumber"].ToString();
TreeNode tn = new TreeNode();
tn.Text = LocationNumber;
//tn.NavigateUrl = "javascript:getBoundary('" + LocationNumber + "')";//调用前台js方法
//tn.ImageUrl = "images/file.png";//默认图标为file.png
tnc.Add(tn);
string tncword =Convert.ToString(ds.Tables[0].Rows.IndexOf(dr));
int tncInt =ds.Tables[0].Rows.IndexOf(dr);
//tncXunHuan(tnc[tncword].ChildNodes, ID);
tncXunHuan(tnc[tncInt].ChildNodes, ID); //----------递归调用
// tnc = tnc;
}
//------------------end-----------根据数据结构向treeView添加节点
//------------------start---------设置包含子节点的父节点的图标
if (ds.Tables[0].Rows.Count > 0)
{
//tnc[0].Parent.ImageUrl = "images/openfoldericon.png";//设置父文件图标
}
else
{
//tnc[0].Parent.ImageUrl = "images/file.png";
}
}
catch (Exception e) { return; }
}
protected void tree_SelectedNodeChanged(object sender, EventArgs e)
{
Session["location"] = tree.SelectedNode.Text;
Response.Redirect("Mailbox.aspx");
}
}
}