Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

placing a variable in href

Status
Not open for further replies.

wvdba

IS-IT--Management
Jun 3, 2008
465
US
hi.
i have this href within a repeater control:
Code:
<td>
    <a id="Anchor1"  href="Page.aspx?uid=?????????" runat="server">  
           </a>
</td>
<td><%#Container.DataItem("user_id")%></td>
<td><%#Container.DataItem("last_name")%></td>
<td><%#Container.DataItem("first_name")%></td>
<td><%#Container.DataItem("middle_name")%></td>
i'm trying to put Container.DataItem("user_id") where the ??????? is
in classic isp i would use <%response.write user_id%>
but vb.net is different and i can't figure out what code to use.
any suggestions would be appreciated.
 
try
Code:
href='Page.aspx?uid=<%=Container.DataItem("user_id")%>'
since you are treating it as a server control this may also work
Code:
href='Page.aspx?uid=<%#Container.DataItem("user_id")%>'

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
thanks.
none of those worked.
 
care to elaborate?

you could also do this
Code:
<a href='<%=string.Format("Page.aspx?uid={0}", Container.DataItem("user_id"))%>'>click me</a>
note the use of single quotes, not double quotes.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
i need to use vb.net.
it didn't still work.
when you hover over the href. it shows this at the status bar. the user id is 100756
where as it should show the user id not the code.
Home.aspx?uid=<%#Container.DataItem("user_id")%>
when you hover over the href it should show this:
Home.aspx?uid=100756
 
post your code, chances are there is a subtle difference between what i posted and what you wrote.

did you try the 3rd option of string.format?

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
thanks so much.
what i'm trying to find out what is the equivalent of
<%response.write%> in vb.net
if you put anything in the href tag, it's going to treat it as a literal - not variable. the only way it will be treated as a variable, is to write out with response.write.
here's my code:
Code:
<a id="Anchor1"  href="Home.aspx?oid=<%#Container.DataItem('offender_id_display')%>"  runat="server">  
      <%#Container.DataItem("offender_id_display")%>  
    </a>
thanks.
 
this should work
Code:
href='Home.aspx?oid=<%=Container.DataItem("offender_id_display")%>'
you need to use single quotes and = instead of #. That's the way I have do this in the past.

you're other option is to replace the html tag with a webforms control tag.
Code:
<asp:HyperLink ID="mylink" runat="server" Text="Click Me" NavigateUrl='Home.aspx?oid=<%=Container.DataItem("offender_id_display")%>' EnableViewState="false" />

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
the first one gives this error:
Line 186: href='Home.aspx?oid=<%=Container.DataItem("offender_id_display")%>'</a>

Compiler Error Message: BC30469: Reference to a non-shared member requires an object reference.

and the second one.
displays the CODE -
<%=Container.DataItem("offender_id_display")%>
when you hover over the link.
it's supposed to display the actual value of the variable.


 
final attempt :)
Code:
<asp:HyperLink ID="mylink" runat="server" Text="Click Me" NavigateUrl='Home.aspx?oid=<%#Container.DataItem("offender_id_display")%>' EnableViewState="false" />

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
still no joy.
"click me" is showing up in blue - as a link.
when you hover over the link, it displays:
Home.aspx?oid=<%#Container.DataItem("offender_id_display")%>
instead of the actual value of the variable.
i tried response.write with a constant:
response.write("123456") and it worked
but when "123456" is replaced with a variable like a repeater control item
Container.DataItem("offender_id_display")
it doesn't work.
 
ok.
i found the correct syntax and it worked perfectly:
Code:
<a href='<%# "Home.aspx?oid=" + Cstr(Databinder.Eval(Container.DataItem, "offender_id_display"))%>'>
    <%#Container.DataItem("offender_id_display")%>
  </a>
as customary, i put the solution here so that others can see what the solution is.
thanks much.
 
There are a couple of ways of doing this:

1. Use a HTML anchor element i.e. without the runat="server" tag and use the format <%#DataBinder.Eval(Container.DataItem, "MyIDColumn")%> in the href tag

2. Use a Hyperlink control, and use the ItemDataBound event to find the control and set it's value e.g.
Code:
Dim h As HyperLink = e.Item.FindControl("MyHyperlinkControl")
h.NavigateUrl = "~/mypage.aspx?id=" & e.Item.DataItem("MyIDColumn")


Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
thanks everybody. i got the link to work ok and it's linking to home.aspx. basically what i want to do in home.aspx is to write out the passed variable (oid) and do some other processing.
i'm using the following code:
Code:
sub Page_Load
        Dim x As String
        x = Request.Form("oid")
        Response.Write("oid=" & x)

        '.....some other processes
        
End Sub
 
i'm passing it through the URL as you can see from previous posts.
ok. it worked with request.querystring("oid") as in classic asp.
it also works with request("oid")
is there any difference?
which one should i use?
thanks again for all the help
 
thanks mark.
i revised the code and just put Request.QueryString
it works perfectly.
:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top