KavyaVS
asked on
Pass a variable to hyperlink in code behind using visual basic.net
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").ToStr ing
dtRow = New TableRow()
dtCell = New TableCell()
dtCell.Text = <li><a href="Info.aspx#UsefulTips ">UsefulTi ps</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").ToStr ing
link.Attributes.Add("name" ,UsefulTip s )
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
dttable = GetDataFromDb()
Dim dv As dataview
dv = New DataView(dttable)
Dim UsefulTips As String
UsefulTips = dv(0)("Information").ToStr
dtRow = New TableRow()
dtCell = New TableCell()
dtCell.Text = <li><a href="Info.aspx#UsefulTips
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").ToStr
link.Attributes.Add("name"
link.Attributes.Add("href"
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
When you click the link does it respond in some way or do nothing?
What does the HTML look like?
If the target page is "info.aspx"
And the target on the page is coded as
Then the hyperlink should look like:
If the target page is "info.aspx"
And the target on the page is coded as
<div name="UsefulTips">... something... </div>
Then the hyperlink should look like:
<a href="info.aspx#UsefulTips>link here</a>
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.
Another guess...
You gave "name" to the link and not to the section?
should be
and
should be
You gave "name" to the link and not to the section?
dtCell.Text = <li><a href="Info.aspx#UsefulTips">UsefulTips</a></li>
should be
dtCell.Text = <li><a name="UsefulTips">UsefulTips</a></li>
and
link.Attributes.Add("name",UsefulTips )
link.Attributes.Add("href", "javascript:void(0)")
should be
link.Attributes.Add("href", "#UsefulTips")
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.
ASKER
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").ToStr ing
dtRow = New TableRow()
dtCell = New TableCell()
dtCell.Text = <li><a href="Info.aspx">UsefulTip s</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").ToStr ing
dtRow = New TableRow()
dtCell = New TableCell()
dtCell.Text = <li><a href="Info.aspx#UsefulTips ">UsefulTi ps</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").ToStr ing
link.Attributes.Add("name" ,UsefulTip s )
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.
1. dttable = GetDataFromDb()
Dim dv As dataview
dv = New DataView(dttable)
Dim UsefulTips As String
UsefulTips = dv(0)("Information").ToStr
dtRow = New TableRow()
dtCell = New TableCell()
dtCell.Text = <li><a href="Info.aspx">UsefulTip
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").ToStr
dtRow = New TableRow()
dtCell = New TableCell()
dtCell.Text = <li><a href="Info.aspx#UsefulTips
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").ToStr
link.Attributes.Add("name"
link.Attributes.Add("href"
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.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
1st part worked.
How to add variable to name tag
dtRow = New TableRow()
dtCell = New TableCell()
link = New HyperLink()
UsefulTips = dv(0)("Information").ToStr ing
link.Attributes.Add("name" ,UsefulTip s )
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" ,UsefulTip s)
Here UsefulTips is a string variable.I want to assign the value stored in UsefulTips variable to name
How to assign it.
Thanks
How to add variable to name tag
dtRow = New TableRow()
dtCell = New TableCell()
link = New HyperLink()
UsefulTips = dv(0)("Information").ToStr
link.Attributes.Add("name"
link.Attributes.Add("href"
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"
Here UsefulTips is a string variable.I want to assign the value stored in UsefulTips variable to name
How to assign it.
Thanks
Did you try changing
link.Attributes.Add("name" ,UsefulTip s )
to
link.Nname = UsefulTips
link.Attributes.Add("name"
to
link.Nname = UsefulTips
ASKER
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
Nname is not a member of System.Web.UI.WebControls.
Please let me know how to set the name with Variable value.
Thanks
Sorry it was supposed to be
link.Name = UsefulTips
link.Name = UsefulTips
ASKER
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").ToStr ing
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
'Name' is not a member of 'System.Web.UI.WebControls
This link is destination anchor for another link.
2.I want to set another link as source anchor
UsefulTips = dv(0)("Information").ToStr
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"
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
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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").ToStr ing
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
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").ToStr
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"
How to set the Href as variable value.
When I click the 2nd part link it will go to the ID propert link.
Thanks
You have lost me now. Can you clarify please using a screenshot if possible.
ASKER
Thanks