我在我的页面中使用ASP.NET TreeView Control。 我使用CreateChildControls()(覆盖)构建树。
protected override void CreateChildControls()
{
base.CreateChildControls();
TVOrg = this.FindControl<TreeView>("TVOrg");
try
{
DataTable itemsTable = CreateTable();
if (itemsTable != null)
{
XmlDocument xmlDoc = CreateXML(CreateDataRow(itemsTable));
if (xmlDoc != null)
FillTreeViewFromXML(xmlDoc);
}
}
catch (Exception ex)
{
PNMsoft.Sequence.Diagnostics.DiagnosticUtility.EventLog.LogError(String.Format("TreeViewOrg.aspx: OnPreRender(), {0}", ex.Message));
}
}
在这个方法中,我从sql视图中获取树的层次结构。
public DataTable CreateTable()
{
DataTable dataTable = new DataTable();
try
{
// base.DbConnection.Open();
dataTable.Columns.Add(idColumnName);
dataTable.Columns.Add(ParentColumnName);
dataTable.Columns.Add(valueColumnName);
List<DBParameter> prms = new List<DBParameter>();
dataTable = DBUtil.ExecuteReader("sp_GetTreeViewHierarchy", prms.ToArray(), System.Data.CommandType.StoredProcedure);
}
catch (Exception ex)
{
PNMsoft.Sequence.Diagnostics.DiagnosticUtility.EventLog.LogError(String.Format("TreeViewOrg.aspx: CreateTable(), {0}", ex.Message));
}
return dataTable;
}
之后,我正在创建一个XML文件(在内存中)并使用XmlDataSource来绑定数据。
public DataRow[] CreateDataRow(DataTable table)
{
// Use the Select method to sort the rows by ParentID
DataRow[] SortedRows = null;
try
{
SortedRows = table.Select("", levelColumnName + "," + valueColumnName + "," + ParentColumnName);
}
catch (Exception ex)
{
PNMsoft.Sequence.Diagnostics.DiagnosticUtility.EventLog.LogError(String.Format("TreeViewOrg.aspx: CreateDataRow(), {0}", ex.Message));
}
return SortedRows;
}
public XmlDocument CreateXML(DataRow[] SortedRows)
{
// create an XmlDocument (with an XML declaration)
XmlDocument XDoc = new XmlDocument();
try
{
XmlDeclaration XDec = XDoc.CreateXmlDeclaration("1.0", null, null);
XDoc.AppendChild(XDec);
// iterate through the sorted data
// and build the XML document
foreach (DataRow Row in SortedRows)
{
XmlElement NewNode = XDoc.CreateElement("_" + Row[idColumnName].ToString());
NewNode.SetAttribute(idColumnName, Row[idColumnName].ToString());
NewNode.SetAttribute(ParentColumnName, Row[ParentColumnName].ToString());
NewNode.SetAttribute(valueColumnName, Row[valueColumnName].ToString());
// special case for top level node
if (int.Parse(Row[ParentColumnName].ToString()) == 0)
XDoc.AppendChild(NewNode); // root node
else
{
// use XPath to find the parent node in the tree
String SearchString;
SearchString = String.Format("//*[@" + idColumnName + "=\"{0}\"] ", Row[ParentColumnName].ToString());
XmlNode Parent = XDoc.SelectSingleNode(SearchString);
if (Parent != null)
Parent.AppendChild(NewNode);
}
}
}
catch (Exception ex)
{
PNMsoft.Sequence.Diagnostics.DiagnosticUtility.EventLog.LogError(String.Format("TreeViewOrg.aspx: CreateXML(), {0}", ex.Message));
}
return XDoc;
}
public void FillTreeViewFromXML(XmlDocument XDoc)
{
try
{
// we cannot bind the TreeView directly to an XmlDocument
// so we must create an XmlDataSource and assign the XML text
XmlDataSource XDdataSource = new XmlDataSource();
XDdataSource.ID = DateTime.Now.Ticks.ToString(); // unique ID is required
XDdataSource.Data = XDoc.OuterXml;
// we want the full name displayed in the tree so
// do custom databindings
TreeNodeBinding Binding = new TreeNodeBinding();
Binding.TextField = valueColumnName;
Binding.ValueField = idColumnName;
Binding.Target = "#";
TVOrg.DataBindings.Add(Binding);
// Finally! Hook that bad boy up!
TVOrg.DataSource = XDdataSource;
TVOrg.DataBind();
}
catch (Exception ex)
{
PNMsoft.Sequence.Diagnostics.DiagnosticUtility.EventLog.LogError(String.Format("TreeViewOrg.aspx: FillTreeViewFromXML(), {0}", ex.Message));
}
}
我通过填写表单创建一个新节点,而不是调用此方法。
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
var entityIdHidden = this.FindControl<HiddenField>("SelectedNodeId");
if (entityIdHidden != null)
if (entityIdHidden.Value == "")
if (!CheckifSiteAlreadyExists())
return;
if (!CheckIfEntityHasParent())
return;
var user = AuthenticationManager.LoggedInUser;
WorkflowAPI api = new WorkflowAPI();
Dictionary<string, object> entityFields = GetEntityControls();
AttachmentEntity image = AttchmentItem("AsyncFileUpload2");
if (image != null)
entityFields.Add("fldImage", image);
int entityId = 0;
bool isSite = CheckIfEntityIsSite();
if ((entityIdHidden != null) && (!string.IsNullOrEmpty(entityIdHidden.Value)))
{
int.TryParse(entityIdHidden.Value, out entityId);
}
api.UpdateACtivityFields(WorkflowInstanceId, user, this.GetFormUIViewModel().ActivityInstance, entityFields, entityId, "EntityType");
List<DBParameter> prms = new List<DBParameter>();
prms.Add(new DBParameter(System.Data.DbType.Int32, WorkflowInstanceId, "iwf"));
prms.Add(new DBParameter(System.Data.DbType.Int32, entityId, "entityId"));
int.TryParse(DBUtil.ExecuteScalar("USP_TEN_Entities_UpdateDynamicsValues", prms.ToArray(),
CommandType.StoredProcedure).ToString(), out entityId);
Dictionary<string, object> siteFields;
if (isSite)
{
siteFields = GetSiteControls();
siteFields.Add("fldParentid", entityId);
api.UpdateACtivityFields(WorkflowInstanceId, user, this.GetFormUIViewModel().ActivityInstance, siteFields, entityId, "SiteDetails");
}
entityIdHidden.Value = entityId.ToString();
this.CreateChildControls();
//Response.Redirect(Request.RawUrl);
}
catch (Exception ex)
{
PNMsoft.Sequence.Diagnostics.DiagnosticUtility.EventLog.LogError(String.Format("TreeViewOrg.aspx: btnSave_Click(), {0}", ex.Message));
}
}
此方法使用我输入的详细信息更新数据库。
正如您在最后的btnSubmit方法中所看到的,我调用了CreateChildControls()方法,因为我需要使用最新数据更新树。 问题是我失去了焦点和SelectedNode。
如何返回上一个选定节点。
答案 0 :(得分:1)
首先请检查 CreateChildControls()功能是否在 IsPostBack()条件下。如果是,请浏览此链接 - Set Node in Treeview - 这将指导您如何操作。