I have a page, Default.aspx, and a UserControl, HelloControl.ascx. In the page, I dynamically instantiate the control as follows:
protected void Page_Load(object sender, EventArgs e)
{
HelloControl c = Page.LoadControl(typeof (HelloControl), null) as HelloControl;
c.Greet();
}
This works fine, and the user control writes "Hello from a control" to the response. I have no #Register directive in Default.aspx, but when I try a similar dynamic control creation on a client's machine, I get an error that the "Type or Namespace does not exist".
I have even gotten feedback, on forums, from MS, that I need the #Register directive, but I obviously don't. Can anyone help me out with info on how and when the user control is compiled if no #Register directive references it?
EDIT: I have tried a different direction of investigation, losing my initial call to LoadControl, and I still can't reproduce the problem. The following code also works fine on my dev machine, without any #Register directive.
protected void Page_Load(object sender, EventArgs e)
{
HelloControl c = new HelloControl();
Response.Write(c.Greet());
}
use full path to control :
this.Controls.Add(Page.LoadControl("~/CustomControls/HelloControl.ascx"));
Related
I built a website and I want to redirect a site built in asp.net
( similar example www.mysite.net/Front/ContactUs.aspx?Page=ContactUs&mn=ContactUs) to my new site.
the problem is that I have not found where I can make changes in aspx code, I looked into the source code but in vain.
The project contains a lot of files of code, I do not really know what part of the code I have to show you.
Is ASP's projects have a syntax to define the site URLs?
Please I need help, how to make this redirection , and where I can make changes.
Thanks.
Whether you want to redirect all requests or just select individual files, partially or fully, they all normally have a function called Page_Load.
For every file you want to be redirected, you can use this code piece.
private void Page_Load(object sender, EventArgs e)
{
// Check whether the browser remains
// connected to the server.
if (Response.IsClientConnected)
{
// Redirect
Response.Redirect("http://new.website.com/", false);
}
else
{
// Browser is not connected, stop all response processing
Response.End();
}
}
If you want the full site to be redirected, you can use the this function, in global.asax file
<%# Application Language="C#" %>
<script runat="server">
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Response.Redirect("http://new.website.com/", false);
Response.End();
}
</script>
I have two .aspx pages. In the first page I have a button, on its click event the user is redirected to a second page. In the page_load event of the second page, I wrote code to download a file.
It works. But I need to download this file when the second page is completely loaded in the browser (meaning, I'm able to see all the content of the second page).
Here is my code:
Page-1
protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("Page-2.aspx");
}
page-2
protected void Page_Load(object sender, EventArgs e)
{
// code to download file
}
The LoadComplete event of page occurs after all postback data and view-state data is loaded into the page and after the OnLoad method has been called for all controls on the page.
Example usage (in your C# code)
protected void Page_Load(object sender, EventArgs e)
{
Page.LoadComplete +=new EventHandler(Page_LoadComplete);
}
void Page_LoadComplete(object sender, EventArgs e)
{
// call your download function
}
Alternately you can use following jQuery function
$(document).ready(function()
{
//page is fully loaded and ready, do stuff here
}
it will be called only when page is loaded fully. Including all js, images and other resources.
Two ways to achieve this:
ASP.NET way -- write the file download code on "Unload" page life cycle. Unload is fired after the page has fully been rendered into the browser. Page_Load fires when the page has just started loading.
jQuery way -- inside $document.ready(){} write a call to asp.net method to download the file. $document.ready() gets executed after the your document has loaded or the document is ready. make sure u write the jquery method below the page.
I've written a custom widget to allow a user to select a location from a database of locations. This is my first ASP.net custom control. It seemed like everything was working fine, but now there's a problem.
My control implements the RaisePostBackEvent function as follows:
public void RaisePostBackEvent(string eventArgument)
{
SelectedLocationId = eventArgument.Split('|')[0];
SelectedLocationDescription = eventArgument.Split('|')[1];
}
I wrote a test page and included the following in my ASP code:
<%= locationSelector.SelectedLocationId %>
That worked fine.
However, in my web application, the following code does not work:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
Response.Write(locationSelector.SelectedLocationId);
// SelectedLocationId is null here!!!
}
When I run this code in the debugger, I see that my Page Load event fires before the Post Back event! Therefore, the data is not yet read from the postback. I know that using the MS provided text field control, the text is available at Page Load, so I think I must be doing something wrong.
How can I read the location that the user selected when the Page Load event fires? To clarify, I'm refering to the Page Load of a web application page.
You're setting SelectedLocationId on a postback event and at the same time you are trying to retrieve its value on the first load. SelectedLocationId will be null all right.
Try:
protected void Page_Load(object sender, EventArgs e)
{
if (locationSelector != null)
Response.Write(locationSelector.SelectedLocationId);
}
I'm using a Sharepoint WebPart to load a UserControl which has a button that does some processing on PostBack. I got a problem: when I click the button for the first time, the data loaded on ! IsPosback gets lost, but this does not occur when I click the button again. I think my problem is explained here: Sharepoint Lifecycle, but I haven't been able to find a workaround.
Any help would be really appreciated.
Additional Info:
I'm using EnsureChildControls on the WebPart's OnLoad event, and loading the UserControl on CreateChildControls.
I was able to fix this by programatically specifying an ID to the User Control.
E.g.:
protected void Page_Load(object sender, EventArgs e)
{
this.ID = "MyUserControlID";
}
More info here: http://bytes.com/topic/asp-net/answers/314816-dynamically-loaded-control-event-only-reached-2nd-postback
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (ViewState["MyStuff"] == null)
LoadMyStuffAndSaveToViewState();
else
DoSomethingWith(ViewState["MyStuff"]);
}
Normally I create web application projects and use code-behind, but I have a requirement to create an small throwaway demo app using code-inline.
I added a global.asax file to the app, but for some odd reason, Visual Studio 2008 SP1 won't let me edit any of the code between the script tags i.e. add code to the event handlers such as Application_Start, Session_Start. VS does however let me edit outside the script tags.
This is just a simple file based web app using the built in web server.
Any ideas what's going on?
This is the code-inline global.asax VS creates:
<%# Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when
// the sessionstate mode
// is set to InProc in the Web.config file.
// If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
Ok, so here is the answer:
You can see part of the explanation here and here.
Basically the global.asax file doesn't actually get compiled, so VS2008 put in a fix to prevent you from modifying it, since your modifications will have no effect.
I don't think VS allows inline script in Global.asax. You need to put the code in Global.asax.cs instead.
The only time I ever saw this kind of of thing is when I am in debug mode, and I didn't realize it. Seems pretty basic, but have you checked that?
tried right-clicking -> view code?