<%@ Control Language="C#"
AutoEventWireup="true"
CodeFile="Control.ascx.cs"
Inherits="LinkTable" %>
Font-Size="Small"
Font-Names="Verdana"
Font-Bold="True"
ForeColor="#C00000"
runat="server">[Title Goes Here]
Font-Names="Verdana"
Font-Size="XX-Small"
ForeColor="#0000cd"
runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Text") %>'
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Url") %>'>
File: Control.ascx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class LinkTable : System.Web.UI.UserControl
{
public event LinkClickedEventHandler LinkClicked;
public string Title
{
get { return lblTitle.Text; }
set { lblTitle.Text = value; }
}
public LinkTableItem[] items;
public LinkTableItem[] Items
{
get { return items; }
set
{
items = value;
listContent.DataSource = items;
listContent.DataBind();
}
}
public void listContent_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if (LinkClicked != null)
{
LinkButton link = (LinkButton)e.Item.Controls[1];
LinkTableItem item = new LinkTableItem(link.Text, link.CommandArgument);
LinkTableEventArgs args = new LinkTableEventArgs(item);
LinkClicked(this, args);
if (!args.Cancel)
{
Response.Redirect(item.Url);
}
}
}
}
public class LinkTableItem
{
private string text;
public string Text
{
get { return text; }
set { text = value; }
}
private string url;
public string Url
{
get { return url; }
set { url = value; }
}
public LinkTableItem()
{ }
public LinkTableItem(string text, string url)
{
this.text = text;
this.url = url;
}
}
public class LinkTableEventArgs : EventArgs{
private LinkTableItem selectedItem;
public LinkTableItem SelectedItem
{
get { return selectedItem; }
}
private bool cancel = false;
public bool Cancel
{
get { return cancel; }
set { cancel = value; }
}
public LinkTableEventArgs(LinkTableItem item)
{
selectedItem = item;
}
}
public delegate void LinkClickedEventHandler(object sender,LinkTableEventArgs e);
File: Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="LinkTableHost" %>
<%@ Register Src="Control.ascx" TagName="LinkTable" TagPrefix="uc1" %>
Untitled Page
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class LinkTableHost : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
LinkTable1.Title = "A List of Links";
LinkTableItem[] items = new LinkTableItem[3];
items[0] = new LinkTableItem("Test Item 1", "http://www.rntsoft.com");
items[1] = new LinkTableItem("Test Item 2", "http://www.rntsoft.com");
items[2] = new LinkTableItem("Test Item 3", "http://www.rntsoft.com");
LinkTable1.Items = items;
}
public void LinkClicked(object sender, LinkTableEventArgs e)
{
lblInfo.Text = "You clicked '" + e.SelectedItem.Text + " URL:" + e.SelectedItem.Url + ".";
e.Cancel = true;
}
}