Creating Hyperlinks

Discussion in 'ASP .Net' started by Patrick.O.Ige, Feb 6, 2006.

  1. Garth you can make use of a repeater.
    Just embed a hyerplink control in it and Databind it.
    Hope that helps
    Patrick

    "Garth Wells" <> wrote in message
    news:...
    > used the following "classic ASP" approach to build a dynamic menu, but

    would
    > like to know the proper way to implement the same functionality using a

    ..Net
    > technique (e.g., placing the code in the .cs file and dynamically building

    the
    > hyperlink controls). The controls need to appear within the same <TD>,
    > separated by two spaces
    >
    > Thanks for your help
    >
    >
    > -----
    > <%@ Import Namespace="Microsoft.Practices.EnterpriseLibrary.Data" %>
    > <%@ Import Namespace="Microsoft.Practices.EnterpriseLibrary.Data.Sql" %>
    > <%@ Import Namespace="System.Data" %>
    > <%@ Import Namespace="System.Data.Common" %>
    > <%@ Import Namespace="System.Data.SqlClient" %>
    >
    > <%@ Control Language="C#" AutoEventWireup="true"

    CodeFile="MenuSub.ascx.cs"
    > Inherits="SubMenu" %>
    > <table id="table1" cellSpacing="0" cellPadding="0" width="900" border="0">
    > <TR bgColor="#2a69b3">
    > <TD vAlign="middle" align="center" width="900" height="20">
    > <%
    > Database db = DatabaseFactory.CreateDatabase();
    > string sqlCommand = "pr_MenuSub_SELECT";
    > DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
    > db.AddInParameter(dbCommand, "MM_Description", DbType.String,

    "HR");
    >
    > using (IDataReader dataReader = db.ExecuteReader(dbCommand))
    > {
    > while (dataReader.Read())
    > {
    > // Get the value of the Name column in the

    DbDataReader.
    > Response.Write("<a href=" +
    > dataReader["MS_Page"].ToString() + ">" +

    dataReader["MS_Description"].ToString()
    > + "</href>");
    > Response.Write("&nbsp;&nbsp;");
    > }
    > }
    > %>
    > </TD>
    > </TR>
    > </table>
    >
    >
    >
    >
    Patrick.O.Ige, Feb 6, 2006
    #1
    1. Advertising

  2. You could actually do this in several ways. The trick is to think
    object-oriented. First, you'll need to place your databsae code within an
    event. You can't just place it inline as you could in classic asp. Your best
    bet is to place it in the Page_Load event of your MenuSub.ascx.cs file. You
    can do a couple of different things here.

    The easiest, is to create a Literal control at the location you want between
    the td elements like so:
    <TD vAlign="middle" align="center" width="900" height="20"><asp:Literal
    id="myLinks" runat="server"/></TD>

    Now, in the codebehind you'll need to ensure you have this declared as a
    variable as so:

    protected Literal myLinks;

    You can create your while loop just like you do, except instead of
    response.writing, concatenate it into a string. Once you're done with the
    look you can then dump the string which now holds the concactenated link
    strings right into the literal such as myLinks.Text = stringLinks;

    Of course, you could do it by creating a new HtmlAnchor object during each
    iteration, setting the values from the datareader, and then adding them as a
    child control to the literal. That would be the more object oriented
    approach and give you a bit more control later on if you want to tie
    server-side events such as an onlclick event to them, but initially you
    would just end up with using a bit more memory and slower response to create
    each object in the list.

    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP - FrontPage



    "Garth Wells" <> wrote in message
    news:...
    > used the following "classic ASP" approach to build a dynamic menu, but
    > would
    > like to know the proper way to implement the same functionality using a
    > .Net
    > technique (e.g., placing the code in the .cs file and dynamically building
    > the
    > hyperlink controls). The controls need to appear within the same <TD>,
    > separated by two spaces
    >
    > Thanks for your help
    >
    >
    > -----
    > <%@ Import Namespace="Microsoft.Practices.EnterpriseLibrary.Data" %>
    > <%@ Import Namespace="Microsoft.Practices.EnterpriseLibrary.Data.Sql" %>
    > <%@ Import Namespace="System.Data" %>
    > <%@ Import Namespace="System.Data.Common" %>
    > <%@ Import Namespace="System.Data.SqlClient" %>
    >
    > <%@ Control Language="C#" AutoEventWireup="true"
    > CodeFile="MenuSub.ascx.cs"
    > Inherits="SubMenu" %>
    > <table id="table1" cellSpacing="0" cellPadding="0" width="900" border="0">
    > <TR bgColor="#2a69b3">
    > <TD vAlign="middle" align="center" width="900" height="20">
    > <%
    > Database db = DatabaseFactory.CreateDatabase();
    > string sqlCommand = "pr_MenuSub_SELECT";
    > DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
    > db.AddInParameter(dbCommand, "MM_Description", DbType.String,
    > "HR");
    >
    > using (IDataReader dataReader = db.ExecuteReader(dbCommand))
    > {
    > while (dataReader.Read())
    > {
    > // Get the value of the Name column in the
    > DbDataReader.
    > Response.Write("<a href=" +
    > dataReader["MS_Page"].ToString() + ">" +
    > dataReader["MS_Description"].ToString()
    > + "</href>");
    > Response.Write("&nbsp;&nbsp;");
    > }
    > }
    > %>
    > </TD>
    > </TR>
    > </table>
    >
    >
    >
    >
    Mark Fitzpatrick, Feb 6, 2006
    #2
    1. Advertising

  3. For what you're describing, you would use a Repeater Control that is bound
    to a DataTable. The Repeater Control repeats whatever HTML and data you put
    into the Template.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Who is Mighty Abbott?
    A twin turret scalawag.

    "Garth Wells" <> wrote in message
    news:...
    > used the following "classic ASP" approach to build a dynamic menu, but
    > would
    > like to know the proper way to implement the same functionality using a
    > .Net
    > technique (e.g., placing the code in the .cs file and dynamically building
    > the
    > hyperlink controls). The controls need to appear within the same <TD>,
    > separated by two spaces
    >
    > Thanks for your help
    >
    >
    > -----
    > <%@ Import Namespace="Microsoft.Practices.EnterpriseLibrary.Data" %>
    > <%@ Import Namespace="Microsoft.Practices.EnterpriseLibrary.Data.Sql" %>
    > <%@ Import Namespace="System.Data" %>
    > <%@ Import Namespace="System.Data.Common" %>
    > <%@ Import Namespace="System.Data.SqlClient" %>
    >
    > <%@ Control Language="C#" AutoEventWireup="true"
    > CodeFile="MenuSub.ascx.cs"
    > Inherits="SubMenu" %>
    > <table id="table1" cellSpacing="0" cellPadding="0" width="900" border="0">
    > <TR bgColor="#2a69b3">
    > <TD vAlign="middle" align="center" width="900" height="20">
    > <%
    > Database db = DatabaseFactory.CreateDatabase();
    > string sqlCommand = "pr_MenuSub_SELECT";
    > DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
    > db.AddInParameter(dbCommand, "MM_Description", DbType.String,
    > "HR");
    >
    > using (IDataReader dataReader = db.ExecuteReader(dbCommand))
    > {
    > while (dataReader.Read())
    > {
    > // Get the value of the Name column in the
    > DbDataReader.
    > Response.Write("<a href=" +
    > dataReader["MS_Page"].ToString() + ">" +
    > dataReader["MS_Description"].ToString()
    > + "</href>");
    > Response.Write("&nbsp;&nbsp;");
    > }
    > }
    > %>
    > </TD>
    > </TR>
    > </table>
    >
    >
    >
    >
    Kevin Spencer, Feb 6, 2006
    #3
  4. Patrick.O.Ige

    Garth Wells Guest

    used the following "classic ASP" approach to build a dynamic menu, but would
    like to know the proper way to implement the same functionality using a .Net
    technique (e.g., placing the code in the .cs file and dynamically building the
    hyperlink controls). The controls need to appear within the same <TD>,
    separated by two spaces

    Thanks for your help


    -----
    <%@ Import Namespace="Microsoft.Practices.EnterpriseLibrary.Data" %>
    <%@ Import Namespace="Microsoft.Practices.EnterpriseLibrary.Data.Sql" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.Common" %>
    <%@ Import Namespace="System.Data.SqlClient" %>

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MenuSub.ascx.cs"
    Inherits="SubMenu" %>
    <table id="table1" cellSpacing="0" cellPadding="0" width="900" border="0">
    <TR bgColor="#2a69b3">
    <TD vAlign="middle" align="center" width="900" height="20">
    <%
    Database db = DatabaseFactory.CreateDatabase();
    string sqlCommand = "pr_MenuSub_SELECT";
    DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
    db.AddInParameter(dbCommand, "MM_Description", DbType.String, "HR");

    using (IDataReader dataReader = db.ExecuteReader(dbCommand))
    {
    while (dataReader.Read())
    {
    // Get the value of the Name column in the DbDataReader.
    Response.Write("<a href=" +
    dataReader["MS_Page"].ToString() + ">" + dataReader["MS_Description"].ToString()
    + "</href>");
    Response.Write("&nbsp;&nbsp;");
    }
    }
    %>
    </TD>
    </TR>
    </table>
    Garth Wells, Feb 7, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    Hyperlinks and viewstate

    John, Jul 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    371
    David Waz...
    Jul 6, 2003
  2. Guest
    Replies:
    0
    Views:
    432
    Guest
    Aug 19, 2003
  3. asha
    Replies:
    0
    Views:
    508
  4. Scott

    Creating hyperlinks?

    Scott, Jan 5, 2004, in forum: ASP General
    Replies:
    3
    Views:
    95
    Curt_C [MVP]
    Jan 5, 2004
  5. Fred

    Creating hyperlinks from text

    Fred, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    91
    Tad McClellan
    Oct 12, 2003
Loading...

Share This Page