Netferrets Online Blog, Internet Related Programming Solutions, Created in C# .net, VB .net, MS SQL Server Javascript, HTML and CSS. Proving sample solutions to problems found when creating web application solutions.
Tuesday, 18 August 2009
TreeView - Some Useful Code
Anyway, after lots of messing about here is some code you might find useful:
//Add Root with Text
TreeNode root = new TreeNode();
root.Text = "Root Text";
TreeView1.Nodes.Add(root);
//Get Index of Selected Item
int myindex = TreeView1.SelectedNode.Parent.ChildNodes.IndexOf(TreeView1.SelectedNode);
//Remove Selected Child
try
{
int myindex = TreeView1.SelectedNode.Parent.ChildNodes.IndexOf(TreeView1.SelectedNode);
string removenode = TreeView1.SelectedNode.ValuePath;
TreeNode MyNodeToDelete = TreeView1.FindNode(removenode);
MyNodeToDelete.Parent.ChildNodes.Remove(MyNodeToDelete);
}
catch (Exception ex) {
}
//Remove Parent Node
try {
int myindex = TreeView1.SelectedNode.Parent.ChildNodes.IndexOf(TreeView1.SelectedNode);
string removenode = TreeView1.SelectedNode.ValuePath;
TreeNode MyNodeToDelete = TreeView1.FindNode(removenode);
MyNodeToDelete.Parent.ChildNodes.Remove(MyNodeToDelete);
}
catch (Exception ex)
{
}
I apologize for the code formatting, I will try to rectify this as soon as I have chance.
Saturday, 15 August 2009
Javascript Highlight Text and Dynamic Creation of Links
The text that you wish to markup is in an array as well as the url and description. This allows you to add as many text links as you wish and obviously you could make this database driven with some additional coding.
I have also created a div for the containing text, this ensures they other elements are not affected outside the div tag.
<script>
var word = new Array('The','Javascript','Functions');
var url = new Array('http://www.google.co.uk', 'http://www.yahoo.co.uk','http://www.lycos.co.uk');
var desc = new Array('http://www.google.co.uk','http://www.yahoo.co.uk','http://www.lycos.co.uk');
function generatelinks() {
// Target the mytext div
var bdy = document.getElementById('mytext').innerHTML;
for (var i = word.length - 1; i >= 0; i--) {
var re = new RegExp('(\\b' + word[i] + '\\b)','ig');
bdy = bdy.replace(re,'<a class="hl" href="' + url[i] + '" title="' + desc[i] + '" target="_blank">$1<\/a>');
}
document.getElementById('mytext').innerHTML = bdy;
}
// run script on page start
window.onload = generatelinks;
</script>
<style>
.hl {
cursor:pointer;
color:#009999;
border-bottom-style: dashed;
border-bottom-color: #009999;
border-bottom-width: 1px;
text-decoration: none;
}
</style>
<div id="mytext">
Your Text Here
</div>
Here is an example of how it should look:
SQLHelper - Microsoft Data Access Application Block
This is a really basic example of how the class can be used.
Add this namespace to your page
using Microsoft.ApplicationBlocks.Data;
// Setup Connection
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
// Open datareader to read through our items
SqlDataReader dr = SqlHelper.ExecuteReader(cn, CommandType.Text, "SELECT * FROM tbl_blogs ORDER BY title");
// See if we have any data and if so then cycle through the datareader
while (dr.Read())
{
Response.Write(Convert.ToString(dr["title"]) + ""<br />");
}
// Close the datareader or we cant use the connection for other operations
dr.Close();
}
}
There is more information available on http://msdn.microsoft.com/en-us/library/ms954827.aspx