link text and a href from db table

Discussion in 'ASP .Net Web Controls' started by Rod Snyder, Jan 22, 2004.

  1. Rod  Snyder

    Rod Snyder Guest

    For an asp.net page, I am going to have to pull the text of an a href and
    the actual url from two fields of a sql server table. I've done this before
    with classic asp where I just put the url value in an a href tag and wrap it
    around the text output of the link, but I'm wondering is there is a more
    ".NET" way to accomplish this using controls. Any suggestions or direction
    to articles would be appreciated.

    Rod
    Rod Snyder, Jan 22, 2004
    #1
    1. Advertising

  2. Hi Rod,

    >>>where I just put the url value in an a href tag and wrap it around the

    text output of the link

    You could do about the same in ASP.NET, that is generating by hand a string
    containing an anchor tag with proper attributes set. You could also use a
    server-side control, ie:

    HyperLink hl = new HyperLink();
    hl.NavigateUrl = "YourURL"
    // insert hl into the Controls collection of the parent control (ie.Form,
    etc)

    Or you could also use an HtmlTextWriter, take a look at its Render* methods,
    AddAttributes, etc.

    Take a look in the docs at the differences between each one of them. You
    should choose one based on your specific reqs.

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx

    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    "Rod Snyder" <> wrote in message
    news:%...
    > For an asp.net page, I am going to have to pull the text of an a href and
    > the actual url from two fields of a sql server table. I've done this

    before
    > with classic asp where I just put the url value in an a href tag and wrap

    it
    > around the text output of the link, but I'm wondering is there is a more
    > ".NET" way to accomplish this using controls. Any suggestions or direction
    > to articles would be appreciated.
    >
    > Rod
    >
    >
    Victor Garcia Aprea [MVP], Jan 22, 2004
    #2
    1. Advertising

  3. Hi Rod ,

    Thank you for posting in the community! My name is Jeffrey, and I will be
    assisting you on this issue.

    Based on my understanding, you want to create a hyperlink whoes link text
    and navigation Url both are retrieved from Sql Server databse fields.

    ======================================================
    Actually, .Net encapsulate the hyperlink html tag <a> into a web control.
    You can manipulate its link text and navigation url through Text property
    and NavigateUrl property.

    So you can do like this:
    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    SqlDataAdapter adapter=new SqlDataAdapter("select * from
    test","server=localhost;database=pubs;uid=sa;pwd=");
    DataSet ds=new DataSet();
    adapter.Fill(ds);

    foreach(DataRow dr in ds.Tables[0].Rows)
    {
    HyperLink hl=new HyperLink();
    hl.Text=(string)dr["link_text"];
    hl.NavigateUrl=(string)dr["link"];

    this.Page.FindControl("Form1").Controls.Add(hl);
    }
    }

    In the sample code, I create all the hyperlink control dynamicly, then add
    them to the "Form1". I retrieve all the data from "test" table in Sql
    Server's pubs database.
    ======================================================
    Please apply my suggestion above and let me know if it helps resolve your
    problem.

    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.
    Have a nice day!!

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Jan 23, 2004
    #3
  4. Rod  Snyder

    CMA Guest

    this description is not much detailed, what you need.
    anyway i think this will help you.
    you need to get a set of values from the database like...

    TEXT URL
    ------- ------
    google www.google.com
    yahoo www.yahoo.com
    ....
    ....
    like wise.. right?
    so the best way to use a DATAGRID control.
    http://samples.gotdotnet.com/quicks...ms/ctrlref/webctrl/datagrid/doc_datagrid.aspx
    and learn about hyperlink column....

    regards,
    CMA


    "Rod Snyder" <> wrote in message
    news:%...
    > For an asp.net page, I am going to have to pull the text of an a href and
    > the actual url from two fields of a sql server table. I've done this

    before
    > with classic asp where I just put the url value in an a href tag and wrap

    it
    > around the text output of the link, but I'm wondering is there is a more
    > ".NET" way to accomplish this using controls. Any suggestions or direction
    > to articles would be appreciated.
    >
    > Rod
    >
    >
    CMA, Jan 23, 2004
    #4
  5. Rod  Snyder

    Rod Snyder Guest

    Thanks. This helps alot.

    "Victor Garcia Aprea [MVP]" <> wrote in message
    news:OK$...
    > Hi Rod,
    >
    > >>>where I just put the url value in an a href tag and wrap it around the

    > text output of the link
    >
    > You could do about the same in ASP.NET, that is generating by hand a

    string
    > containing an anchor tag with proper attributes set. You could also use a
    > server-side control, ie:
    >
    > HyperLink hl = new HyperLink();
    > hl.NavigateUrl = "YourURL"
    > // insert hl into the Controls collection of the parent control (ie.Form,
    > etc)
    >
    > Or you could also use an HtmlTextWriter, take a look at its Render*

    methods,
    > AddAttributes, etc.
    >
    > Take a look in the docs at the differences between each one of them. You
    > should choose one based on your specific reqs.
    >
    > --
    > Victor Garcia Aprea
    > Microsoft MVP | ASP.NET
    > Looking for insights on ASP.NET? Read my blog:
    > http://obies.com/vga/blog.aspx
    >
    > To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    > "Rod Snyder" <> wrote in message
    > news:%...
    > > For an asp.net page, I am going to have to pull the text of an a href

    and
    > > the actual url from two fields of a sql server table. I've done this

    > before
    > > with classic asp where I just put the url value in an a href tag and

    wrap
    > it
    > > around the text output of the link, but I'm wondering is there is a more
    > > ".NET" way to accomplish this using controls. Any suggestions or

    direction
    > > to articles would be appreciated.
    > >
    > > Rod
    > >
    > >

    >
    >
    Rod Snyder, Jan 23, 2004
    #5
  6. Rod  Snyder

    Rod Snyder Guest

    Thanks for the reply. This is pretty much what I was looking for.
    Rod
    ""Jeffrey Tan[MSFT]"" <> wrote in message
    news:...
    >
    > Hi Rod ,
    >
    > Thank you for posting in the community! My name is Jeffrey, and I will be
    > assisting you on this issue.
    >
    > Based on my understanding, you want to create a hyperlink whoes link text
    > and navigation Url both are retrieved from Sql Server databse fields.
    >
    > ======================================================
    > Actually, .Net encapsulate the hyperlink html tag <a> into a web control.
    > You can manipulate its link text and navigation url through Text property
    > and NavigateUrl property.
    >
    > So you can do like this:
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > // Put user code to initialize the page here
    > SqlDataAdapter adapter=new SqlDataAdapter("select * from
    > test","server=localhost;database=pubs;uid=sa;pwd=");
    > DataSet ds=new DataSet();
    > adapter.Fill(ds);
    >
    > foreach(DataRow dr in ds.Tables[0].Rows)
    > {
    > HyperLink hl=new HyperLink();
    > hl.Text=(string)dr["link_text"];
    > hl.NavigateUrl=(string)dr["link"];
    >
    > this.Page.FindControl("Form1").Controls.Add(hl);
    > }
    > }
    >
    > In the sample code, I create all the hyperlink control dynamicly, then add
    > them to the "Form1". I retrieve all the data from "test" table in Sql
    > Server's pubs database.
    > ======================================================
    > Please apply my suggestion above and let me know if it helps resolve your
    > problem.
    >
    > Thank you for your patience and cooperation. If you have any questions or
    > concerns, please feel free to post it in the group. I am standing by to be
    > of assistance.
    > Have a nice day!!
    >
    > Best regards,
    > Jeffrey Tan
    > Microsoft Online Partner Support
    > Get Secure! - www.microsoft.com/security
    > This posting is provided "as is" with no warranties and confers no rights.
    >
    Rod Snyder, Jan 23, 2004
    #6
  7. Rod  Snyder

    Rod Snyder Guest

    thanks for the reply. I'll check out the article on the datagrid.
    Rod

    "CMA" <> wrote in message
    news:%...
    > this description is not much detailed, what you need.
    > anyway i think this will help you.
    > you need to get a set of values from the database like...
    >
    > TEXT URL
    > ------- ------
    > google www.google.com
    > yahoo www.yahoo.com
    > ...
    > ...
    > like wise.. right?
    > so the best way to use a DATAGRID control.
    >

    http://samples.gotdotnet.com/quicks...ms/ctrlref/webctrl/datagrid/doc_datagrid.aspx
    > and learn about hyperlink column....
    >
    > regards,
    > CMA
    >
    >
    > "Rod Snyder" <> wrote in message
    > news:%...
    > > For an asp.net page, I am going to have to pull the text of an a href

    and
    > > the actual url from two fields of a sql server table. I've done this

    > before
    > > with classic asp where I just put the url value in an a href tag and

    wrap
    > it
    > > around the text output of the link, but I'm wondering is there is a more
    > > ".NET" way to accomplish this using controls. Any suggestions or

    direction
    > > to articles would be appreciated.
    > >
    > > Rod
    > >
    > >

    >
    >
    Rod Snyder, Jan 23, 2004
    #7
  8. Hi Rod,

    The easy way is to use a repeater and a hyperlink control in ASP.NET. You
    set the datasource of the repeater. The hyperlink embedded inside the
    repeater can get the URL and text from its container.

    <form id="Form1" method="post" runat="server">
    <asp:Repeater id="Repeater1" runat="server">
    <ItemTemplate>
    <asp:HyperLink id="HyperLink1"
    NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "URLValue")
    %>'
    runat="server"><%# DataBinder.Eval(Container.DataItem,
    "TxtValue") %>
    </asp:HyperLink><br>
    </ItemTemplate>
    </asp:Repeater>
    </form>


    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    Repeater1.DataSource = CreateDataSource()
    Repeater1.DataBind()
    End Sub
    Function CreateDataSource() As DataTable
    Dim dt As New DataTable
    Dim dr As DataRow
    dt.Columns.Add(New DataColumn _
    ("TxtValue", GetType(String)))
    dt.Columns.Add(New DataColumn _
    ("URLValue", GetType(String)))
    Dim i As Integer
    For i = 0 To 8
    dr = dt.NewRow()
    dr(0) = "Item " & i.ToString()
    dr(1) = "destin" & i.ToString() & ".aspx"
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function 'CreateDataSource

    Does this help?

    Ken
    Microsoft MVP [ASP.NET]

    "Rod Snyder" <> wrote in message
    news:%...
    > For an asp.net page, I am going to have to pull the text of an a href and
    > the actual url from two fields of a sql server table. I've done this
    > before
    > with classic asp where I just put the url value in an a href tag and wrap
    > it
    > around the text output of the link, but I'm wondering is there is a more
    > ".NET" way to accomplish this using controls. Any suggestions or direction
    > to articles would be appreciated.
    >
    > Rod
    >
    >
    Ken Cox [Microsoft MVP], Jan 24, 2004
    #8
  9. Rod  Snyder

    Rod Snyder Guest

    Yes sir, it does. That's a really neat way of handling it. Thanks.
    Rod
    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:...
    > Hi Rod,
    >
    > The easy way is to use a repeater and a hyperlink control in ASP.NET. You
    > set the datasource of the repeater. The hyperlink embedded inside the
    > repeater can get the URL and text from its container.
    >
    > <form id="Form1" method="post" runat="server">
    > <asp:Repeater id="Repeater1" runat="server">
    > <ItemTemplate>
    > <asp:HyperLink id="HyperLink1"
    > NavigateUrl='<%# DataBinder.Eval(Container.DataItem,

    "URLValue")
    > %>'
    > runat="server"><%# DataBinder.Eval(Container.DataItem,
    > "TxtValue") %>
    > </asp:HyperLink><br>
    > </ItemTemplate>
    > </asp:Repeater>
    > </form>
    >
    >
    > Private Sub Page_Load _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles MyBase.Load
    > Repeater1.DataSource = CreateDataSource()
    > Repeater1.DataBind()
    > End Sub
    > Function CreateDataSource() As DataTable
    > Dim dt As New DataTable
    > Dim dr As DataRow
    > dt.Columns.Add(New DataColumn _
    > ("TxtValue", GetType(String)))
    > dt.Columns.Add(New DataColumn _
    > ("URLValue", GetType(String)))
    > Dim i As Integer
    > For i = 0 To 8
    > dr = dt.NewRow()
    > dr(0) = "Item " & i.ToString()
    > dr(1) = "destin" & i.ToString() & ".aspx"
    > dt.Rows.Add(dr)
    > Next i
    > Return dt
    > End Function 'CreateDataSource
    >
    > Does this help?
    >
    > Ken
    > Microsoft MVP [ASP.NET]
    >
    > "Rod Snyder" <> wrote in message
    > news:%...
    > > For an asp.net page, I am going to have to pull the text of an a href

    and
    > > the actual url from two fields of a sql server table. I've done this
    > > before
    > > with classic asp where I just put the url value in an a href tag and

    wrap
    > > it
    > > around the text output of the link, but I'm wondering is there is a more
    > > ".NET" way to accomplish this using controls. Any suggestions or

    direction
    > > to articles would be appreciated.
    > >
    > > Rod
    > >
    > >

    >
    Rod Snyder, Jan 24, 2004
    #9
  10. Hi Rod,

    I am glad you got what you want.
    If you have any further concern, please feel free to tell me, I will work
    with you

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Jan 26, 2004
    #10
    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. CRON
    Replies:
    24
    Views:
    200,393
    Adrienne Boswell
    Jun 20, 2006
  2. saiho.yuen
    Replies:
    3
    Views:
    401
    kaeli
    Sep 14, 2004
  3. Replies:
    2
    Views:
    438
  4. Vincent van Beveren

    BASE HREF and A HREF="#" onclick="..."

    Vincent van Beveren, Jul 6, 2006, in forum: Javascript
    Replies:
    2
    Views:
    288
    Vincent van Beveren
    Jul 6, 2006
  5. David
    Replies:
    1
    Views:
    298
    David
    Dec 6, 2006
Loading...

Share This Page