Few random development Tips-N-Tricks-3

1) A simple way to Refresh Image in Browser after upload is complete, using AsyncFileUpload control.

//markup

<form id="form1" runat="server"> 
<asp:ScriptManager ID="scriptManager" runat="server" /> 
<script type="text/javascript" language="javascript">
    function UpLoadStarted(sender, e) {
        var t1 = $get('<%= imgEPhoto.ClientID %>');
        t1.src = "";
    }
    function UploadError(sender, e) {
        alert("Upload error!!");
    }

    function ClientUploadComplete(sender, e) {
        var t1 = $get('<%= imgEPhoto.ClientID %>');
        t1.src = "http://localhost:3763/ExampleAsync/Folder1/Pic1.jpg";
    } 
</script> 
<div> 
<ajaxToolkit:AsyncFileUpload ID="AsyncFileUpload1" runat="server"  
        ClientIDMode="AutoID" UploadingBackColor="Yellow"   
        OnClientUploadStarted="UpLoadStarted" OnClientUploadError="UploadError" OnClientUploadComplete="ClientUploadComplete"   
        onuploadedcomplete="AsyncFileUpload1_UploadedComplete"  /> 
<br /> 
<asp:Image runat="server" Height="50px" Width="50px" ID="imgEPhoto"></asp:Image> 
</div> 
</form>

//codebehind

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
if (AsyncFileUpload1.HasFile)
{
    String SaveLocation = Server.MapPath("Folder1\\Pic1.jpg");
    AsyncFileUpload1.SaveAs(SaveLocation);
    string uploadpath = "./Folder1/Pic1.jpg";

    imgEPhoto.ImageUrl = uploadpath;
}
}

2) A simple way to prevent double submit through Browser Refresh (F5) click.

In this technique below, it saves the current dateTime in a hidden varuable when Submit button is clicked.
On the server end , before executing the button handler it compares this hidden variable with a Session value.
If they don’t match, means submit btn has been clicked, it runs whatever logic and at the end populates the Session variable w/ hidden button’s value, —- else if they match it’s apparent that it came from browser Refresh.
//markup

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BrowserRefresh.aspx.cs" Inherits="BrowserRefresh" %> 
 
<!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 id="Head1" runat="server"> 
<title></title> 
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.js" type="text/javascript"></script>  
<script type="text/javascript">
$(document).ready(function () {
$("[id*=Button1]").click(function () {
    $("[id*=hf1]")[0].value = new Date();
});
});  
</script> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
     
</div> 
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
<asp:HiddenField ID="hf1" runat="server" /> 
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" /> 
</form> 
</body> 
</html>

//codebehind

public partial class BrowserRefresh : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["k1"] = "SomeValue";
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    if (Session["k1"].ToString() == hf1.Value)
    {
        return;
    }
    //do your logic 
    Session["k1"] = hf1.Value;
}
}

The above are from my very own answer, in one of the asp.net forum question. I reproduced again, for easy reference and being interesting-&-new approach, worth sharing.
Thanks for reading.

Advertisements
This entry was posted in Tips-n-Tricks. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s