Thursday 29 May 2014

AlwaysVisible Control Extender

Step1:
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

Step2:
<style type="text/css"> 

        .staticPanel { 
             width: 150px; 
             background-color: White;   
             border: solid 1px black; 
             padding: 10px;  
        } 
     
    </style>


Step3:
 <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> 
    </asp:ToolkitScriptManager>


Step4:
<asp:Panel ID="Panel1" CssClass="staticPanel" runat="server"> 
        <h2>sujoysantra.blogspot.in</h2> 
    </asp:Panel> 
 
    <asp:AlwaysVisibleControlExtender  
        ID="AlwaysVisibleControlExtender1"  
        TargetControlID="Panel1" 
        VerticalSide="Top" 
        VerticalOffset="10" 
        HorizontalSide="Right" 
        HorizontalOffset="10" 
        runat="server" />  



End Date must be greater than Start date in javascript

function compareDate()
{

var start = document.formName.begindate.value;
var end = document.formName.enddate.value;

var stDate = new Date(start);
var enDate = new Date(end);
var compDate = enDate - stDate;

if(compDate >= 0)
return true;
else
{
alert("Please Enter the correct date ");
return false;
}

Currency checking in javascript

<html>
<head>
<title> </title>
<script type="text/javascript">
    function validate() {
        var currency = document.getElementById("currency").value;
        var pattern = /^\d+(?:\.\d{0,2})$/ ;
        if (pattern.test(currency)) {
            alert("Currency is in valid format");
            return true;
        }
            alert("Currency is not in valid format!Enter in 00.00 format");
            return false;

    }
</script>
</head>
<body>
<h2>Validating Currency..</h2>
Enter Currency:<input type="text" name="currency" id="currency" />
<input type="submit" value="Check" onclick="validate();" />
</body>
</html>

Thursday 22 May 2014

Get Filenames From Folder in Gridview Control in ASP.Net


Introduction
This article introduces the Directory class and shows how to get the file names from a folder into a GridView control.

Directory Class
The System.IO.Directory class in the .NET Framework class library provides static methods for creating, copying, moving, and deleting directories and subdirectories. For using the Directory class you need to use the System.IO namespace.

Directory.GetFiles method
The GetFiles method returns the names of files in a specified directory or returns a string array of file names.

protected void BindGridview()
    {
        string[] filesLoc = Directory.GetFiles(Server.MapPath("~/MyFiles/"));
        List<ListItem> files = new List<ListItem>();
        foreach (string file in filesLoc )
        {
            files.Add(new ListItem(Path.GetFileName(file)));
        }
        gvFiles.DataSource = files;
        gvFiles.DataBind();
    }

Binding User Control Data on Button Click With Modal Popup

In this article I will explain how to bind User Control data on a button click with a modal popup.

Step 1:
Create a user control. I created the UserInfo.ascx User Control in the given sample. 
<table width="100%" border="0" cellspacing="0" style="background-color:white">    <tr>        <td>            User Name:
       
</td>        <td>            <asp:Label ID="UserNameLabel" runat="server"></asp:Label>        </td>    </tr>    <tr>        <td>Address1:</td>        <td>            <asp:Label ID="Address1Label" runat="server"></asp:Label>        </td>    </tr>    <tr>        <td>Address2:</td>        <td>            <asp:Label ID="Address2Label" runat="server">            </asp:Label>        </td>    </tr>    <tr>        <td>            City:
       
</td>        <td>            <asp:Label ID="CityLabel" runat="server"></asp:Label>        </td>    </tr>    <tr>        <td>            Country:
       
</td>        <td>            <asp:Label ID="CountryLable" runat="server"></asp:Label>        </td>    </tr>    <tr>        <td colspan="2">
//For hide the modal popup
            <a href="#" id="Cancel" onclick="$find('ShowUserInfoModalPopUp').hide(); return false;">Cancel</a>        </td>
   
</tr></table>

Step 2. 
In UserInfo.ascx.cs create a function BindUser(), to bind the user information.
public void BindUser()
{
    UserNameLabel.Text =
"Ish Bandhu";
    Address1Label.Text =
"XYZ flat no 445";
    Address2Label.Text =
"Street no 1";
    CityLabel.Text =
"Noida";
    CountryLable.Text =
"India";
 
}
 

Step  3. 
Register that user control in your page.
<%@ Register Src="~/UserInfo.ascx" TagName="UserInformation" TagPrefix="uc1" %>

Step  4. 
Register the toolkit control in your page.
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
Step  5. 
Define a CSS class to show the modal popup background as gray.
<style type="text/css">    .modalBackGround {
     
background-color: gray;
     
filter: alpha(opacity=50);
     
opacity: 0.7;
  }
</style>
Step 6. 
Add a script manager to your page:
<asp:ScriptManager runat="server" ID="ScriptManager1">
</
asp:ScriptManager>

Step 7.
 Add a modalpopextender to your page:
<asp:Button ID="TargetButton" runat="server" Style="display: none" />        <ajaxToolkit:ModalPopupExtender ID="ShowUserInfoModalPopUp" runat="server" PopupControlID="UserInfoPanel" DropShadow="true"            TargetControlID="TargetButton" BackgroundCssClass="modalBackGround" RepositionMode="RepositionOnWindowResizeAndScroll"
BehaviorID="ShowUserInfoModalPopUp"></ajaxToolkit:ModalPopupExtender>

// PopupControlID:This is panel name in which your user control is defined.
Step 8. 
Add a panel and add the userinfo User Control to that panel:
<asp:Panel ID="UserInfoPanel" runat="server" Style="display: none">            <asp:UpdatePanel ID="UserInfoUpdatePanel" runat="server" UpdateMode="Conditional">                <ContentTemplate>                    <uc1:UserInformation ID="UserInformationControl" runat="server" />                </ContentTemplate>            </asp:UpdatePanel>       </asp:Panel>

You can see that I added the userinfo User Control within the update panel, because after binding the user information we will call the update method of that update panel to update the user control information.
Step 9.
 Add a button to show the user information in the modalpopup:
<asp:Button ID="ShowUserInforButton" runat="server" OnClick="ShowUserInforButton_Click" Text="Show User Information" />
Step 10. 
On the click event of the above mention button write the code:
protected void ShowUserInforButton_Click(object sender, EventArgs e)
{
    
UserInfo userInfo = (UserInfo)Page.FindControl("UserInformationControl");
    userInfo.BindUser();
    ShowUserInfoModalPopUp.Show();
}

Bind Files in Gridview and Download in ZIP Folder in ASP.NET

Introduction

This article shows how to bind files in a GridView and how to download selected files in zip format.


Requirements 
Dll Ionic.Zip.dll 

ASPX Page:
<html xmlns="http://www.w3.org/1999/xhtml">
<
head>    <title>Download in zip format</title>
</
head>
<
body>    <form id="form1" runat="server">        <div>            <br />            <asp:Label ID="lbl_txt" runat="server" Font-Bold="true" ForeColor="Red" />        </div>        <asp:GridView ID="gridview1" CellPadding="5" runat="server" AutoGenerateColumns="false">            <Columns>                <asp:TemplateField>                    <ItemTemplate>                        <asp:CheckBox ID="chk_Select" runat="server" />                    </ItemTemplate>                </asp:TemplateField>                <asp:BoundField DataField="Text" HeaderText="FileName" />            </Columns>            <HeaderStyle BackColor="white" Font-Bold="true" ForeColor="White" />        </asp:GridView>        <asp:Button ID="btn_Download" Text="Download " runat="server" OnClick="btnDownload_Click" />    </form>
</
body>
</
html>



CS Page:

protected void BindGridview()
{
    
string[] files_Path = Directory.GetFiles(Server.MapPath("~/files/"));
    List<ListItem> files =
new List<ListItem>();
   
foreach (string path in files_Path)
    {
        files.Add(
new ListItem(Path.GetFileName(path)));
    }
        gridview1.DataSource = files;
        gridview1.DataBind();
    }
    Call
this function on pageload :
   
if (!IsPostBack)
    {
        BindGridview();
    }
}



Now write the following code for the Download button click:
using (ZipFile zip = new ZipFile())
{
   
foreach (GridViewRow gr in gridview1.Rows)
    {
    CheckBox chk = (CheckBox)gr.FindControl(
"chkSelect");
   
if (chk.Checked)
    {
       
string fileName = gr.Cells[1].Text;
       
string filePath = Server.MapPath("~/files/" + fileName);
        zip.AddFile(filePath,
"files");
    }
}
Response.Clear();
Response.AddHeader(
"Content-Disposition", "attachment; filename=DownloadedFile.zip");Response.ContentType = "application/zip";zip.Save(Response.OutputStream);Response.End();}