Thursday, December 18, 2008

Generating Random Image on a button Click

Generating Random Image on a button Click


public static string GetRandomPassword(int length)
{
Random rand = new Random();
System.Text.StringBuilder password = new System.Text.StringBuilder(length);
for (int i = 1; i <= length; i++)
{
int charIndex;
do {
charIndex = rand.Next(48, 123);

}
while (!((charIndex >= 48 && charIndex <= 57) ||(charIndex >= 65 && charIndex <= 90)|| (charIndex >= 97 && charIndex <= 122)));
// add the random char to the password being built
password.Append(Convert.ToChar(charIndex));
}
return password.ToString();
}


private void GenerateImage(string strRegistrationStr)
{
System.Drawing.Bitmap objBitmap = new Bitmap(150, 30);
Graphics objGraphics = Graphics.FromImage(objBitmap);

//Fore Color and Background of Image
SolidBrush objForeColor = new SolidBrush(Color.White);
SolidBrush objBackColor = new SolidBrush(Color.Black);

objGraphics.FillRectangle(objBackColor, 0, 0, 150, 30);
//Font settings for Image
Font objFont = new Font("Arial", 15);
//Display from 5point from x-axis and 5point from y-axis
Point objPoint = new Point(5, 5);
//Drawing Registration String
objGraphics.DrawString(strRegistrationStr, objFont, objForeColor, objPoint);
//Saving Registration String as Image
//If you dont want image to save
//objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
//otherwise use this
objBitmap.Save(Server.MapPath("RegistrationImg.gif"), ImageFormat.Gif);
//Release object
if (objBitmap != null)
objBitmap.Dispose();
if (objGraphics != null)
objGraphics.Dispose();
}

protected void butRegister_Click(object sender, EventArgs e)
{
GenerateImage(GetRandomPassword(10));
ImgRegistrationStr.ImageUrl = "RegistrationImg.gif";
}





Thanks,
Nitin Sharma

Tuesday, December 16, 2008

Fetching QueryString Parameters by Javascript

script language="javascript" type="text/javascript"
// Create a new QueryString object
//alert( window.top.location.search.substring(1));

var qrStr = window.location.search;
var spQrStr = qrStr.substring(1);
var arrQrStr = new Array();

// splits each of pair
var arr = spQrStr.split("&");

for (var i=0;i// splits each of field-value pair
var index = arr[i].indexOf("=");
var key = arr[i].substring(0,index);
var val = arr[i].substring(index+1);

// saves each of field-value pair in an array variable
arrQrStr[key] = val;
}


alert("Parameter:" + arrQrStr["company"]);

script


you just change the name of the paremeter in the alert('Parameter Name: '+ arrQrStr["company"]);

Here it is Company


Thanks,
Nitin Sharma

Monday, December 15, 2008

create,Read and erase Cookie in Javascript

The scripts
These are the three scripts you need
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}


function eraseCookie(name) {
createCookie(name,"",-1);
}

Thanks,
Nitin Sharma

Thursday, December 4, 2008

Yield Keyword in C#

One interesting new feature of the C# 2।0 is the "yield" keyword. Basically it is used to iterate through objects returned by a method. It creates a state engine in IL so you can create methods that retain their state and dont have to go through the pain of maintaining state in your code.



public static IEnumerable GetInt()
{
for (int i = 0; i <>
yield return i;
}


Here's the implementation.


class प्रोग्राम
{ static void Main(string[] args)
{
foreach (int i in GetInt())
Console।WriteLine("Got " + i।ToString());
}


public static IEnumerable GetInt()
{
for (int i = 0; i <>
yield return i;
}
}

Thanks,
Nitin Sharma

Tuesday, December 2, 2008

Consuming Web Service by AJAX SlideShow Extender Control

Hi All,
     Here is a way to Consume Web Service in  AJAX SlideShowExtender Control using Context Key..

in .aspx page::

 if (Request.QueryString["Id"] != null)
        {
            uint Id = Convert.ToUInt16(Request.QueryString["Id"].ToString());
                       
          SlideShowExtender1.ContextKey = Id.ToString(); //Passing the ID attribute Dynamically
            
        }
 ------------------------------------------------------------------------------------
   div align="center"
    asp:Label runat="Server" ID="imageTitle" CssClass="slideTitle" 

    asp:Image ID="ImagePlayer" runat="server" Height="300" Style="border: 1px 
solid black; width: auto" 
    asp:Label runat="server" ID="imageDescription" CssClass="slideDescription"
    asp:Label
    
    asp:Button runat="Server" ID="btnBack" Text="Prev" Font-Size="Larger" 
    asp:Button runat="Server" ID="btnPlay" Text="Play" Font-Size="Larger" 
    asp:Button runat="Server" ID="btnNext" Text="Next" Font-Size="Larger" 
    cc1:SlideShowExtender ID="SlideShowExtender1" runat="server" TargetControlID="ImagePlayer"
        AutoPlay="True" Loop="True" NextButtonID="btnNext" PlayButtonID="btnPlay" PlayButtonText="Play"
        PreviousButtonID="btnBack" SlideShowServiceMethod="GetSlides" SlideShowServicePath="~/Test.asmx"
        StopButtonText="Pause" ImageDescriptionLabelID="imageDescription" ImageTitleLabelID="ImgTitle"
    cc1:SlideShowExtender>
    div

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

In .asmx(web service).cs page::
 [WebMethod]
    public AjaxControlToolkit.Slide[] GetSlides(string contextKey)
    {
        System.Web.UI.Page ObjUIPage = new System.Web.UI.Page();
        int i = 0;
        int j = 0;

        //'The below line specifies the Directory to be used.Here the images are in images folder of my application
        System.IO.DirectoryInfo DirInfo = new System.IO.DirectoryInfo(ObjUIPage.Server.MapPath("") + "\\PropertyUnitImages\\" + contextKey);
        System.IO.DirectoryInfo[] subDirs = DirInfo.GetDirectories();

        //'The below line gets the list of files in the directory specified above
        System.IO.FileInfo[] Files = DirInfo.GetFiles();

        //The below line gives the file count, which is useful to specify the size of array
        j = Files.Length;

        Slide[] mySlide = new AjaxControlToolkit.Slide[j + 1];
        //System.IO.FileInfo di = default(System.IO.FileInfo);

        //' This loop continues upto the last file in the directory
        foreach (var d in Files)
        {
            mySlide[i] = new AjaxControlToolkit.Slide(d.FullName, d.Name, d.Name.Substring(0, d.Name.IndexOf(".")));
            i = i + 1;
        }
        return mySlide;
        //' this line sends the dynamically added details as AjaxControlToolkit.Slide
    }
      





A word onContext Key  property of SlideShowExtender:



The parameter is contextKey of type String. You can use contextKey in anyway you want, it is meant to pass information from the calling AJAX control to the web service. In this application, the parameter will be used to supply the directory for the slide show to be fed from. The return parameter is an array of Slides. The AJAX SlideShow control looks for this signature and the signature is case sensitive and exact. Don't try to change any of this syntax or your service not be called.

Thanks,
Nitin Sharma