• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

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
0
taduh
Asked:
taduh
  • 15
  • 14
1 Solution
 
bprojoeCommented:
0
 
burakiewiczCommented:
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.
0
 
taduhFinancial Systems AnalystAuthor Commented:
burakiewicz,

What is the .aspx.designer.cs?

taduh
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
burakiewiczCommented:
also, why not use session to pass values?
0
 
taduhFinancial Systems AnalystAuthor Commented:
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?
0
 
burakiewiczCommented:
that is the designer file where all of the controls from the aspx are defined so you can use them in the codebehind.
0
 
burakiewiczCommented:
thats cool gotta start some way.  How are you redirecting, after a button click or a link?
0
 
taduhFinancial Systems AnalystAuthor Commented:
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?
0
 
burakiewiczCommented:
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")
0
 
burakiewiczCommented:
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
0
 
taduhFinancial Systems AnalystAuthor Commented:
Let me give that a shot on one of my controls. I'll let you know what happens.
0
 
taduhFinancial Systems AnalystAuthor Commented:
Where do you put the Session statement ? Would it  go in your page_load event handler?
0
 
burakiewiczCommented:
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
0
 
taduhFinancial Systems AnalystAuthor Commented:
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



0
 
taduhFinancial Systems AnalystAuthor Commented:
burakiewicz,

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

Thanks,

taduh
0
 
taduhFinancial Systems AnalystAuthor Commented:
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
0
 
burakiewiczCommented:
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

0
 
taduhFinancial Systems AnalystAuthor Commented:
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
0
 
burakiewiczCommented:
instead of using the postbackurl use this in the codebehind
Response.Redirect("webpageurl")
or
Server.Transfer("webpageurl")
0
 
burakiewiczCommented:
or if you want it to post back to the same page
add this to the button click

        Page_Load(Nothing, Nothing)
0
 
taduhFinancial Systems AnalystAuthor Commented:
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?

 
0
 
burakiewiczCommented:

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.
0
 
taduhFinancial Systems AnalystAuthor Commented:
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?
0
 
burakiewiczCommented:
ok each aspx page has 3 parts

.aspx contains the html for the page
.aspx.vb contains the codebehind
.aspx.designer.vb contains the references to the server controls on the page.  Any controls that are in here you can reference in the codebehind  You dont need to create this it is automatically created.  If you cant see it when you click the + next to the file.  there is a button for show all files
0
 
taduhFinancial Systems AnalystAuthor Commented:
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.
0
 
burakiewiczCommented:
hmm, not sure how to get it to show up in the solution explorer, mine just does
0
 
taduhFinancial Systems AnalystAuthor Commented:
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?
0
 
burakiewiczCommented:
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'
0
 
taduhFinancial Systems AnalystAuthor Commented:
Nope - Only one.
0
 
taduhFinancial Systems AnalystAuthor Commented:
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
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 15
  • 14
Tackle projects and never again get stuck behind a technical roadblock.
Join Now