ASP.NET Tutorial/ASP.net Controls/HyperLink

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

HyperLink: Visible, Text, ToolTip, NavigateUrl

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="HyperLinkTest" %> <!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>HyperLink Test</title>

</head> <body>

  <form id="form1" runat="server">

HyperLink Test

     Use list to specify link:
<asp:DropDownList ID="drpLinks" Runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpLinks_SelectedIndexChanged"> <asp:ListItem>Select a company</asp:ListItem> <asp:ListItem>adobe</asp:ListItem> <asp:ListItem>ibm</asp:ListItem> <asp:ListItem>microsoft</asp:ListItem> </asp:DropDownList> Here is a link: <asp:HyperLink ID="hypTest" Runat="server" />
  </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 HyperLinkTest : System.Web.UI.Page {

 protected void Page_Load(object sender, System.EventArgs e)
 {
   if (drpLinks.SelectedIndex == 0)
     hypTest.Visible = false;
 }
 protected void drpLinks_SelectedIndexChanged(object sender, System.EventArgs e)
 {
   if (drpLinks.SelectedIndex > 0)
   {
     string company = drpLinks.SelectedItem.Text;
     hypTest.Visible = true;
     hypTest.Text = company;
     hypTest.ToolTip = "Go to the web site of " + company;
     hypTest.NavigateUrl = "http://www." + company + ".ru";
   }
 }

}</source>


Using the HyperLink Control

   <source lang="csharp">

Important properties of HyperLink control Enabled: disable the hyperlink. ImageUrl: an image for the hyperlink. NavigateUrl:URL represented by the hyperlink. Target: open a new window. Text: label the hyperlink.

<%@ Page Language="C#" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   void Page_Load()
   {
       lnkRandom.NavigateUrl = GetRandomFile();
   }
   string GetRandomFile()
   {
       string[] files = Directory.GetFiles(MapPath(Request.ApplicationPath), "*.aspx");
       Random rnd = new Random();
       string rndFile = files[rnd.Next(files.Length)];
       return Path.GetFileName(rndFile);
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Show HyperLink</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:HyperLink
       id="lnkRandom"
       Text="Random Link"
       Runat="server" />
   </form>

</body> </html></source>