How can i access asp.net server control in Ajax.AjaxMethod.
My code is below.
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(Default2));
}
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
public void CreateModelPopupForRenameApplication()
{
Timer1.Enabled = true;
}
I am getting error.......on Timer1 (object ref not set)
Ajax methods are not meant to interact with the server controls on your page in this way, because you are not actually running through the page lifecycle like when you normally load the page. That's why it cannot find the Timer1 object.
You should think of these methods as stand-alone functions that calculate a value or do something independently of any page controls.
Related
I'm a beginner in ASP.NET, just a question on page life cycle:
The MSDN doc says: "Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance", which means, I can also put the programming logic here
protected void Page_PreLoad(object sender, EventArgs e)
{
Label1.Text = "Hello World; the time is now " + DateTime.Now.ToString();
}
so why we always do like
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Hello World; the time is now " + DateTime.Now.ToString();
}
?
The Page_Load event handler will properly handle creation of all page controls. Per documentation:
The Page object calls the OnLoad method on the Page object, and then
recursively does the same for each child control until the page and
all controls are loaded. The Load event of individual controls occurs
after the Load event of the page.
Use the OnLoad event method to set properties in controls and to
establish database connections.
...which means, based on your example, the Label1.Text may get reset by that control's OnLoad event (depending on the individual control's implementation; I do not know if this is true for Label control, but it would be in compliance with the documentation if that did happen).
I'm only a beginner so ,my question might sound a bit stupid or basic.
I learn programming in asp.net, therefore I see a lot of functions activated by events. Yet, I didn't find anything in the code nor in the type signature that defines which event activates the function.
So, in functions like public void Page_Load (object sender, EventArgs e), where are the code lines that determine what event will make the function to start? Does it have any relation to the function's name?
Thanks :)
Functions like Page_Load are called by ASP.NET in a particular order. You cannot configure which will fire first. The idea is that you override the ones you need to fire your code in the particular order you need.
Here is the MSDN Page Lifecycle information which talks about which event can be overridden and what order they go in.
In ASP.Net 1.1, we used to have the following system generated code in every code behind files.
public class Default : System.Web.UI.Page
{
// ----- System generated code
protected System.Web.UI.WebControls.TextBox Name;
protected System.Web.UI.WebControls.TextBox Email;
public Default()
{
Page.Init += new System.EventHandler(Page_Init);
}
// ----- System generated code
private void Page_Init(object sender, System.EventArgs e)
{
}
}
It basically registers method to page event. They are nothing but just make the code behind file dirty.
Start from ASP.Net 2, they moved the system generated code to designer file, and code behind file becomes clean and easy read.
public class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
}
-- OR --
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
}
where are the code lines that determine what event will make the
function to start?
ASP.Net uses conversion over configuration approach to register events. It means, you can name a Protected method with following event name, and the page will know how to attach those event. For example, Page_Init, Page_Load and Page_PreRender
In addition, you can override those events explicit if you want.
https://msdn.microsoft.com/en-us/library/ms178472.aspx
May I know if there is any way to invoke a method from the child page(.aspx) after the page load of the user control is finished?
Right now I have an issue of being unable to retrieve the value from the child page because the variables from the user control has not been assigned the value yet.
To put it simply
FROM MY .ASPX FILE
Page_Load(object sender, EventArgs e)
{
x = getValueFromUserControl();
}
FROM MY USER CONTROL
Page_Load(object sender, EventArgs e)
{
int x = getvalueFromDatabase();
}
getValueFromuserControl()
{
return x;
}
Since the ASP.NET Life Cycle goes from the child page(.aspx) page_load -> user control page_load, I am unable to retrieve the value of x.
That said, ideally I would not like to put the function in the child page and call it from the user control as the user control is being used in other pages.
In short, I would like to invoke a method from my .aspx page after the page_load in my user control ends, Thank you!
Get the value at a later page event:
protected override void OnLoadComplete(EventArgs e) {
x = getValueFromUserControl();
}
Unless, of course, there is a specific reason why you must get the value on Page_Load. There are other, probably more appropriate ways to handle this, but without knowing what x is and what you need to do with it, it is hard to give any other advice. For example, maybe the UserControl should fire an event that is handled by the page.
I am loading my with web controls and loading them based on the URL parameters.
I need to load a gridview if the user is in :&cat=8
My web control has a method which I need to call. One of its parameters is a session which is created only when the category is 8.
Technically I need a way of calling methods within my web control from my page. Placing it on page_load results in an error.
Thanks
You need to bind an event-handler on Load event of the Control itself.
private void Page_Load (object sender, System.EventArgs e)
{
// Add the following code:
yourControl.Load += new EventHandler(yourControl_Load);
}
private void yourControl_Load (object sender, System.EventArgs e)
{
//your code here.
}
I'm experimenting with some AJAX now. I have a custom control which appears on my masterpage in which there is an update panel and a timer. The timer fires and the panel updates and everything is dandy. Except that there are some operations that I don't want it to perform on every refresh. It seems like the entire page lifecycle happens with each refresh. There are variables I want to set, and keep their value on the refresh. Is there a way to make it perform ONLY what's in the timer_tick call?
You could take a look at Request["__EVENTTARGET"] in the page load event to see what control caused the postback. If it's the timer control, jump out of the function.
Assuming your timer is called "refreshtimer":
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTTARGET"] == "refreshtimer")
{
return;
}
// etc
Not sure what what an AJAX.Net post back looks like to, But I usually protect my other controls and content by checking for post back;
protected void Page_Load(object sender, EventArgs e)
{
// if its a post back then my controls should already be setup...
if (!Page.IsPostBack)
{
InitControlData();
}
}
and then it should fall thru to your event handling?
protected void timer_tick(object sender, EventArgs e)
{
// Do my Ajaxy work~
}
UpdatePanels always force the entire page to refresh. If you want only a certain portion of the page to be processed (and it's a fixed size) then you could try using an iframe.
Alternatively, if you want to save the variables you can either put them in ViewState or SessionState so that they are persisted between postbacks
This is not possible with UpdatePanels... the entire page will go through the entire lifecycle. As others mentioned, you can limit the processing that happens by using IsPostBack or ScriptManager's IsInAsyncPostBack, but ultimately this is not going to be a great solution for complex pages.
However, you can use Page Methods to execute just one static method in your page, but you'll have to make the Javascript call yourself and update the UI. Here are some examples:
http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx
http://encosia.com/2009/07/21/simplify-calling-asp-net-ajax-services-from-jquery/
http://weblogs.asp.net/craigshoemaker/archive/2008/09/29/using-jquery-to-call-asp-net-ajax-page-methods.aspx