Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Wednesday, March 28, 2012

very very simple question about $get

in javascript how can I transform this :

var texte=document.getElementById("Text1").value;

with a $get ? I've tried var texte=$get("Text1","value"); but it doesn't work ?

I'm on Vista with VS 2008 RTM

Thanks

Try var texte=$get("Text1").value, basically $get() is a shortcut to document.getElementById()

Hope this helps,

Elias.


thanks it works, I was waiting for intellisense, but $get(tytu). there is no intellisense.


Keep in mind that $get is exactly the same as getElementById. They both return a reference to the supplied DOM element. $get is just a shortcut, to save typing and improve readability.

If you have existing code using getElementById, you don't have to convert it to $get unless you want to.

Viewing Source - Javascript error on missing elements

Hello,

I know this is very likely to be my misunderstanding of how ajax works, but any help would be greatly appreciated.

When my page first loads there is a panel that is not visible, inside the UpdatePanel. After a partial postback this panel becomes visible, but when I 'View Source', the outputted code for the panel does not exists, nor the code for its child controls (even though they show on screen). This means when I try to use client-side code for a control in that panel, eg.

var t = window.document.getElementByID(childID);

it cannot be found as the childID doesn't appear in the source.

Any ideas?

Thanks very much.

Simon

This is actually expected... View/Source in the browser only displays the source that was sent down with the original request.

As for your specific issue, make sure you're using the client ID... a common idiom is: $get('<%= MyControl.ClientID %>'). $get() is a shortcut in ASP.NET AJAX for document.getElementByID().

ViewState and JavaScript

Hi, I posted this in another forum, and I am hopping I'll get more response here.

I have two list boxes that I use for dual list box functionality, and there are four buttons that allow movement of the items from one box to another. All this is performed using javascript functions attached to buttons those 4 buttons. There is one more button that is supposed to get values from the right box and print reports. All controls are in update panel. In Pre-Reneder method I am checking to see if there are any items in the right box, and there aren't ever any. Is there something that I need to do so state of the controls would be maintained?

Here is the javascript

function MoveDualList( srcListId, destListId, moveAll )

{

var srcList = document.getElementById(srcListId);

var destList = document.getElementById(destListId);

// Do nothing if nothing is selected

if ( ( srcList.selectedIndex == -1 ) && ( moveAll ==false ) )

{

return;

}

newDestList =new Array( destList.options.length );

var len = 0;

for( len = 0; len < destList.options.length; len++ )

{

if ( destList.options[ len ] !=null )

{

newDestList[ len ] =new Option( destList.options[ len ].text, destList.options[ len ].value, destList.options[ len ].defaultSelected, destList.options[ len ].selected );

}

}

for(var i = 0; i < srcList.options.length; i++ )

{

if ( srcList.options[i] !=null && ( srcList.options[i].selected ==true || moveAll ) )

{

// Statements to perform if option is selected

// Incorporate into new list

newDestList[ len ] =new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected );

len++;

}

}

// Populate the destination with the items from the new array

for (var j = 0; j < newDestList.length; j++ )

{

if ( newDestList[ j ] !=null )

{

destList.options[ j ] = newDestList[ j ];

}

}

// Erase source list selected elements

for(var i = srcList.options.length - 1; i >= 0; i-- )

{

if ( srcList.options[i] !=null && ( srcList.options[i].selected ==true || moveAll ) )

{

// Erase Source

srcList.options[i] =null;

}

}

//sort destinantion list box

SortListBox(destList,compareText)

}// End of moveDualList()

function compareText (opt1, opt2)

{

return opt1.text.toLowerCase() < opt2.text.toLowerCase() ? -1 :

opt1.text.toLowerCase() > opt2.text.toLowerCase() ? 1 : 0;

}

function SortListBox (pListBox, compareFunction)

{

if (!compareFunction)

{

compareFunction = compareText;

}

var options =new Array (pListBox.options.length);for (var i = 0; i < options.length; i++)

{

options[i] =new Option ( pListBox.options[i].text,

pListBox.options[i].value,

pListBox.options[i].defaultSelected,

pListBox.options[i].selected );

}

options.sort(compareFunction);

pListBox.options.length = 0;

for (var i = 0; i < options.length; i++)

{

pListBox.options[i] = options[i];

}

}

Hi,

ListBoxes do not post their values when HTML form is submitted. ListBoxes do only post their selected values. Therefore, any items added to the list box on the client are not available on the server. Your page restores list boxes from the viewstate encoded in the hidden field at the server and it doe not know anything about changes made to your listboxes on the lcient.

As a simple solution, I can suggest you placing a hidden field for your list box on the client and every time you change list box on the client, place a comma separated list of value into that hidden field. Then at the server, you can retrieve coma seperated list of values and populate your list (if you still need to render the same page) or process items.

-yuriy
http://couldbedone.blogspot.com

ViewState and JavaScript

Hi guys,

I have two list boxes that I use for dual list box functionality, and there are four buttons that allow movement of the items from one box to another. All this is performed using javascript functions attached to buttons those 4 buttons. There is one more button that is supposed to get values from the right box and print reports. All controls are in update panel. In Pre-Reneder method I am checking to see if there are any items in the right box, and there aren't ever any. Is there something that I need to do so state of the controls would be maintained?

Are you adding the Right side listbox items in client side by JS/DHTML? If so the viewstate will not sync with the server.


Yes I am using javascript/dhtml. So is there other way to do it on client side that will sync up with server?


Hi

I'm afraid there is no way to do this.Please change it to using AJAX to do that at server side.

Good luck.


Hi,

Please refer to this article[ http://aspalliance.com/1071_Smart_ListControl_in_ASPNET_1x20 ], it explains the reason and gives a solution.

Hope this helps.

Monday, March 26, 2012

Want to call a javascript function from code behind

Hi.

I am trying to call a javascript function on button click, buttton is present in update panel. It is not working.

On button click event handler i m writing the following code

Response.Write("<script>functionname();</script>");

I have added script reference in script manager and given the path.

Response.Write("<script>functionname();</script>") --> this works if button is outside update panel.

Help me...

Why not try placing your javascript function name in the button's OnClientClick parameter? That way it calls it directly without requiring a post back...

OnClientClick = "functionname();"


PhunkNugget:

If you do that, which runs first, the javascript in the client or the

click event code in the code behind? may be related, so it's

important to know..I also need to invoke a modal popup

extender from code in a codebehind, not from a click event..

I'm watching this thread closely.. Geeked

Thanks in advance..


The OnClientClick runs first.


Kanwar,

You can't use Response.Write() inside an update panel. It conflicts with the ajax somehow (very annoying). You can attach the script to your button's onclick event in page load such as the following:

string myString="<script language='Javascript'>\n";

myString+="function fuctionname()\n";

myString+="{\n";

myString+="whatever your function does...\n";

myString+=

"}\n";myString+="</script>\n";

Page.RegisterClientScriptBlock(

"MYFUNCTION", myString);

btnMyButton.Attributes.Add(

"onClick","functionname();");

Then when you click on the MyButton, the javascript function will be called.

Saturday, March 24, 2012

want to pass datatable (.net) to javascript function

hello,

i have a datatable stored at page level (in aspx). i am binding this datatable to gridview object which has a checkbox column for row selection.this table may contain about 500 to 1000 records. when ever i check/uncheck this checkbox, i want to update this bound datatable without posting back to server.is it possible through ajax/javascript ?

pl. provide solution for this asap...

Jayesh Shah

You will have to post back to the server if you want to update the DataTable as that is a server side object. However, you could use a UpdatePanel around the GridView to hide the fact that a full postback is occurring.

watermark extender and javascript?

Hi, i have a textbox on the page. When the user clicks on a username in the list to the right i use this javascript to insert their username into the textbox.

function

setTextBoxText(text)
{
var textbox = window.document.getElementById("<%=touser.ClientID%>");
textbox.value ='';
textbox.value = text;
textbox.focus();
}

This works perfectly.

Then i tried to add a TextboxWatermarkExtender and now it does not work anymore, the initial text in the textbox (set by the watermark extender) gets erased, but the username does not show up, it just turns blank and focus is set on the textbox. Is there anyway to fix that?

Patrick

any idea?

I tested this out yesterday and found out that when you use WaterMarked on your textbox, all of the changes to the textbox through javascript will be written to watermarked's layer.

e.g. you have a textbox with onchange or onfocus command to call a javascript function; and the textbox also has a watermarked "Enter Name Here"

Your JavaScript function will insert the user's name into the textbox.

document.getElementById('" & Me.textbox1.ClientID & "').value = 'John Smith';

Problem: What happen is, "Enter Name Here" will be replaced with "John Smith", and the textbox is still empty.


Question, is this a bug or is it as design?

Wednesday, March 21, 2012

Web Service Access using JavaScript

I've installed the ASP.NET Futures on my dev machine after VS 2008 with .NET 3.5 was installed. When I attempt to access my webservice marked as Namespace.Class.Function, I receive a JavaScript error stating 'TestNamespace' is undefined. Any ideas what could be happening? It seems to occur when I add the converters in my web.config file:

<jsonSerialization maxJsonLength="8388608"> <converters> <add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview"/> <add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview"/> <add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview"/> </converters></jsonSerialization>

Got it. Wrong version of the Microsoft.Web.Preview.dll file in the bin.


Can you please let me know the right version? I can't find a download for the .net 3.5 version of this DLL on ajax.asp.net website, only the July 2007 version.

Thanks

Angie


I've tracked this down. Web.config required the following entry for it to work.

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>

web service call by javascript

In my AJAX enabled website, call web service by javascript.

Web Service:

<WebService(Namespace:="http://tempuri.org/")> _<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _<ScriptService()> _Public Class SimpleServiceInherits System.Web.Services.WebService<WebMethod()> _Public Function Addup(ByRef int1As Integer,ByRef int2As Integer)As Booleanint1 = int1 + int2Return TrueEnd FunctionEnd Class

Javascript:

var num1=1, num2=2;function Button1_onclick() {HangmanService.Addup(num1, num2, OnComplete);}function OnComplete(ret){if (ret == true){alert(num1);// should popup "3"}}

Actually, I got error of "System.InvalidOperationException -- Can't convert object of type "System.Int32" to type "System.Int32&" instead of "3". So do I have to get number from return value?

Hi,

the reference to num1 on server side has nothing to do with the reference to num1 on client side, they are two separate variables. Therefore Addup must return a number.


Try this,
 
<WebService(Namespace:="http://tempuri.org/")> _<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _<ScriptService()> _Public Class SimpleServiceInherits System.Web.Services.WebService<WebMethod()> _Public Function Addup(ByRef int1As Integer,ByRef int2As Integer)As Stringint1 = int1 + int2Return int1.toString()End FunctionEnd Class

Javascript:

var num1=1, num2=2;function Button1_onclick() {HangmanService.Addup(num1, num2, OnComplete);}function OnComplete(result){alert(result); // should popup "3"}
WS

Thank you guys' reply.

The problem is not about return value but javascript even can't call the web method. Maybe in javascript a variable can't be reference parameter.


Ah, gotcha,

It's a little bit odd how you call your webservice function. Should it be ...

SimpleService.Addup(num1, num2, OnComplete); ......???
WS

I believe you should return the number in the return value if it's possible.
AJAX serializes input parameters and return values. I think that using ByRef is not going to work.
If you need to get those two values back (bool and int), you should consider returning a different type.

Hope this helps,

MaĆ­ra


Thanks for your reply. I just wanna know if it works. Anyway, I know it.

Web service call from javascript and SQL Server 2005 Express

Hi,

I'm developing a web application that uses Ajax to call web services using javascript.

The web service must connect to a SQL Server 2005 Database (express edition) and uses a dynamically created connection. The connection string used to create the connection is right because it works for web page data loading and it uses SQL server authentication mode.

In the development environment the application works correctly, but when deployed on a different machine with a different SQL Server installation, the sql connection in the web service fails returning the following error:

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.

I've tried all suggested changes in SQL Server Configuration Manager andSQL Server Surface Area Configuration, enabling both tcp/ip and named pipes for remote connection, but the connection is still not working.

The server has a windows XP professional SP2 operating system and windows firewall is active, but i have enabled http from lan and sql server access.

Can you suggest me something?

I must add or change something in the web.config file?

I feel?that this problem is in the permissions connecting Web?Service?to?SQL?Server?2005.I give you some suggestions for your reference as the following:
Try to add Network Service in the virtual directory permissions of IIS and make sure the execute permissions is Scripts and Executables through?right mouse clicking the virtual directory?and?choosing?properties?in?IIS.

Web service class undefined on Client load

Having tested a simple atlas web method call from the client which is fired by calling a javascript function on a button client side click event -it works perfectly.

The problem is I need this function called when the page loads, however whenever calling this function from either the body onload client side event or by using the RegisterStratupScript on the server side page load method, I get a javascript error refering to the web service class name as being undefined.

In theory this should work, Am I missing something regarding my understanding of the atlas lifecycle perhaps?

Please help!
Thanks

Dynamic script references (included in the References section of the ScriptManager) are not available until after page loading. To use these, you need to hook the Load event of the Application object. Declaratively, you can do this with:

<application>

<load>

<invokeMethod method="MyMethodName" />

</load>

</application>

Alternatively, you can statically include the service with a <script> tag.


A simpler syntax to do this is to have:

<application load="OnApplicationLoad" />

This goes into your <components>. So you would have something like this:

<script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
<references>
</references>
<components>
<application load="OnApplicationLoad" />
</components>
</page>
</script>

David


BTW, I should mention that there is yet an easier way: just name your method pageLoad and it will be automatically called without having to be registered. See theAtlas docs for some examples.

David