Thursday, November 10, 2011

Button-rollover with using RegisterClientScriptBlock Method in asp.net

Now let's write a better version of the button rollover by using the RegisterClientScriptBlock method. The problem with the rollover button example from earlier is that when the user's mouse hovered over the button image, the rollover image had to be retrieved from the server in a different request. A better rollover button solution would be where the rollover image of the button is already downloaded and stored in the client's cache so that when the end user hovers over the button, it is immediately displayed. To success this we must create a JavaScript function. The following example shows the JavaScript function as well as the use of the RegisterClientScriptBlock method to get the function onto the page. For this example, the code-behind only needs a Page_Load event for an ImageButton server control.

In .aspx,


<form id="form1" runat="server">
<div>
<asp:ImageButton id="ImageButton1" runat="server" ImageUrl="~/Images/img1.jpg">asp:ImageButton>
<div>
<form>

In code-behind,

protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript",
"<script type='text/javascript'>" +
"if (document.images) {" +
"MyButton = new Image;" +
"MyButtonShaded = new Image;" +
"MyButton.src = 'Images/img1.jpg';" +
"MyButtonShaded.src = 'Images/img2.jpg';" +
"}" +
"else {" +
"MyButton = '';" +
"MyButtonShaded = '';" +
"}" +
"</script>");
ImageButton1.Attributes.Add("onmouseover", "this.src = MyButtonShaded.src;");
ImageButton1.Attributes.Add("onmouseout", "this.src = MyButton.src;");
}

That' it. :)

Performing a Simple Button-rollover


The rollover effect experience is when the end user hovers their mouse over a button on a Web page and then button itself changes color or image. This can be especially useful for Web pages that have attractive buttons, and it would be beneficial from a usability viewpoint to notify the end user of the button they would be clicking prior to clicking it.
This is fairly easy to do before server controls came along and it isn't that difficult now with server controls. The code for performing such an operation is as follows:

<form id="form1" runat="server">
<asp:ImageButton id="ImageButton2" onmouseover="this.src='images/img2.jpg'"
onmouseout="this.src='images/img1.jpg';" runat="server" ImageUrl="~/Images/img1.jpg"/>
<form>

Tuesday, November 8, 2011

How to post data and get data with HttpWebRequest and HttpWebResponse in asp.net

Put the shown below code in code-behind.

protected string PostAndReadDataFromUrl()

{

string para = "";

string retVal = "";

string targetUrl = "";

try

{

targetUrl = "http://test.iblogseeker.com/test.aspx";

para = string.Format("dataRequest={0}", "This is test.");

HttpWebResponse webRes = null;

HttpWebRequest webReq;

webReq = (HttpWebRequest)WebRequest.Create(targetUrl);

webReq.Method = "POST";

webReq.ContentType = "application/x-www-form-urlencoded";

string postData = para;

byte[] bytes = Encoding.ASCII.GetBytes(postData);

Stream os = null;

webReq.ContentLength = bytes.Length;

os = webReq.GetRequestStream();

os.Write(bytes, 0, bytes.Length);

webRes = (HttpWebResponse)webReq.GetResponse();

StreamReader streamRdr = new StreamReader(webRes.GetResponseStream());

retVal = streamRdr.ReadToEnd();

}

catch (Exception ex)

{

retVal = "";

}

return retVal;

}


Enjoy the programming! :)

How to write image downloader using IHttpHandler in asp.net

First of all, create the Generic Handler(.ashx) as below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class Downloader : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            HttpResponse Response = context.Response;
            HttpRequest Request = context.Request;

            string fileName = "IMG.jpg";
            string fullPath = @"G:\" + fileName;  //here is your image path.

            if (!string.IsNullOrEmpty(fileName))
            {
                System.IO.FileInfo finfo = new System.IO.FileInfo(fullPath);
                System.IO.FileStream fs = System.IO.File.OpenRead(fullPath);
                int byteLen = (int)fs.Length;
                byte[] file = new byte[byteLen];
                fs.Read(file, 0, byteLen);
                string name = finfo.Name;
                string size = byteLen.ToString();
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + name);
                Response.AddHeader("Content-Length", size);
                Response.Cache.SetCacheability(HttpCacheability.Public);
                Response.BinaryWrite(file);
                fs.Flush();
                fs.Close();
                HttpContext.Current.ApplicationInstance.CompleteRequest();

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Put the following control to <form> tag.
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Downloader.ashx">Download Image</asp:HyperLink>

That's it. Whenever you click the link, it will show the download dialog box.
Enjoy!