ASP.NET Tutorial/Custom Controls/Table — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:00, 26 мая 2010

User control: link table

<%@ Control Language="C#" 
            AutoEventWireup="true" 
            CodeFile="Control.ascx.cs" 
            Inherits="LinkTable" %>
<table border="1">
  <tr>
    <td width="100%" height="1">
      <asp:label id="lblTitle" 
                    Font-Size="Small" 
                    Font-Names="Verdana" 
                    Font-Bold="True" 
                    ForeColor="#C00000"
                runat="server">[Title Goes Here]</asp:label>
    </td>
  </tr>
  <tr>
    <td width="100%" height="1">
       <asp:datalist id="listContent" runat="server" OnItemCommand="listContent_ItemCommand">
       <ItemTemplate>
                <asp:LinkButton id="HyperLink1" 
                                Font-Names="Verdana" 
                                Font-Size="XX-Small"
                                ForeColor="#0000cd" 
                                runat="server" 
                                Text="<%# DataBinder.Eval(Container.DataItem, "Text") %>" 
                                CommandArgument="<%# DataBinder.Eval(Container.DataItem, "Url") %>">
                </asp:LinkButton>
             </ItemTemplate>
      </asp:datalist></td>
  </tr>
</table>
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.rumandArgument);
      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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:LinkTable ID="LinkTable1" runat="server" OnLinkClicked="LinkClicked"/>
        <br />
        <asp:Label ID="lblInfo" runat="server"></asp:Label>
    
    </div>
    </form>
</body>
</html>
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.nfex.ru");
    items[1] = new LinkTableItem("Test Item 2", "http://www.nfex.ru");
    items[2] = new LinkTableItem("Test Item 3", "http://www.nfex.ru");
    LinkTable1.Items = items;
  }
  public void LinkClicked(object sender, LinkTableEventArgs e)
  {
    lblInfo.Text = "You clicked "" + e.SelectedItem.Text + "  URL:" + e.SelectedItem.Url + ".";
    e.Cancel = true;
  }
}