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

Wednesday, March 28, 2012

Programmatically adding a ConfirmButtonExtender

Hi,

I cannot get my ConfirmButtonExtender to work when I add it from the code behind.

A javascript page error occurs the page loads:

Line: 2829
Char:23
Error: Sys.ArgumentException: Value must not be null for Controls and Behaviors.
Parameter name: element
Code:0

---------------

Here is the code:

-- .asp snippet----

 <asp:ScriptManager ID="ScriptMan1" runat="server"> </asp:ScriptManager> <asp:Panel ID="Panel1" runat="server"> </asp:Panel> 

---.asp.cs snippet----

protected void Page_Load(object sender, EventArgs e) { Button button =new Button(); button.Text ="Click Me"; Panel1.Controls.Add(button); AjaxControlToolkit.ConfirmButtonExtender confirmDeleteExt =new AjaxControlToolkit.ConfirmButtonExtender(); confirmDeleteExt.TargetControlID = button.UniqueID; confirmDeleteExt.ConfirmText ="Are you sure?"; Panel1.Controls.Add(confirmDeleteExt); }

----------

It's nothing special, but what am I missing?!

I am dynamically building the page, so beginning with the button on the page is not an option (which works no problem).

Thanks!

Your code works fine on my computer.


OK,

Thanks for your reply, kipo, looking at it again it seems that the error only seems to occur when using a masterpage.

Does it still work for you if you put all of it into a masterpage?


I've tried to put your code in page which is using MasterPage and it doesn't work, so you were right - error is occuring only with MasterPage. But, you can achieve it with this code:

Button button = new Button();
button.Text = "Click Me";
button.ID = "button1";
Panel1.Controls.Add(button);

AjaxControlToolkit.ConfirmButtonExtender confirmDeleteExt = new AjaxControlToolkit.ConfirmButtonExtender();
confirmDeleteExt.TargetControlID = "button1";
confirmDeleteExt.ConfirmText = "Are you sure?";
Panel1.Controls.Add(confirmDeleteExt);


Excellent!Big Smile

Thanks a lot for your help!

I thought this problem was related to IDs, but is this a bug or is this by design?

Monday, March 26, 2012

Programmatically trigger modal popup

I want to write a page that checks for a cookie via Javascript and then possibly opens a modal dialog box. How do I trigger the modal popup via client side javascript?

I saw the example that comes on the modal dialog that sample page, however that works with a client side event such as a click. Can I do it without listening for an event?

Hi theregit,

In the javascript where you check the cookie, you can trigger a button.

That button's click event can then be another javascript that opens a modal window.

Trigger the button like:
var myButton = document.getElementById('myButton');
myButton.click();

Is this an answer to your question?
if you have comments/remarks/questions .. please do so!

Kind regards
Wim


Try this ,

Show and Hide ModalPopupExtender from JavaScript

Hope this helps


Phanatic,

Thank you sooo much for that post. I was having the problem with the 'null' is null or not an object error. Once I used the pageLoad() method it worked great.

Property animation in javascript.

How do i perform a property animation in javascript? the code below does not do anything... anything wrong?

offSet = 40;
controlSize = 19;
animation = new AjaxControlToolkit.Animation.PropertyAnimation(
panel,
0.25,
100,
'style',
'height');
animation.setValue(offSet + numOfControls*controlSize + "px");
animation.play();

Hi ,

Try setting the Width Property to "null" in the ReSize animation .

Ex :

//AjaxControlToolkit.Animation.ResizeAnimation.play(target, duration, fps, width, height, unit);

AjaxControlToolkit.Animation.ResizeAnimation.play( $get("ID") , 0.3 , 125 ,null , 500 ,"px" );

Hope this helps


HI,

I am assuming that resolved it .

Feel free to correct me .


Hey,

Another way to increase only the height .

AjaxControlToolkit.Animation.LengthAnimation.play(panel, 0.25, 50 ,

'style' ,'height', 100 , 400 ,'px');

This is better than the Resize animation with the width set to null.

Protecting AJAX Javascript - how?!?

Hi, I'm using AJAX so I've got into this question - as AJAX uses Javascript "powers", it means that the client is able to get more information about services or other stuff of that site correct?

Is there any way to encrypt, protect or reduce the probability of getting that data to make damage or access denied places or funcions? AJAX is really nice and powerful, but security on the web is still a big issue!Thanks a lot!

I personally like this articles about Ajax security, look at the development section:

http://www.cgisecurity.com/ajax/

Protecting javascript based on role

To set up what i am going to ask, allow me to explain the page:

I've got a page that shows reports with a popup panel that has all sorts of options on it.... when i first load the page, i show/hide/fill these options based on Roles

The page does not do any post backs, just javascript calls to webservices and i show the report data based onthis blog post by Scott Guthrie (using a page instance, a user control and generating/grabbing the HTML)....

In the aspx markup of the page is hundreds of lines of javascript i have written over the past week, and what i am realizing is that all the script is there for the world to see regardless of role....

So where i may say: $get('drpManager') , that is a control that is only available to certain role (i set visible and enabled to false if the user isn't allowed to see it), but just seeing that in the code, a malicious user may be able to figure out how to do something that they shouldn't be allowed to...

So what I am asking/wondering is maybe some ideas to package the javascript, broken up into role specific functionality, into the page execution and spit it out (this cried out WebResource and ScriptManager.RegisterScript) based on role

Any thoughts on this or pointers?

Unfortunately ( and I hope someone with greater knowledge than I will lambast me for saying something inaccurate)... is no - there is no such thing as security when it comes to scripts. This is the only primary reason I handle everything from the server side when it comes to security specific issues...You can however authenticate using the profile feature of Ajax...but even if you use any of the number of javascript encryptors available (most will not work with Ajax anyways). You will be exposing your logic to those whom realize that you are doing something sensistive..

Naturally you could could create seperate javascripts depending on the role it would be more secure - but if you are using anything Ajax in a security related enviroment - it is my recomended best practice handle it serverside and not clientside... the less the client knows what is going on means the less a hacker knows what is going on... Ajax is afterall just a tool... and security wise - its is the the subject of numerous related security articles because there really is no framework to protect against plain text script... but I would not rely on a global and executing based upon parameters sent to the client on the client...if you must and refuse to do the stuff server side and take the perf hit while maintaining the ajax ui glitz for the customer... then send only the scripts to the client that are related for the role they have...


jodywbcb:

then send only the scripts to the client that are related for the role they have...

Apparently you missed the whole entire point of my topic and the question i asked at the very end, as the above line by you that i quoted is exactly what i was asking for ideas onhow to do...

I wasn't asking for page architecture advice... the page is big and it's fast as hell and getting/updated/refreshing sections of the report piece by piece via AJAX, which absolutely is the best way for this page to serve it's existance, now i am just asking about steps, even if it's just a little step, on keeping code out that a lesser role doesn't need

Prototype and Rico library port of Rounded Corner functionality

Good morning!

As an exercise, I recently ported the rounded corner feature provided by theRico javascript libraries based on the open sourceprototype libraries. I ported that function as an Extender in Atlas, and I it turned out pretty well. I was wondering if anyone here would be interested in checking it out. If so, I will post it on myweb site for interested parties to download.

Check out the Rico link above and view the "Rounded Corner" samples to see the functionality I am talking about.

Answer here to let me know if there is interest, so I know to do it.

I would be very interested in seeing that - please post to your site if you don't mind.

-Josh


yes, please share - sounds good!
Very cool!
Okay, everyone! I will post a link to an example later tonight!

Okay! I was a little late, because I had to fix a bug in the Rico library related to padding! But here it is atmy web site!

Feel free to check it out and tell me what you think! It implements all the semantics of the Rico rounded corner library exactly.


Fantastic work!

I will be playing around with this over the weekend.


Awesome... this is great to see... and a great example of what behaviors are all about.

Saturday, March 24, 2012

Question about object returned from web service...

I have the following script code:

<

scriptlanguage="javascript"type="text/javascript">

<!--

function

Button1_onclick() {

ret = SimpleService.ReturnDataTable(

"fi","la","te", OnComplete, OnTimeOut, OnError);

return

(true);

}

function

OnComplete(arg) {

document.getElementById(

'Text1').value = arg;

//alert(arg);

}

function

OnTimeOut(arg) {alert("TimeOut encountered when calling Say Hello.");

}

function

OnError(arg) {

<

scriptlanguage="javascript"type="text/javascript">

<!--

function

Button1_onclick() {

ret = SimpleService.ReturnDataTable(

"fi","la","te", OnComplete, OnTimeOut, OnError);

return

(true);

}

function

OnComplete(arg) {

document.getElementById(

'Text1').value = arg;

}

function

OnTimeOut(arg) {alert("Error encountered when calling Say Hello.");

}

function

OnError(arg) {

alert(

"Error encountered when calling Say Hello.");

}

// -->

}

</script>

And this is the webservice method it calls:

publicDataSet ReturnDataTable(String Name,String Last,String Test)

{

DataSet ds =newDataSet();DataTable dt = ds.Tables.Add("RandomTable");DMStoDD dd =newDMStoDD();DataColumn column1 =newDataColumn();

column1.ColumnName =

"Name";

dt.Columns.Add(column1);

DataColumn column2 =newDataColumn();

column2.ColumnName =

"Last";

dt.Columns.Add(column2);

DataColumn column3 =newDataColumn();

column3.ColumnName =

"Test";

dt.Columns.Add(column3);

DataRow row;

row = dt.NewRow();

row[

"Name"] = Name;

dt.Rows.Add(row);

row = dt.NewRow();

row[

"Last"] = Last;

dt.Rows.Add(row);

row = dt.NewRow();

row[

"Test"] = Test;

dt.Rows.Add(row);

return ds;

}

Is there something wrong with the way I'm calling my DataSet/DataTable?

Also, what properties does the client side object 'arg' have?

J

arg should have whatever properties are public on the server-side object (I believe) but only if you have a [ScriptService] attribute on the web service, and only if you have a jsonconverter defined for the data type. The default converter works pretty well for most data types, but seems to choke a bit on certain things. I've never done dataset/datatables (typically I roll my own collection for those applications) but from what I've read you can get a dataset converter if you use the Futures dll and make the appropriate changes to your web.config to suppor tit.

Wednesday, March 21, 2012

question on Ajax and .NET method

hi all,

is it possible to pass a javascript object (which has key value pairs) to a C# method through an Ajax call.

if yes,

can you please give me some sample code on how to refer to those key values in C# method?

Thanks in advaance

Sanjay

Using AJAX, you are making a call to the server. Your key value pair in Javascript is serialized in to string with some dilimeter when sent to the server right?

I guess, you just need to parse the string on the server!


thanks for the reply

the key value pair is not serialized into a string. it is a javascript object [like myObj[id] = 1; myObj[name] = 'test'....]

and i'm using Ajax to make a call to C# method [which does some sql] and

i need to pass the javascript object [myObj] to C# method and parse the object and use the values in sql

can you please give me some simple sample code for this

thank you

Sanjay


Hi sanjayk,

It is possible. Here is a sample shows how to pass a javascript object to a C# method through a button click event. For using Ajax call , you should do very little change.

ASPX:

<%@. Page Language="C#" AutoEventWireup="true" CodeFile="JSON.aspx.cs" Inherits="ClientScript_JSON" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>JSON Test</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <div> Display client serialize result:<asp:TextBox ID="TextBox1" runat="server" Text="5555" Style=""></asp:TextBox><br /> <br /> <asp:Label ID="Label1" runat="server" Text="Show FirstName here"></asp:Label> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form> <script type="text/javascript" language="javascript"> function pageLoad(){ var myObj = new Object(); myObj.FirstName = "Jonathan"; myObj.LastName = "Shen"; myObj.Title ="MSTF"; var serialiedObj = Sys.Serialization.JavaScriptSerializer.serialize(myObj); $get("<%=TextBox1.ClientID%>").value = serialiedObj; } </script></body></html>

C# Code:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Script.Serialization;

public partial class ClientScript_JSON : System.Web.UI.Page
{
JavaScriptSerializer serializer;

protected void Page_Load(object sender, EventArgs e)
{
serializer = new JavaScriptSerializer();
}
protected void Button1_Click(object sender, EventArgs e)
{
string jsonstring = Page.Request.Params["TextBox1"].ToString();
Dictionary<string, string> temp = serializer.Deserialize<Dictionary<string,string>>(jsonstring);
this.Label1.Text ="FirstName: "+temp["FirstName"].ToString();
}
}

For more details , please visit this url: http://ajax.asp.net/docs/ViewSample.aspx?sref=System.Web.Script.Serialization/cs/App_Code/ListItemCollectionConverter.cs

Hope it helps. If i misunderstood you, please let me know.


Hi Jonathan,

Thank you for your reply, i figured it out by passing Complex DataTypes to C# methods.

Sanjay

question on ajax & webservices

hello all again

here is what i'm doing,

i've a javascript object, which has key value pairs, and some values are objects themselves.

i'm passing this object to a method in a web service, and i'm able to get the values and also able to see the object (which is a value for some keys),

and also i can read that object (and values) in the callback function in my javascript file.

now my question is - i'm unable to refer to the object's value in .asmx file?

.js object looks like this:

myObj = new customObject(); // customObject is a public class in .cs file with get() and set() methods

myObj['id'] = 1;

myObj['name'] = 'test';

addressObj = new secondCustomObject(); // secondCustomObject is a public class in .cs file with get() and set() methods

addressObj['streeName'] = 'test Street Name';

addressObj['City'] = 'test City';

myObj['address'] = addressObj;

webservice.methodName(myObj, callback);

can anyone guide me how to read the addressObj's values in .asmx file?

Thank you

Sanjay

Hi

Would you please post a demo of your issue?

To troubleshoot this issue, we really need the source code of your webservice to reproduce the problem, so that we can investigate the issue in house. It is not necessary that you send out the complete source of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business logic from it.

Thank you.


Hi

Basically we only need to modify the proxy class that is generated when you add a web reference. I googled and found a nice article about this.

http://ryanfarley.com/blog/archive/2004/05/26/737.aspx


Let me know if you need more info.

Hope this helps.

Thanks!

NOTE:This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


Hi

Demo:

Page:

<%@. Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<script type="text/javascript">
function HelloWorld()
{
myObj = new person("Andrew","Yin");
myObj["firstName"] = "Jin Yu";
myObj["lastName"] = "Yin";
TestWebService.HelloWorld(myObj,SucceededCallback);
}

function person(fName, lName)
{
this.firstName = fName;
this.lastName = lName;
}
function SucceededCallback(result, eventArgs)
{
// Page element to display feedback.
var RsltElem = document.getElementById("ResultId");
RsltElem.value = result;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server" ID="scriptManagerId">
<Services>
<asp:ServiceReference Path="TestWebService.asmx" />
</Services>

</asp:ScriptManager>
<input id="ResultId" type="text" />
<input id="Button1" type="button" value="button" onclick="javascript: HelloWorld();" /></div>
</form>
</body>
</html>

WebService And Class:

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;


/// <summary>
/// Summary description for TestWebService
/// </summary>
[ScriptService]
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TestWebService : System.Web.Services.WebService {

public TestWebService () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld(person p) {
return "Hello World, " + p.firstName + " " + p.lastName;
}

}

public class person
{
public string firstName = "";
public string lastName = "";
}

Hope this helps.

Thanks


Thank you Yin-Yu for your reply,

I figured that part out, what i'm doing is pass the javascript object with key value pairs and some values are objects [with key value pairs] themselves.

in .asmx file i created custom datatypes for 1. key value pairs and 2. objects. this way i can see my elements [belongs to child objects] in my .asmx file

Thank you

Sanjay