How To Access Controls in Code Behind - c#

i have a page named Default.aspx and inside it i use an include virtual for the header. Is there a way i can use the controls inside that header in Default.aspx.cs? (Code Behind).It just doesn't recognize the code inside the include file, everytime i try to compile .net throws me a lot of errors related to unrecognized controls..
I know i can put my header inside the default page as usual, but i'd like to keep it in a separate file because it has a lot of content.
Thanks in advance..

Why would you want to do that?
Only reason I can come up with is reusability - and that could be solved by using a masterpage containing the header information or a usercontrol (or even a custom control)
I'd probably go with the masterpage solution - but your description of what and why you want to do as you do is a lite vague...

Related

C# Insert block of code to every aspx page

I'm having a web application project which is running .NET 4.0. I've plenty of .aspx page and now I would like to add in a block of script code to all the .aspx page header, for example Google Analytics.
I know there is a solution to do is add in every single page, but I would like to know is there any other's way to do this instead modify every single .aspx page?
*My header is not runat server
I got an idea to do but not sure it's work or not.
Get the page class in Global.asax
Get the output stream from the page class.
Insert the Google Analytics code in the HTML header.
I couldn't get the Page.Response in the Global.asax as I tried in the Application_PostRequestHandlerExecute & also Application_EndRequest. Does anyone know is this work and how it's work?
Thanks.
Use master pages. This is the ASP.NET way of putting the same content on multiple pages without repeating yourself.
All of our aspx pages code-behind classes inherit from the same base class, which allows us to inject standard client side elements (controls, script, etc) into every page using a single point of control.
Our design was implemented before the advent of master pages, but while it could possibly be converted to a master-page design, we have found this implementation to be extremely flexible and responsive to changing needs.
For example, we have two completely separate application designs (different skin, some different behavior) that is based off of the same code base and page sets. We were able to dynamically swap out banners and other UI and script elements by simple modifications to the base class in order to support this without having to duplicate every page.
Unfortunately, if you want the script to be in the head element, you will need to ensure that they are all marked as runat=server.
Our base class itself inherits from Page, so it can intercept all of the page's events and act on them either instead of or in addition to the inheriting classes (we actually have internal overrideable methods that inheritors should use instead of the page events in order to ensure order of execution).
This is our (VB) code for adding script to the header (in the Page's LoadComplete method):
' sbscript is a stringbuilder that contains all of the javascript we want to place in the header
Me.Page.Header.Controls.Add(New LiteralControl(sbScript.ToString))
If it is not possible to change the heads to runat server, you could look into ClientScriptManager method RegisterClientScriptBlock which places the script at the top of the page.
You can create a basic page with the header with the custom code such as Google analytics and have the other pages inherit from that. It will facilitate two things:
1) In case you ever want to change the custom code you will only have to do it in one place
2) No repetitive code hence more maintainable
I am trying to do the same thing on a legacy app that we're trying to decommission. I need to display a popup on all the old pages to nag users to update their bookmarks to use the new sites, without forcing them to stop using the legacy site (yet). It is not worth the time to convert the site to run on a master page when I can just plop in a popup script, since this whole thing is getting retired soon. The main new site uses a master page, which obviously simplifies things there.
I have this line in a file that has some various constants in it.
Public Shared ReadOnly RetirementNagScript As String = "<Script Language='javascript'> alert('[app name] is being retired and will be shut down [in the near future]. Please update your bookmarks and references to the following URL: [some URL]'); </script>"
Then I am inserting it in Global.asax, in Application_PostAcquireRequestState:
Response.Write(Globals.RetirementNagScript)
Hopefully this is useful to you; I still need to be able to present a clickable URL to the user that way, on each page of the legacy site, and JS alert doesn't do that for me.

The name Repeater1 does not exist in the current content

Im using a masterpage on asp.net c# for the website. (And created an aspx page related to the master page, by hand, not automatically)
I put a repeater control to the page.
Then I couldn't reach to the control by name in codebehind.
The compiling error is : 'The name Repeater1 does not exist in the current content'
Can you please point out my mistake?
Sometimes the designers get out of sync with the aspx pages. When such a thing occurs, the code behind files become unable to access controls that you have added to your aspx pages and you'll get errors like the one you received.
You can get around this error by regenerating the designer file, as you said Project -> Convert to web application is one way to do it.
One last thing, not sure what version of VS you're using, or if this will help, but there was a hotfix released for VS2010 that is supposed to take care of some occurrences of this problem.
http://blogs.msdn.com/b/webdev/archive/2010/03/05/hotfix-for-issue-with-auto-generated-designer-files-not-adding-controls.aspx

How can I dynamically include external files / user control?

I am trying to build a C# .NET website that includes files based on DB input.
In PHP, this is simple, where I could do this very easily like this:
<?php include('inc/'.$filename);?>
Is there something similar to do this in C#? I've been trying to figure this out for a while, and can't find a simple solution. I know I can specify static files like this:
<%# Register Src="~/controls/blah.ascx" TagPrefix="test" TagName="blah" %>
... But I can't change the "blah.ascx" dynamically in the code-behind. This would work just fine if I always knew exactly what needed to be included.
Has anyone been able to accomplish this? Thanks!
You can easily add and read from files on the server. Not sure if you want to include existing files or make them on the fly.
Anyway, here's a link that explains how to read from a file on the server.
http://msdn.microsoft.com/en-us/library/94223t4d.aspx
Here is a link that explains how to load user controls dynamically.
http://msdn.microsoft.com/en-us/library/c0az2h86(v=vs.100).aspx
I finally found a solution that allows me to include any user control without having to register it in the .aspx page.
// Include external files here
// CustomControl variable can equal something like "home.ascx"
UserControl uc = (UserControl)Page.LoadControl("~/pages/" + CustomControl);
// PageCustomControl can be a Panel or PlaceHolder
PageCustomControl.Controls.Add(uc);
Based on code from http://forums.asp.net/t/1186225.aspx?Add+Web+User+Control+from+Code+Behind

No access to cooresponding .cs file in asp.net

The aspx page that I need to add code to has an aspx.cs file which was written by a company that has this aspx.cs file on their system (per contract) and I cannot modify/access.
Example: MyFile.aspx and no access file MyFile.aspx.cs
I need to use a label.text from the page in the control pages and also depending on IF ELSEIF statements it will call which of the different controls that it needs to execute.
What is a good way to do this when one does not have access to the aspx.cs file?
I have spent a couple days trying to find/figure out answers to this problem and keep running into problems.
You do have a strange situation here ;)
Here's one wild idea. You can change the Inherits attribute at the top of the .aspx file to substitue your own class instead.
Of course that means you have to rewrite all the logic behind the page -- or you can try to have your own class inherit the original one, but depending on visibility of original code this may not work.
Another option could be to use a decompiler to re-create all the source code of the web project from the compiled dll. But you may not be legally allowed to do that -- although I would point out that if you don't have the rights to use the code, you probably don't have the rights to use the .aspx either, even if you have access to them.

Creating a custom pop up for Umbraco back office

[Final EDIT] Here is a link to the code I wrote in case it helps anyone.
I think I have a solution. Umbraco uses asp.net files for their popups
which is something I haven't used yet but I think I can get the hang
of it. I don't know how to access the aspx from within my class,
should I make a code behind partial class?
Thanks for any help.
I am developing a multi-lingual site, using Umbraco, where content nodes are automatically copied to each language as they are created. Is there any way to implement a custom popup to confirm that it should be copied to all instead?
This wouldn't actually be on the site, rather in the back office.
Or is it possible to open a browser popup with c# as all I really need is a bool value from a message box?
[EDIT: added possible solution]
I sorted this by adapting Umbraco's own create function. I made a new .aspx file and added the functionality that I needed to the code behind.
I was able to add a context menu item that allowed me to call my new page and from there called a method to duplicate the content.
From the method, I pass the new node and get the parent id. Then I compare all the node names for those that match and use the umbraco document.copy() method to recreate the content under each language at the correct position.
If I can make the code more generic then I will upload it as a package to Umbraco.

Categories