Link to home
Start Free TrialLog in
Avatar of taduh
taduhFlag for United States of America

asked on

Aspx page controls are erroring out when referenced in the codebehind?

Hi Experts,

I have controls defined in my aspx page which I reference in my code-behind, but I'm getting errors everywhere they're referenced in my code-behind. The errors for some of the controls indicate that the controls are not declared.

I have 2 aspx pages in this project and 2 codebehind files. I don't know whether that has anything to do with this. I'm  getting these kinds of errors in both codebehind files.

I also have some controls from my first aspx page whose values I pass to the codebehind for my 2nd aspx page by making them public properties in the codebehind for my first aspx page. When I reference them in the codebehind for my 2nd aspx page, by using PreviousPage.controlname, they get an error saying the control is not a member of System.Web.UI.Page.

When I run the project in test mode, everything runs fine.

Thanks,

taduh
Avatar of bprojoe
bprojoe

that is most likely because the controls are not being referenced in the .aspx.designer.cs.  This sometimes happens if there is an error on the page.  one way to get around it is to manually add the controls to the .aspx.designer.cs.
Avatar of taduh

ASKER

burakiewicz,

What is the .aspx.designer.cs?

taduh
also, why not use session to pass values?
Avatar of taduh

ASKER

burakiewicz,

This is only my 2nd web page and I've had no formal training - just kind of hacking my way through this (yep, one of those types).

If its a better way, can you easily explain how to use session to transfer the values or refer me to an article that can?
that is the designer file where all of the controls from the aspx are defined so you can use them in the codebehind.
thats cool gotta start some way.  How are you redirecting, after a button click or a link?
Avatar of taduh

ASKER

I'm redirecting after a button click using a postback URL to get to my second page.

Is the aspx.designer.cs different from the aspx page?
ill try to find a tutorial but this is a quick overview
To set something in session
Session("FirstName") = txtFirstName.Text

to read the value from a different page or the same page
txtFirstName.Text=Session("FirstName")
yes. the aspx page contains all of the HTML for the page. like
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
yours would be .aspx.designer.vb for vb
the .aspx.designer.vb would contain the actual reference to the server control
'''<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
Avatar of taduh

ASKER

Let me give that a shot on one of my controls. I'll let you know what happens.
Avatar of taduh

ASKER

Where do you put the Session statement ? Would it  go in your page_load event handler?
for the setting of it you would put it in the button click event.  Then on the page you are redirected to load it when you need it.  Page load or another button click
Avatar of taduh

ASKER

Anything else I need to do to use Session? It does not seem to be passing the values from my first page too my second page.

Here is my code from the first page:

Protected Sub btnGetData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetData.Click

        Session("DDLBranchItem") = DropDownBranch.SelectedItem.ToString
        Session("DDLBranchValue") = DropDownBranch.SelectedValue

End Sub

Here is my code from the second page:

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


        If Not IsPostBack Then
            txtboxBranch.Text = Session("DDLBranchItem")
            txtboxBranch_ID.Text = Session("DDLBranchValue")
        End If

End Sub



Avatar of taduh

ASKER

burakiewicz,

Can we continue this exchange tomorrow morning? I've got to wrap it up for the day?

Thanks,

taduh
Avatar of taduh

ASKER

burakiewicz,

As I mentioned yesterday, my data doe snot seem to be getting passed through session.Did you see anything lacking in my code?

Thanks,

taduh
it looks like your session has all of the right stuff.
try this just to see what happens. set the values and then use the session value to set the selected value.  This is just a check to see that the values are actually being set
Protected Sub btnGetData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetData.Click
 
        Session("DDLBranchItem") = DropDownBranch.SelectedItem.ToString
        Session("DDLBranchValue") = DropDownBranch.SelectedValue
 
DropDownBranch.SelectedValue=Session("DDLBranchValue")
 
End Sub

Open in new window

Avatar of taduh

ASKER

burakiewicz,

I know whats happening. I set some breakpoints. The session variables are not even being assigned in the btnGetData_Click subroutine, because that subroutine is not getting a chance to run. The PostBackURl associated with the button in Properties causes it to go to my other aspx pagewithout even running the btnGetData_Click routine. If I remove the PostBackURL from the Properties, is there a way to assign it manually and have it postback in the btnGetData_Click subroutine after I have assigned the session variables?

taduh
instead of using the postbackurl use this in the codebehind
Response.Redirect("webpageurl")
or
Server.Transfer("webpageurl")
or if you want it to post back to the same page
add this to the button click

        Page_Load(Nothing, Nothing)
Avatar of taduh

ASKER

That worked!

Now, would you have any idea why controls defined in my Aspx page are receiving errors in the error list which indicate that they have not been declared when they are referenced in the codebehind for that page?

 

that is most likely because the controls are not being referenced in the .aspx.designer.vb.  This sometimes happens if there is an error on the page.  one way to get around it is to manually add the controls to the .aspx.designer.vb.
Avatar of taduh

ASKER

Okay, i'm still a little foggy on this whole concept of the aspx.designer.vb. I have an aspx page  - default.aspx and I have its codebehind, default.aspx.vb. Are you saying there should be a third page, default.aspx.designer.vb.  If so,how do I create it and what should go on it? What is its purpose?
ASKER CERTIFIED SOLUTION
Avatar of burakiewicz
burakiewicz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of taduh

ASKER

When I have it fully expanded, it shows only the .aspx.vb beneath the .aspx. When I click on show all files, it closes everything up again. When I open them back up by clicking on the plus sign. I still just have the .aspx.vb and the .aspx files - no .aspx.designer.vb file.
hmm, not sure how to get it to show up in the solution explorer, mine just does
Avatar of taduh

ASKER

I found this through Googling. If the .aspx.designer.vb file does not show up in the Solution Explorer, right-click on on the .aspx file and click on Convert to Web Application. I did this and the .aspx.designer.vb file was generated.

So, now I'm down to 1 error and it has to do with an entry in the .aspx.designer.vb page. I have a simple label called lblHeader defined in my .aspx page. The error message indicates "lblheader is already declared as 'Protected WIthEvents lblheder As System.Web.UI.WebControls.Label' in this class.

Any ideas what I can do to get rid of this error?
go into the designer file and see if it has 2 of these in there if it does remove 1
'Protected WIthEvents lblheder As System.Web.UI.WebControls.Label'
Avatar of taduh

ASKER

Nope - Only one.
Avatar of taduh

ASKER

burakiewicz,

I'm awarding the points to you because you have resolved my initial problem. I will open up additional posts for the problems I am now experiencing. Thanks for your help! - taduh