Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Monday, March 26, 2012

Updating and ASP Textbox from Server Side Script using AJAX Extensions

I've searched the web extensively and cannot find an answer to a question that I believe is simple.

I have an application running AJAX extensions so that scripts are run at server side. Everything works great. The problem I have is that based on selections in the script I need to base data to ASP.NET VB code behind to intergrate with an existing application.

Quite simply I want to update a hidden textbox then use that data to pass along to another asp page. I know how to pass the data once I get it to and ASP control. Just need to get it to the ASP side of the server.

Thanks in advance for any help.

Brian

I solved my problem.

Make sure there is an Name tag on the textbox that you will be using to pass info to code behind

This will accept a value and display it.

<inputtype="text"id="textbox1" value="JavaScript JumpStart"runat=server>

If you want to use the value in the code behind file this is what you need.

<inputtype="text"id="textbox1"name="textbox1"value="JavaScript JumpStart"runat=server>

Note the addition of the name tag.

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 "Hello World Using Client Script" in UserControl

Hi

I want to create a asp.net user control for dynamic date entering, and I'm trying to use the same approach as in the example:

 <form runat="server">
<div>
Search for
<input id="SearchKey" type="text" />
<input id="SearchButton" type="button"
value="Search"
onclick="DoSearch()" />
</div>
</form>
<hr style="width: 300px" />
<div>
<span id="Results"></span>
</div>
<script type="text/javascript"
function DoSearch()
{
var SrchElem = document.getElementById("SearchKey");
Samples.AspNet.HelloWorldService.HelloWorld(SrchElem.value,
OnRequestComplete);
}

function OnRequestComplete(result)
{
var RsltElem = document.getElementById("Results");
RsltElem.innerHTML = result;
}

</script>

The problem is if I use <input id=SearchKey> -tag for the result, then there will be problems if I use the same control multiple times on one page (the id is not unique, and the OnRequestComplete will update every <input>-tag with id=SearchKey). If I change it to <input id=SearchKey runat=server>, asp will render unique id's for the <input>, but then .getElementById() does not find "object".

I have done a "workaround" where I passes the object's Id to the Webservice, and returns it in an array together with the result, but I don't think this is "a great way". Is there any other solution? A better one?

function DoSearch(CallerId)
{
var SrchElem = document.getElementById(CallerId);
Samples.AspNet.HelloWorldService.HelloWorld(this.id, SrchElem.value,
OnRequestComplete);
}

function OnRequestComplete(result)
{

var objectId = result[0].toString();
var WebServiceResult = result[1].toString();
var RsltElem = document.getElementById(objectId);
RsltElem.innerHTML = result;
}

Regards

Lars K.

This is a common problem... if you use ASP.NET server IDs you need to reference the client ID in client script:

var Ctl = $('<%= SearchKey.ClientID %>');

will do the trick in finding the control reliably.

You also need to use this if use MasterPages or any contained control, so as a general rule this is the best way to reference server based page variables.

+++ Rick --