Handling Dynamic Images in ASP.NET 3.5 AJAX Applications - Property Related Value
(Page 5 of 5 )
Next, let's look at the OnGenerateChallengeAndResponse property related value- a JavaScript function, as follows:
protected void MyChallengeResponse(object sender, NoBotEventArgs e)
{
Panel p = new Panel();
Random rand = new Random();
// set the related properties for the panel
p.ID = "panCheckNoBot";
p.Width = rand.Next(123);
p.Height = rand.Next(456);
// hide Panel
p.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
p.Style.Add(HtmlTextWriterStyle.Position, "absolute");
// add control Panel into the NoBot control
((NoBot)sender).Controls.Add(p);
// specify the challenge and response
e.ChallengeScript = String.Format(
"var e = document.getElementById('{0}'); e.offsetWidth + e.offsetHeight;",
p.ClientID);
e.RequiredResponse = (p.Width.Value + p.Height.Value).ToString();
}
In this function, a <div> element with random width and height is generated and then added onto the DOM tree in the page. At the same time, the sum of the width and height is stored, and a JavaScript code snippet is written in the page. Note that this JavaScript code snippet will be invoked at the client-side running time to find the above <div> and obtain its factual sum of its width and height. In this way, when the page postback takes place, the NoBot control can compare the expected value with that fetched from the browser to judge whether the client side is a browser and further judge whether or not the action came from a robot.
Summary
In this two-part series, we have extensively examined the image-related issues under the ASP.NET 3.5 environment by building several simple yet typical sample applications for the corresponding image solutions. One important point you should take notice of is that nearly all the solutions are in relation to the HTTP handlers. Therefore, it is strongly suggested that you become familiar with them first before diving into any of the image-related solutions provided here.
In the last section, we discussed how to use a custom HTTP handler in ASP.NET 3.5 environments to generate images and charts dynamically. Yet, it is noticeable that the HTTP handler is not the recommended tool for creating advanced diagrams. In fact, the HTTP handler is especially suitable to display images at the running time. To generate dynamic diagrams, the preferable solution is to seek help from the server side controls that are developed based upon the HTML <img> element.
Feel free to have a look at the source code for these two articles:
-DOWNLOAD SOURCE PART 2-
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |