Solved

basic ASP.NET question:  changing a label in a function

Posted on 2014-01-08
2
302 Views
Last Modified: 2014-01-09
I am trying to change the text of label 1.  Here is my ASPX code:
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <%Test()%>
    </div>
    </form>
</body>

Open in new window


And here is my VB:
Protected Function Test() As Boolean
        Label1.Text = "hello world"
   response.write("hello")
        Return Nothing
    End Function

Open in new window


But the label does not change even though when I load the page, function "test" is executed (the response.write works)

I do not understand why the label doesn't change when function "test" is executed?  Can someone help?  How do I change the label within function "test"
0
Comment
Question by:arthurh88
2 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39767359
Your label doesn't change its text because it's loaded and rendered to the browser when you execute the Test function.

Try this instead:

<div>
    <%Test()%>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>

Open in new window

Now, the label's text is changed.

Hope that helps.
0
 
LVL 5

Expert Comment

by:Ronak Patel
ID: 39767444
Hi arthurh88,

You can also call Test() method on Page_Load event as shown in below code.

<body>
    <form id="form1" runat="server">
    <div>
		<asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>

Open in new window


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

	Test()

End Sub

Protected Function Test() As Boolean

	Label1.Text = "hello world"
	Response.Write("hello")
	Return Nothing

End Function

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question