link text and a href from db table

R

Rod Snyder

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
 
V

Victor Garcia Aprea [MVP]

Hi Rod,
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
 
J

Jeffrey Tan[MSFT]

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.
 
R

Rod Snyder

Thanks. This helps alot.

Victor Garcia Aprea said:
Hi Rod,
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
wrap
 
K

Ken Cox [Microsoft MVP]

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]
 
R

Rod Snyder

Yes sir, it does. That's a really neat way of handling it. Thanks.
Rod
Ken Cox said:
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 said:
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
 
J

Jeffrey Tan[MSFT]

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top