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.

No comments:

Post a Comment