Mutsop
asked on
Can't access variable in code behind page
HI,
I have a project I'm working on, and I want to simply change a txtlabel on a page (which will be shown in a contentplaceholder). But for some magical reason, I can't access it.
Why do they make it so difficult?!
Here is the code
default.aspx
Default.aspx.vb
I get 2 errors in the page behind:
1-
2-
When I check other examples, you don't have to declare any labels again in the code behind...What am I doing wrong?
I have a project I'm working on, and I want to simply change a txtlabel on a page (which will be shown in a contentplaceholder). But for some magical reason, I can't access it.
Why do they make it so difficult?!
Here is the code
default.aspx
<%@ Page Language="vb" title="Homepage" MasterPageFile="~/MasterPage.Master" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>
<asp:Content ID="ContentHomePage" ContentPlaceHolderID="ContentPlaceHolder" Runat="Server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> This is the HomePage
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Content>
Default.aspx.vb
Public Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.text = "hi"
End Sub
End Class
I get 2 errors in the page behind:
1-
Error 97 Handles clause requires a WithEvents variable defined in the containing type or one of its base types. C:\***\WebApplication1\WebApplicatio n1\Default .aspx.vb 7 94 C:\...\WebApplication1\
2-
Error 98 Name 'Label1' is not declared. C:\***\WebApplication1\WebApplicatio n1\Default .aspx.vb 8 9 C:\...\WebApplication1\
When I check other examples, you don't have to declare any labels again in the code behind...What am I doing wrong?
ASKER
I tried just now, and just doesnt work for some reason. :(
In visual Studio, click "Design" tab at the bottom, do you see your label and button? If yes, then run it again to see if you still have problem.
ASKER
Well the thing is, I can access the buttons and labels in the same page (default.aspx) but not in the page behind (default.aspx.vb).
Does that help?
Does that help?
Can you check your Default.aspx.designer.vb if the label and button are declared in there? You should have something like this.
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:2.0.50727.3603
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict On
Option Explicit On
Partial Public Class _Default
'''<summary>
'''Label1 control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents Label1 As Global.System.Web.UI.WebControls.Label
'''<summary>
'''Button1 control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents Button1 As Global.System.Web.UI.WebControls.Button
End Class
ASKER
K so apparently it is recognized
but the button still doesn't work for example
The label is still with his default values.
btw how do you auto generate that?
and how come other examples I see don't need the protect withevents declaration?
but the button still doesn't work for example
The label is still with his default values.
btw how do you auto generate that?
and how come other examples I see don't need the protect withevents declaration?
Did you try my suggestion?
My implementation has the WithEvents declared in the aspx.designer.vb file.
The .aspx.vb and .aspx.designer.vb files are considered as one. Both are partial classes. If you look at the class declaration, it has the word "Partial" in it (Public Partial Class _Default)
If you compile this, the resulting compilation will be as one class. If you don't have these declarations, then you will get those errors in your question.
The .aspx.vb and .aspx.designer.vb files are considered as one. Both are partial classes. If you look at the class declaration, it has the word "Partial" in it (Public Partial Class _Default)
If you compile this, the resulting compilation will be as one class. If you don't have these declarations, then you will get those errors in your question.
OK. Maybe you are not seeing your designer files, in your Solution Explorer, click on the Show All Files icon to display all files related to your aspx page.
ASKER
I do see my designs,
I see the button and label.
I can click on my button and change all properties according.
I see the button and label.
I can click on my button and change all properties according.
[English]
Try to find the control by this way
(System.Web.UI.WebControls .Label)thi s.FindCont rol("Label 1").text = "hi";
it's not the "Optimal" way but if u r desperate, use it now and fix later with more time.
[Español]
Trata de buscar al control de esta manera:
(System.Web.UI.WebControls .Label)thi s.FindCont rol("Label 1").text = "hi";
No es la forma "Optima" de hacerlo pero si tienes la urgencia usalo asi ahora y despues lo corriges.
Try to find the control by this way
(System.Web.UI.WebControls
it's not the "Optimal" way but if u r desperate, use it now and fix later with more time.
[Español]
Trata de buscar al control de esta manera:
(System.Web.UI.WebControls
No es la forma "Optima" de hacerlo pero si tienes la urgencia usalo asi ahora y despues lo corriges.
[English]
Sorry about the other comment. this is the right way
System.Web.UI.WebControls. Label lblFound;
lblFound= (System.Web.UI.WebControls .Label)thi s.FindCont rol("Label 1");
lblFound.Text = "Hi".
[Español]
Lo siento por el comentario anterior. Esta es la forma correcta
System.Web.UI.WebControls. Label lblFound;
lblFound= (System.Web.UI.WebControls .Label)thi s.FindCont rol("Label 1");
lblFound.Text = "Hi".
Sorry about the other comment. this is the right way
System.Web.UI.WebControls.
lblFound= (System.Web.UI.WebControls
lblFound.Text = "Hi".
[Español]
Lo siento por el comentario anterior. Esta es la forma correcta
System.Web.UI.WebControls.
lblFound= (System.Web.UI.WebControls
lblFound.Text = "Hi".
Can you do the following?
1. Right-Click on the Project Name and select "Clean"
2. Right-Click on the Project Name again and select "Rebuild"
3. test it out.
If that doesn't work, try this,
1. Create a new "Web Content Form"
2. Add a Label and Button "from the Toolbox" (drag the controls to the toolbox)
3. In Design Mode, double click your button and put label1.Text = "hi" in there.
4. Make this new "Web Content Form" as you Start Page, by right clicking on the aspx and selecting "Set as Start Page"
4. Build or Rebuild
5. Test it out.
1. Right-Click on the Project Name and select "Clean"
2. Right-Click on the Project Name again and select "Rebuild"
3. test it out.
If that doesn't work, try this,
1. Create a new "Web Content Form"
2. Add a Label and Button "from the Toolbox" (drag the controls to the toolbox)
3. In Design Mode, double click your button and put label1.Text = "hi" in there.
4. Make this new "Web Content Form" as you Start Page, by right clicking on the aspx and selecting "Set as Start Page"
4. Build or Rebuild
5. Test it out.
ASKER
@gabosoft:
The script give problems, also I suppose I need to set it in the event handler.
I'm also using vb.net
@Alfred1:
I can't seem to find the clean button.
Also the second part works, but as I said, in the same file (aspx) the code works, but the moment as I use it in the aspx.vb (page behind) it doesnt
The script give problems, also I suppose I need to set it in the event handler.
I'm also using vb.net
@Alfred1:
I can't seem to find the clean button.
Also the second part works, but as I said, in the same file (aspx) the code works, but the moment as I use it in the aspx.vb (page behind) it doesnt
When you click on the Project Name and then right-click, you should see a popup menu and one of the menu options is "Clean".
Regarding the second part, if you look at no. 3, if that works then everything should work. Double clicking on the button control in design mode would create the structure for the default button event in code behind which is button click event. And then you can then type in Label1.Text = "hi" inside of the event in code behind.
Also, check your namespaces for all pages including Master. As I can see in there, it should be WebApplication1. Check your project namespace as well in project properties under the Application tab and check the root namespace listed in there if it is really WebApplication1.
Regarding the second part, if you look at no. 3, if that works then everything should work. Double clicking on the button control in design mode would create the structure for the default button event in code behind which is button click event. And then you can then type in Label1.Text = "hi" inside of the event in code behind.
Also, check your namespaces for all pages including Master. As I can see in there, it should be WebApplication1. Check your project namespace as well in project properties under the Application tab and check the root namespace listed in there if it is really WebApplication1.
ASKER
Well I just created a whole new project and everything worked for some reason :(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well it worked indeed with your comment, but for some reason it didn't the first time...
THanks
THanks
Try doing a Rebuild of your project. It might just need a Rebuild especially if you directly typed-in the code in the source and code behind.