We help IT Professionals succeed at work.

Pass a variable to hyperlink in code behind using visual basic.net

KavyaVS
KavyaVS asked
on
I have to pass a variable to  hyperlink  at the start ofthe page. When we click this link it will go to the "Useful Tips Section" inside the same page:
                    dttable = GetDataFromDb()
                    Dim dv As dataview
                     dv = New DataView(dttable)
                    Dim UsefulTips As String  
                    UsefulTips = dv(0)("Information").ToString
                    dtRow = New TableRow()
                    dtCell = New TableCell()
                    dtCell.Text = <li><a href="Info.aspx#UsefulTips">UsefulTips</a></li>
                    dtRow.Cells.Add(dtCell)
                    dttable1.Rows.Add(dtRow)

I Created a link to the "Useful Tips Section" following way

                    dtRow = New TableRow()
                    dtCell = New TableCell()
                    link = New HyperLink()
                    UsefulTips = dv(0)("Information").ToString
                    link.Attributes.Add("name",UsefulTips )
                    link.Attributes.Add("href", "javascript:void(0)")
                    dtCell.Controls.Add(link)
                    dtRow.Cells.Add(dtCell)
                    dtTalbe2.Rows.Add(dtRow)

It is not working.Please let me know how to make it work when we pass the variable to hyperlink.
The link test is variable and and when we click the link it will go
 the section in a page using variable.
Thanks
Comment
Watch Question

Top Expert 2013

Commented:
When you click the link does it respond in some way or do nothing?
Top Expert 2011

Commented:
What does the HTML look like?

If the target page is "info.aspx"
And the target on the page is coded as
<div name="UsefulTips">... something... </div>

Open in new window


Then the hyperlink should look like:
<a href="info.aspx#UsefulTips>link here</a>

Open in new window

Top Expert 2011

Commented:
Your questions seems to include two ways of how you created the link, but none of the how you created the section on the page that you are linking to.  Maybe I'm just misreading it though.
Top Expert 2011

Commented:
Another guess...
You gave "name" to the link and not to the section?

dtCell.Text = <li><a href="Info.aspx#UsefulTips">UsefulTips</a></li>

Open in new window


should be

dtCell.Text = <li><a name="UsefulTips">UsefulTips</a></li>

Open in new window


and

link.Attributes.Add("name",UsefulTips )
link.Attributes.Add("href", "javascript:void(0)")

Open in new window


should be

link.Attributes.Add("href", "#UsefulTips")

Open in new window

Top Expert 2013

Commented:
Another guess...

Guessing is not the best approach.  It just clogs up the thread and causes confusion. Ithink the user will post back with some additional information so we can get to a solution.  A little patience is a good thing some times.

Author

Commented:
I will divide my question into 2 parts.
1.   dttable = GetDataFromDb()
                    Dim dv As dataview
                     dv = New DataView(dttable)
                    Dim UsefulTips As String  
                    UsefulTips = dv(0)("Information").ToString
                    dtRow = New TableRow()
                    dtCell = New TableCell()
                    dtCell.Text = <li><a href="Info.aspx">UsefulTips</a></li>
                    dtRow.Cells.Add(dtCell)
                    dttable1.Rows.Add(dtRow)

How to add the variable as the link text. UsefulTips is a variable got it from data table.For me now it is showing just the UsefulTips. Not showing value stored in Usefultips.



2.When we click the link it will go to the UsefulTips section in the same page.
                      dttable = GetDataFromDb()
                    Dim dv As dataview
                     dv = New DataView(dttable)
                    Dim UsefulTips As String  
                    UsefulTips = dv(0)("Information").ToString
                    dtRow = New TableRow()
                    dtCell = New TableCell()
                    dtCell.Text = <li><a href="Info.aspx#UsefulTips">UsefulTips</a></li>
                    dtRow.Cells.Add(dtCell)
                    dttable1.Rows.Add(dtRow)

The section link is created as follows

                          dtRow = New TableRow()
                    dtCell = New TableCell()
                    link = New HyperLink()
                    UsefulTips = dv(0)("Information").ToString
                    link.Attributes.Add("name",UsefulTips )
                    link.Attributes.Add("href", "javascript:void(0)")
                    dtCell.Controls.Add(link)
                    dtRow.Cells.Add(dtCell)
                    dtTalbe2.Rows.Add(dtRow)

I hope it will clear idea about what I asked.
 The page is Info.aspx
The page has hyperlink.The Hyperlink text will be value stored in the  UsefulTips string variable.
when we click on that it should take us to Hyperlink Section on the same Page.
 
Please let me know if you hav any questions.

Thanks.        


                 
BRONZE EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
For 1, try

dtCell.Text = "<li><a href='Info.aspx'>" & UsefulTips & "</a></li>"

Author

Commented:
1st part worked.

How to add variable to name tag

               dtRow = New TableRow()
                    dtCell = New TableCell()
                    link = New HyperLink()
                    UsefulTips = dv(0)("Information").ToString
                    link.Attributes.Add("name",UsefulTips )
                    link.Attributes.Add("href", "javascript:void(0)")
                    dtCell.Controls.Add(link)
                    dtRow.Cells.Add(dtCell)
                    dtTalbe2.Rows.Add(dtRow)

This is the line I want to make it work.
link.Attributes.Add("name",UsefulTips)
Here UsefulTips is a string  variable.I want to assign the value stored in UsefulTips variable to name
How to assign it.

Thanks
BRONZE EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Did you try changing

 link.Attributes.Add("name",UsefulTips )

to

 link.Nname = UsefulTips

Author

Commented:
I tried it but it is giving me error.
Nname is not a member of System.Web.UI.WebControls.HyperLink

Please let me know how to set the name with Variable value.
Thanks
BRONZE EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Sorry it was supposed to be

 link.Name = UsefulTips

Author

Commented:
1. I tried it.But is giving the same error.
      'Name' is not a member of 'System.Web.UI.WebControls.HyperLink'.

This link is destination anchor for another link.

2.I want to set another link as source anchor
                      UsefulTips = dv(0)("Information").ToString
                     dtRow = New TableRow()
                    dtCell = New TableCell()
                    'dtCell.Text = "<li><a href=#UsefulTips>" & strProductDesc & "</a></li>"
                    'dtRow.Cells.Add(dtCell)
                    'dtTalbe3.Rows.Add(dtRow)
                    link = New HyperLink()
                    link.Text = UsefulTips
                    link.Attributes.Add("href", #UsefulTips)      

When we click the link on the second part it will go to the link on the first part.
Both links are in same page.
1st part link need to pass variable value as name.
2nd part link need to pass variable value as Href.

Please let me know how to make it work.
Thanks
BRONZE EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Sorry its the ID property not name.

Author

Commented:
I set the link ID property with variable value.This is the destination anchor for another link.
When we click on another it will come to the ID property link.

2.I want to set another link as source anchor
                      UsefulTips = dv(0)("Information").ToString
                     dtRow = New TableRow()
                    dtCell = New TableCell()
                    'dtCell.Text = "<li><a href=#UsefulTips>" & strProductDesc & "</a></li>"
                    'dtRow.Cells.Add(dtCell)
                    'dtTalbe3.Rows.Add(dtRow)
                    link = New HyperLink()
                    link.Text = UsefulTips
                    link.Attributes.Add("href", #UsefulTips)      

How to set the Href as variable value.
When I click the 2nd part link it will go to the ID propert link.

Thanks
BRONZE EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
You have lost me now. Can you clarify please using a screenshot if possible.

Author

Commented:
Thanks