Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Wednesday, March 21, 2012

Urgent: Serverside object is null, while calling from an Ajax script

Hi there,

I try to call a method from the an ajaxscript

Sys.Net.WebServiceProxy.invoke(this._servicePath,
this._serviceMethod,
false,
{"MethodInformation":this._MethodInformationValue},
Function.createDelegate(this,this._onMethodComplete),
Function.createDelegate(this,this._onMethodError),
this._currentCallID);
 
you cannot see it, buit these method calls a method on the serverside which is placed in
the Default.aspx.cs, I have also an table object there...
 
 
protected void Page_Load(object sender, EventArgs e)
{
CustomTableControll table =new TabelCustomControll();
...
}
 
know when I try to access the table, I get an NullPointerException...
Where is my faillure there?
Please give me some Advices... its very urgent...
 
thanks in advance,
 
Omid 

Hi Omid,

How is the method defined in the default.aspx.cs?

Are you sure that the request made by the javascript experienced a full life cycle of a normal page? That's to say, is the page_load method fired?


Hi Raymond,

first of all, thank you for your response...

that's the way I defined the method:

 [System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string executeCommand(Object[] MethodInformation)
{
Object[] obj =new Object[MethodInformation.Length - 1];
obj[0] = MethodInformation[0];
string bla = reg.execute(MethodInformation[1]as string , obj)as string;
return bla;
}

and reg.execute looks like these:

public Object execute(string methodName, Object[] args)
{
MethodCallInfo mci =null;
Object ret =null;
try {foreach (DictionaryEntry dein _hash)
{
if ((de.Keyas string).Equals(methodName))
{
mci = de.Valueas MethodCallInfo;
break;
}
}

if (mci ==null)
throw new Exception("method not Found");

Object obj = Activator.CreateInstance(mci.ClassType);
MethodInfo dynMethod = mci.ClassType.GetMethod(mci.MethodName);
if (dynMethod !=null && checkMethodInfo(dynMethod))
{
if (args !=null)
ret = dynMethod.Invoke(obj, args);
else ret = dynMethod.Invoke(obj, mci.MethodArgs); } }catch (ArgumentNullException ae)
{
Console.Error.WriteLine("Please check the MethodName.\n" + ae.StackTrace);
return null;
}
catch (TargetParameterCountException tpce)
{
Console.Error.WriteLine("Number of Parameters passed to MethodInfo.Invoke does not match the number of " +"parameters required by the method declaration. \n" + tpce.StackTrace);
return null;
}
catch (Exception e)
{
Console.Error.WriteLine("ERROR: " + e.Message +" - " + e.StackTrace);
return null;
}

return ret;
}

so as you can see i try to execute the method placed in the Derfault.aspx.cs dynamically.

the method I try to exexute is these:

public string sayHello(string ID)
{

Control c = TimeLineGrid1.FindControl(ID);// HERE I GET NULLPOINTER EXCEPTIONreturn"Hello" + ID;
}

how can I make sure to stay in a page life-cycle?

regards Omid


Omid,

Execuse me, I don't quite understand the logic in your code.

You showed 3 method here, but I don't know what's the relation between them. Can you elaborate it?


Hi Raymond,

I know the hole thing is a little bit confusing... so I try to explain it as we would debug the code.

Lets say we have a Control which produces a table

<cc3:TimeLineGrid ID="TimeLineGrid1" runat="server" OnMouseOverColumn="document.current_id = this.id;" EndTimeperiod="17:00" FixedTimeperiod=true StartTimeperiod="08:00" TimePeriod="60" EndDateTimescale="2007-06-05" FixedTimescale=true StartDateTimescale="2007-06-04"
Header="Bla Test" ShowHeader="True" OnMouseOverRow="document.current_id = this.id;" AutoPostBack="True" />
 
and the generated Row from the table looks like these:
 
<tr id="TimeLineGrid1_1" class="DEFAULT">
<td id="TimeLineGrid1_1_0" class="CARER">Omid</td>
<td id="TimeLineGrid1_1_1" colspan="10">
<div id="div_working_1_1_1" class="working" style=" width:618px; "
methodName="sayHello" onmouseover="document.current_id = this.id;" >
9:0
</div>
</td>
<td id="TimeLineGrid1_1_2" colspan="10">
<div id="div_notworking_1_2_2"
class="notworking"
style=" width:96px;float:left; "
onmouseover="document.current_id = this.id;" >
</div>
<div id="div_working_1_2_3"
class="working"
style=" width:520px;float:left; "
onmouseover="document.current_id = this.id;" >
9:0
</div>
</td>
</tr>
ok and than we have an AjaxExtender for the Control:
 
<cc2:AjaxControl4WebGridExtender ID="AjaxControl4WebGridExtender1" runat="server" TargetControlID="TimeLineGrid1"ServiceMethod="executeCommand">
</cc2:AjaxControl4WebGridExtender>
  and know the extender calls on a click event the following code:
 
populate : function(_MethodValue) {
if (this._servicePath &&this._serviceMethod) {
var m = document.getElementById(_MethodValue).attributes["methodName"].nodeValue;
alert(m);
this._MethodInformationValue =new Array(_MethodValue, m);this._CallerIDValue = _MethodValue;
Sys.Net.WebServiceProxy.invoke(this._servicePath,
this._serviceMethod,
false,
{"MethodInformation":this._MethodInformationValue},
Function.createDelegate(this,this._onMethodComplete),
Function.createDelegate(this,this._onMethodError),
this._currentCallID);
}
},
  so what I am doing upto yet is just calling a JS-method (populate) on a clickEvent,
these method reads the value ob metodName and some other information, put them together and sends them to
the server, that's all...
 
Know we are on the serverside:
 
 here the method inthis._serviceMethod is called, in our case: executeCommand, which is defined as follows:
 
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string executeCommand(Object[] MethodInformation)
{
Object[] obj =new Object[MethodInformation.Length - 1];
obj[0] = MethodInformation[0];
string bla = reg.execute(MethodInformation[1]as string , obj)as string;
return bla;
}

what the method is doing, is just calling a method dynamically and returns its return value...

in our case the executeCommand is calling the sayHello method as defined in the generated code (methodName="sayHello").

and here sayHello trys to access the control object and gets a nullpointerException...

I hope these will help both of us a littlebit out ;) ...

regards Omid


It's quite clear now.

Please note that executeCommand is a static method. And the control it tries to visit isn't instantiated, thus an exception is thrown.


sorry for my asininity,

but it's not so clear for me, what you want me to sayCrying

could you please give me a code example / or some more words ...

what I don't understand is why the table is created, but by comming back to the serverside it

doesn't exists anymore...

when I remove the static tag I get an:Web Service call failed: 500

so should I create every time I get an server side call a new Object of the Control?

regards Omid


An easier explanation is that a regular request for the page has a different life cycle to the request made by the javascript.

The control isn't created in the later situation, so can't be used.

May be you can tell me more about your requirements, then we can try to find a solution.


Hi again,

what I aim to do is, that each cell in the table, must have the possibility to execute

a different method on a specific Event (e.g. onClick) on the serverside... I think thats the

hole story Wink. That's why I wrote an Register, where the developer cann register some methods and in

the generated tag there is the method that should be executed (methodName)...

hopefully you have the ultimate solution, I'd be gladEmbarrassed

thanks for helping me so much,

Omid


And is this method able to control the layout/content of a control on the page?


My idea is that this method should be independent of the controls on the page. Any control it tries to access should be instantiate in the method itself.


Hi Raymond,

the problem is when I instantiate the control each time, then I don't / can't recreate the actual state of the control, since many things

are created by the developer from "the outside".

What do you think about a partial (asynchronous)PostBack? By the way, you know any rsources, how it works and how to implement it...

regards Omid


Omid,

If you want to make your extender user get rid of caring about the state of the control totally, you need to add a hiddenfield on the page. Save the control's state in it. And send its value to the server everytime.

Regarding your second question, I'm not quite sure what is it about. It's a little bit too common to answer. Can you be more specific?


Hi Raymond,

I decided to do it as you said, to save the state of the control in a hiddenfield:

HiddenField _hiddentbl =new HiddenField();
_hiddentbl.ID ="htbl";
_hiddentbl.Value =this._tbl;
Controls.Add(_hiddentbl);

but I get the following Errror:

Error 2 Cannot implicitly convert type 'System.Web.UI.WebControls.Table' to 'string' C:\Dokumente und Einstellungen\Administrator\Eigene Dateien\Visual Studio 2005\Projects\AJAXEnabledWebControl\TimeLineGrid\TimeLineGrid.cs 231 32 TimeLineGrid

is there a way to save the object in such a hiddenfield? do I have to override the toString() method and create a new Object from the String?

thanks,

Omid


Omid, the code you posted won't generate this error, please post the code the code in TimeLineGrid.cs. It's where the error occurred.

Use a menu control in updatepanel

that occurs a javascript error when click another tab: "0.cells" is null or not an object.

who can help me? thanks.

The menu control is not compatible with partial-page updates and can't be used inside a UpdatePanel.


what can i do if i want to implement this function?thx.
Could you run this in JS debugger and post a snippet of affected JS.. I vaguely remember dealing with this issue and it might allow us to see what is exactly happening

did quick google

and found e.g. this

http://forums.asp.net/thread/1485169.aspx


thanks for your information, i read a blog and using this method it works well.

http://mcosier.blogspot.com/2006/12/0cells-is-null-or-not-object-aspnet-20.html

but i have a problem that:

it seems that using AsyncPostBackTrigger can't change the StaticSelectedStyle after i clicked another menuitem?

thanks.


I've been using this piece of javascript to go around the menu problem:

var oldMenu_HideItems = Menu_HideItems;if(oldMenu_HideItems){Menu_HideItems = function(items){if (!items || ((typeof(items.tagName) =="undefined") && (items instanceof Event))) { items = __rootMenuItem; }if(items && items.rows && items.rows.length == 0){ items.insertRow(0); }return oldMenu_HideItems(items);} }
I got it from the asp:menu javascript code and I hook it to "patch" the problem.
Adding it to a page where a menu resides makes the javascript error go away. 

This one works great for me.

Thank you very very much.Big Smile

Great


it is working.. Smile Thank you for your great solutions.

thanks t0yBig Smile

i tryed it.and i dident give that error.

thanks again.


Dear sir

can you please tell me where to put this code in what event, name of the function it is very imp.

thank you,

please advise what to do.


This piece of Javascript has saved me hours of googling! Awesome work buddy and thanks heaps!

See for more info , which controls are not compatable with UpdatePanel

http://ajax.asp.net/docs/overview/UpdatePanelOverview.aspx


I cannot figure out how to use this fix. I put this script in my page and I'm still getting the 0.cells is nothing error.

How do I hook this up?

Thanks!


Have you tried the solution thatt0y provided..

It's working..Smile

Use Ajax on the label

Hi all

on the label i am using the code

protected void Page_Load(object sender, EventArgs e)
{
this.lTime.Text = Convert.ToString(DateTime.Now.TimeOfDay);
}

this is showing fised time

but i want to change the time acording to the second how this is possible?

Thanks

Hi there,

Have a look at this article

http://ajax.asp.net/docs/tutorials/IntroToTimerControl.aspx

Hope it helps ;)


You can change the label content on Javascript

1. Get the label client ID (If you're not using master page and the label is not inside an inner control it should be the
same as the ID you gve to the label)

2. Get the label by document.getElementById or $get

3. Change it's textContent property (Mozilla) or innerHTML / innerText (IE) property of the object and than the label content will change


I am getting some problem here!

I read the post 2

and make a user control name header and done all the coding on that page

after I make a master page i first cell i add a script manager then i put update panel there I drag that user control in the update panel ! That i make for the timer so it showing me the following error

Error Rendering control -UpdatePanel1

An Unhandled Exception has occured.
The control with ID 'Timer1' requires s script Manager on this page.The Script Manager must appear before any controls that need it!


Hi There,

Simple Example: NOTE SCRIPT MANAGER HAS TO BE FIRST

ASPX

<asp:ScriptManagerID="ScriptManager1"runat="server"></asp:ScriptManager>

<asp:UpdatePanelID="UpdatePanel3"runat="server">

<ContentTemplate>

<asp:TimerID="Timer1"runat="server"Interval="1000"OnTick="Timer1_Tick">

</asp:Timer>

<asp:LabelID="Label2"runat="server"Text="Label"></asp:Label>

</ContentTemplate>

</asp:UpdatePanel>

ASPX.VB

protectedvoid Timer1_Tick(object sender,EventArgs e)

{

Label2.Text =DateTime.Now.ToString();

}

IF above not working, Could you post your code here so that me or others can guide you through this.


Are you using the AJAX enabled website

I am not using that

is it necessary to use the AJAX enabled website for using the AJAX?


hi

just go through the following link

http://ajax.asp.net/docs/overview/UsingTimerControlTutorial.aspx#CodeExamples


shakti5385:

is it necessary to use the AJAX enabled website for using the AJAX?

It's not necessary, the key difference is in the web.config.

You may modify any existing web site's config file to make it work with AJAX.

Here is the documentation regarding it. http://ajax.asp.net/docs/ConfiguringASPNETAJAX.aspx