Tuesday 18 August 2009

TreeView - Some Useful Code

Well don't know about everyone else, but I think the treeview control is one of the worst controls I have seen simple because of the way you have to code the collections and there isn't even a basic property for the index. Also the webforms version is very different from the winforms control.

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

I created this small Javascript to markup text with html links. The use of this is for advertising or other site affiliation.

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

When starting to program in c# .net, it helps to use a class provided by Microsoft which allows you to code more quickly and effectively. Instead of creating several lines of code to perform a database task you can simply just use a couple of lines.

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