PDA

View Full Version : AjaxEvents From CodeBehind



OneWingedAngel
Oct 10, 2008, 10:41 AM
Hi Everybody,
I'm having some troubles building my application interface completely from codebehind.
First, I post the incriminated code:




(some code here...)
....
public Coolite.Ext.Web.Button GetLoginButton()
{
/*--------------------------------------
* Button Login Creation
* -------------------------------------*/
if (this.btnLogin == null)
{
Coolite.Ext.Web.Button ButtonLogin = new Coolite.Ext.Web.Button();
ButtonLogin.ID = "ButtonLogin";
ButtonLogin.Text = "Login";
ButtonLogin.Icon = Icon.Accept;
ButtonLogin.AjaxEvents.Click.Event += new ComponentAjaxEvent.AjaxEventHandler (ButtonLogin_Click);


ButtonLogin.AjaxEvents.Click.EventMask.ShowMask = true;
ButtonLogin.AjaxEvents.Click.EventMask.Msg = "Wait...";
ButtonLogin.AjaxEvents.Click.EventMask.MinDelay = 1000;
this.btnLogin = ButtonLogin ;
}
return(this.btnLogin);
}

protected void ButtonLogin_Click(object sender, AjaxEventArgs e)
{
// Do Something....
}


protected void Page_Load(object sender, EventArgs e)
{
// Add The Login Window that it contains the Login Button
this.formMain.Controls.Add(GetLoginWindow());
}

....
(some code here...)



Here's the problem:
The Login Window display correctly inside the page with the Login button inside it.
Pressing the button the Event Mask display but after 1 second a runtime error appears inside an ext error window saying:


Server Error in '/' Application.
System.Web.HttpException: The control with ID 'ButtonLogin' not found

Does somebody know the possible cause of this error?
Help Please

Thanks....

Andrea Cammarata WebSite (http://www.andreacammarata.com/)

Vladimir
Oct 10, 2008, 10:51 AM
Hi,

Can you post full code of example? It seems ???? the button will not be recreated on the ajax event.

geoffrey.mcgill
Oct 10, 2008, 11:10 AM
Hi Andrea,

If you are dynamically creating controls, please ensure you recreate the control on each request. You GetLoginButton() Method would need to be called on each request. Posting a code sample demonstrating where that method is called should help us diagnose the problem. 

OneWingedAngel
Oct 10, 2008, 11:53 AM
Thank you everibody to reply so fast to my problem.
Thank to you i solve the problem:
Like geoffrey.mcgill says i need to ricreate the entire interface on each request.
In fact my Default.aspx PageLoad was:



using BackEnd;

protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// Create the entire applocation interface
Interface Software = new Interface();
this.formMain.Controls.Add(GetMainInterface());
}
}


so i had to change the code like below to solve the problem.



using BackEnd;

protected void Page_Load(object sender, EventArgs e)
{
// Create the entire application interface
Interface Software = new Interface();
this.formMain.Controls.Add(GetMainInterface());
}


But now i have a question:
If the ButtonLogin_Click is wrapped inside an AjaxEvent, why clicking on it the entire page will be loaded?This is not Ajax...

NOTE: The function GetMainInterface() is located inside a Interface.cs (namespace BackEnd) in my App_Code directory.

Thank you all for support,

Andrea Cammarata WebSite (http://www.andreacammarata.com/)

geoffrey.mcgill
Oct 10, 2008, 11:57 AM
But now i have a question:
If the ButtonLogin_Click is wrapped inside an AjaxEvent, why clicking on it the entire page will be loaded?This is not Ajax...

I am not sure what you mean? Can you explain further?

OneWingedAngel
Oct 10, 2008, 12:09 PM
Sure,
if you take a look at the code below



ButtonLogin.AjaxEvents.Click.Event += new ComponentAjaxEvent.AjaxEventHandler(ButtonLogin_Cl ick);

protected void ButtonLogin_Click(object sender, AjaxEventArgs e)
{
// Do Something....
}


The ButtonLogin Click Event is defined like an ajaxevent, but if i click on it, at the debug time, the page will reload completely and after that Page_Load complete, the function ButtonLogin_Click() fires.
If is an ajax event, why all the page need to be rewritted?

Thanks...

geoffrey.mcgill
Oct 10, 2008, 12:50 PM
I copy/paste your code into a new .aspx Page and the AjaxEvent fires perfectly.

Can you paste a simplified .aspx code sample demonstrating how to reproduce this issue? Unless you have set the .AutoPostBack property on the button to "true" it's impossible for the <ext:Button> to trigger a postback.