newbie problem :(

nswan

Member
Joined
Mar 31, 2003
Messages
8
Location
UK
hi,

how do i use reference a variable in my .aspx html code that i have declared and set in the aspx.vb code?

Many thanks
nick
 
In the CodeBehind:
Code:
Public Class MyWebForm
    Inherits System.Web.UI.Page
    Public a As String

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        a = "test"
    End Sub
End Class

In the asp.net page:
Code:
<body>
  <% Response.Write(a) %>
</body>
 
<asp:hyperlink id="Hyperlink1" runat="server" NavigateUrl="http://www.computerhelp.forum"><% Response.Write(temp) %></asp:hyperlink>

i tried the above but it comes out with not text
 
ah...

i tried it as normal text and it works ok.

so how do i get it to be the text of my hyperlink?

Many thanks
Nick
 
Did you declare and set a public variable in your CodeBehind?
 
yeah.

if i put the response.write(temp) just in normal html then its okay but if i put it in with the url link thing it doesnt work.

is this not a very good way of doing it? What i was gonna do was read some values from a database in the code behind and fill a dataset. Then in the html code loop through the dataset, filling url objects with urls and descriptions.
I know u can use a datagrid but i was trying to stay away from that.
 
Response.Write() should be working, but if for some reason its not you could alternatively try;
<%=temp%>

**EDIT**
Okay I know why.. you have to bind the variable. To do that use;
<%# temp %>

And then in your page load you need to make sure the control gets bound by doing this;
yourHyperLinkID.DataBind()

or if you want to make sure all controls on your page are bound (if you have several that bind data), instead of binding one at a time, you can just do this;
Me.DataBind()

Heres a sample I wrote up in C# real quick (can easily be converted to VB)

C#:
<Script runat="Server" Language="C#">
string temp = "stuff";
void Page_Load() {
   link1.DataBind();
}
</Script>

<html>
<body>

<asp:HyperLink id="link1" NavigateUrl="http://www.somepage.com" 
runat="server"><%# temp %></asp:HyperLink>

</html>
</body>
 
Last edited by a moderator:
Back
Top