Solved

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

Posted on 2014-01-08
2
296 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now