Friday, December 18, 2009

gridview row can use check box to download file and display excel format

1 . script language="javascript" type="text/javascript"


var AFID = "";
function GetID(rowID, AudioFileID) {
AFID = AudioFileID;
ChangeColor(rowID, 'gvAudioFiles');
}
function ChangeRowColorForMouseMove(rowID) {
ChangeColor1(rowID, 'gvAudioFiles');
}


/script


2. script type="text/javascript" language="javascript"


function SelectAll(CheckBox)
{
TotalChkBx = parseInt('<%= this.gvAudioFiles.Rows.Count %>');
var TargetBaseControl = document.getElementById('<%= this.gvAudioFiles.ClientID %>');
var TargetChildControl = "chkSelect";
var Inputs = TargetBaseControl.getElementsByTagName("input");
for(var iCount = 0; iCount < type ="=">= 0)
Inputs[iCount].checked = CheckBox.checked;
}
}

function SelectDeSelectHeader(CheckBox)
{
TotalChkBx = parseInt('<%= this.gvAudioFiles.Rows.Count %>');
var TargetBaseControl = document.getElementById('<%= this.gvAudioFiles.ClientID %>');
var TargetChildControl = "chkSelect";
var TargetHeaderControl = "chkSelectAll";
var Inputs = TargetBaseControl.getElementsByTagName("input");
var flag = false;
var HeaderCheckBox;
for(var iCount = 0; iCount < type ="=">= 0)
HeaderCheckBox = Inputs[iCount];
if(Inputs[iCount] != CheckBox && Inputs[iCount].type == 'checkbox' && Inputs[iCount].id.indexOf(TargetChildControl,0) >= 0 && Inputs[iCount].id.indexOf(TargetHeaderControl,0) == -1)
{
if(CheckBox.checked)
{
if(!Inputs[iCount].checked)
{
flag = false;
HeaderCheckBox.checked = false;
return;
}
else
flag = true;
}
else if(!CheckBox.checked)
HeaderCheckBox.checked = false;
}
}
if(flag)
HeaderCheckBox.checked = CheckBox.checked
}

/script

3. script type="text/javascript"


/*Check box validation in gridview*/
var TargetBaseControl = null;

window.onload = function()
{
try
{
//get target base control.
TargetBaseControl =
document.getElementById('<%= this.gvAudioFiles.ClientID %>');
}
catch(err)
{
TargetBaseControl = null;
}
}

function TestCheckBox()
{
if(TargetBaseControl == null) return false;

//get target child control.
var TargetChildControl = "chkSelect";

//get all the control of the type INPUT in the base control.
var Inputs = TargetBaseControl.getElementsByTagName("input");

for(var n = 0; n < type ="=">= 0 &&
Inputs[n].checked)
return true;

alert('Select at least one audio file!');
return false;
}
/script



4. Gridview


asp:GridView ID="gvAudioFiles" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvAudioFiles_RowDataBound" Width="100%" AllowPaging="true" AllowSorting="true" OnPageIndexChanging="gvAudioFiles_PageIndexChanging1" OnSorting="gvAudioFiles_Sorting">


Columns

asp:TemplateField HeaderText=""

HeaderTemplate

asp:CheckBox ID="chkSelectAll" runat="server"onclick="SelectAll(this);" />

/HeaderTemplate
ItemTemplate
asp:CheckBox ID="chkSelect" runat="server" /
/ItemTemplate
/asp:TemplateField
asp:BoundField DataField="DoctorName" HeaderText="Doctor Name" HeaderStyle-CssClass="submenu"
SortExpression="DoctorName" /
asp:TemplateField HeaderText="Audio File" HeaderStyle-CssClass="submenu"
ItemTemplate
asp:HyperLink ID="hlnkAudio" runat="server" Target="_blank" Text='<%#Eval("AudioFileName") %>'
ToolTip="Click here to download" NavigateUrl='<%#string.Concat("~/AudioFiles/",DataBinder.Eval(Container.DataItem, "UserName"),"/",Convert.ToDateTime(Eval("FileDate")).ToLongDateString(),"//", DataBinder.Eval(Container.DataItem, "AudioFileName")) %>'
/asp:HyperLink
asp:Label ID="lblAudioFileID" runat="server" Text='<%#Eval("AudioFileID") %>' Visible="false" /
/ItemTemplate
/asp:TemplateField
asp:TemplateField HeaderText="File Added on" HeaderStyle-CssClass="submenu" SortExpression="FileDate"
ItemTemplate
asp:Label ID="lblAudioFileDate" runat="server" Text='<%# Convert.ToDateTime(Eval("FileDate")).ToShortDateString()%>' /
/ItemTemplate
/asp:TemplateField
/Columns
/asp:GridView


5. Buttons


asp:Button ID="btnAssign" runat="server" Text="Assign" CssClass="submitbutton" OnClick="btnAssign_Click"
PostBackUrl="AssignAudioFilesToQA.aspx" /

asp:Button ID="btnFileIDs" runat="server" Visible="false"
/asp:Button
asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="btnDownload_Click" /
asp:Button ID="Button1" runat="server" Text="Export to Excel" OnClick="Button1_Click" /


6 c#

in project add bin this file

ICSharpCode.SharpZipLib.dll


7. code c#

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using StreamlineMT.BO;
using StreamlineMT.BLL;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using System.IO;

public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
VerifyRenderingInServerForm(form1);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
btnAssign.Attributes.Add("onclick", "if (!TestCheckBox()) { return false; }");
if (!Page.IsPostBack)
{
ViewState["sortOrder"] = "";
bindGridView("", "");
}
// GetAudioFiles();
}

private void bindGridView(string sortExp, string sortDir)
{
AudioFileBLL objBLL = new AudioFileBLL();
AudioFileBO objBO = new AudioFileBO();
DataSet ds = new DataSet();
objBO.Type = "SearchFiles";
objBO.FromDate = "12/01/2009";
objBO.ToDate = "12/04/2009";
ds = objBLL.GetAudioFiles(objBO);

if (ds.Tables[0].Rows.Count == 0)

btnAssign.Visible = false;
else
{
DataView myDataView = new DataView();
myDataView = ds.Tables[0].DefaultView;
if (sortExp != string.Empty)
{
myDataView.Sort = string.Format("{0} {1}", sortExp, sortDir);
}
btnAssign.Visible = true;
myDataView.RowFilter = "(FileDate >='12/04/2009 00:00:01') and (FileDate<='12/04/2009 23:59:59')"; gvAudioFiles.DataSource = myDataView; gvAudioFiles.DataBind(); } } protected void gvAudioFiles_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //Label lblStatus = (Label)e.Row.FindControl("lblStatus"); //HyperLink hlnkAssign = (HyperLink)e.Row.FindControl("hlnkAssign"); //if (lblStatus.Text == "") // hlnkAssign.Visible = true; //else // hlnkAssign.Visible = false; //if (lblStatus.Text == "8") // e.Row.Cells[5].Style.Add("background-color", "#ffa463"); string rowID = String.Empty; if (e.Row.RowType == DataControlRowType.DataRow) { rowID = "row" + e.Row.RowIndex; e.Row.Attributes.Add("Id", rowID); // e.Row.Attributes.Add("onclick", "javascript:GetID('" + rowID + "','" + DataBinder.Eval(e.Row.DataItem, "TemplateID") + "');"); e.Row.Attributes.Add("onmouseover", "javascript:ChangeRowColorForMouseMove('" + rowID + "');"); } } //Select All if (e.Row.RowType == DataControlRowType.Header) { //adding an attribute for onclick event on the check box in the header //and passing the ClientID of the Select All checkbox //((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID + "')"); } try { if (e.Row.RowType == DataControlRowType.DataRow) { string strScript = "SelectDeSelectHeader(" + ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).ClientID + ");"; ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Attributes.Add("onclick", strScript); } } catch (Exception Ex) { //report error } } protected void btnAssign_Click(object sender, EventArgs e) { string fileIDs = ""; foreach (GridViewRow gr in gvAudioFiles.Rows) { CheckBox chkSelect = (CheckBox)gr.FindControl("chkSelect"); if (chkSelect.Checked) { Label lblAudioFileID = (Label)gr.FindControl("lblAudioFileID"); fileIDs = fileIDs + lblAudioFileID.Text + ","; //SqlParameter S1=new SqlParameter( } } if (fileIDs != "") { btnFileIDs.Text = fileIDs.TrimEnd(','); btnAssign.PostBackUrl = "AssignAudioFilesToQA.aspx?stat=audi"; //Response.Redirect("AssignAudioFilesToQA.aspx?stat=audi"); } else { string popupScript = "";
ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), popupScript, false);
}
}
protected void gvAudioFiles_PageIndexChanging1(object sender, GridViewPageEventArgs e)
{

gvAudioFiles.PageIndex = e.NewPageIndex;
bindGridView("", "");


}
protected void gvAudioFiles_Sorting(object sender, GridViewSortEventArgs e)
{
bindGridView(e.SortExpression, sortOrder);


}
public string sortOrder
{
get
{
if (ViewState["sortOrder"].ToString() == "desc")
ViewState["sortOrder"] = "asc";
else
ViewState["sortOrder"] = "desc";
return ViewState["sortOrder"].ToString();
}
set
{
ViewState["sortOrder"] = value;
}
}
protected void btnDownload_Click(object sender, EventArgs e)
{
ArrayList filelist = new ArrayList();
string fileLoc = "";
foreach (GridViewRow gr in gvAudioFiles.Rows)
{
CheckBox chkSelect = (CheckBox)gr.FindControl("chkSelect");

if (chkSelect.Checked)
{
HyperLink hlnkAudio = (HyperLink)gr.FindControl("hlnkAudio");
fileLoc = fileLoc + hlnkAudio.NavigateUrl + ",";
filelist.Add(hlnkAudio.NavigateUrl);
//SqlParameter S1=new SqlParameter(
}
}
if (filelist.Count != 0)
{
fileLoc = fileLoc.TrimEnd(',');

string zipFullPath = Server.MapPath("~\\") + "DocumentFolder.zip";

ZipOutputStream zipOut = new ZipOutputStream(File.Create(zipFullPath));
//string[] f = { "D:\\Projects\\STREAMLINEMT\\StreamMT\\AudioFiles\\linder\\Friday, November 27, 2009\\162-13742409.VW", "D:\\Projects\\STREAMLINEMT\\StreamMT\\AudioFiles\\linder\\Friday, November 27, 2009\\13742409.Wav.wav" };

for (int i = 0; i < fi =" new" entry =" new" sreader =" File.OpenRead(Server.MapPath(filelist[i].ToString()));" buff =" new" datetime =" fi.LastWriteTime;" size =" sReader.Length;" postbackurl = "AssignAudioFilesToQA.aspx?stat=audi" stat="audi" targetf =" new" contenttype = "application/zip" filename=" + targetf.Name); FileStream sourceFile = new FileStream(@fapth, FileMode.Open); long FileSize; FileSize = sourceFile.Length; byte[] getContent = new byte[(int)FileSize]; sourceFile.Read(getContent, 0, (int)sourceFile.Length); sourceFile.Close(); Response.BinaryWrite(getContent); } private void exporttoGrid() { Response.Clear(); Response.AddHeader(" filename="FileName.xls" charset = "" contenttype = "application/vnd.xls" stringwrite =" new" htmlwrite =" new" ld =" GetDSSDuration(" popupscript = "