30 December 2008

Time automatically in a web page using javascript

DisplayDate();
setInterval("DisplayDate()",1000);
function DisplayDate()
{
var dt = new Date();
document.getElementById("tdDate").innerHTML=dt.toLocaleString();
}

04 December 2008

Export GridView Data To Excel

private void ExportDynamicReportToExcel()
{
string attachment = "attachment; filename=File.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
GridView1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(GridView1);
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

DateTime.ToString() Patterns

MM/dd/yyyy 08/22/2006
dddd, dd MMMM yyyy Tuesday, 22 August 2006
dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
dddd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM
dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
MM/dd/yyyy HH:mm 08/22/2006 06:30
MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
MM/dd/yyyy H:mm 08/22/2006 6:30
MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07
MMMM dd August 22
yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2006-08-22T06:30:07.7199222-04:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2006-08-22T06:30:07.7199222-04:00
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Tue, 22 Aug 2006 06:30:07 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Tue, 22 Aug 2006 06:30:07 GMT
yyyy'-'MM'-'dd'T'HH':'mm':'ss 2006-08-22T06:30:07
HH:mm 06:30
hh:mm tt 06:30 AM
H:mm 6:30
h:mm tt 6:30 AM
HH:mm:ss 06:30:07
yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2006-08-22 06:30:07Z
dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
yyyy MMMM 2006 August
MM/dd/yyyy 08/22/2006
dddd, dd MMMM yyyy Tuesday, 22 August 2006
dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
dddd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM
dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
MM/dd/yyyy HH:mm 08/22/2006 06:30
MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
MM/dd/yyyy H:mm 08/22/2006 6:30
MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Tue, 22 Aug 2006 06:30:07 GMT
yyyy'-'MM'-'dd'T'HH':'mm':'ss 2006-08-22T06:30:07
yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2006-08-22 06:30:07Z
dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07

Eg:
DateTime dt = DateTime.Now;
lblAppDate.Text = dt.ToString("dd MMM yyyy hh:mm:s tt");

27 November 2008

How to open a pdf Or image file in a new browser window

In the Default.aspx page, in a button click or any other events
----------------------------------------------------------------

ScriptManager.RegisterStartupScript(this, this.GetType(), "s", "window.open('Default2.aspx?FileName=PO0005_R.pdf',null,'height=500, width=500,status= no,resizable= no, scrollbars=no,toolbar=no,menubar=no');", true);

In the Defult2.aspx page
-------------------------

using System.Net;

protected void Page_Load(object sender, EventArgs e)
{
ReadPdfFile(Request.QueryString["FileName"].ToString());
}
private void ReadPdfFile(string FileName)
{
string path = Server.MapPath("PO0005_R.pdf");
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);

if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}

}

18 November 2008

How to get datakeyNames ( Gridview )

GridViewRow row = ((GridViewRow)((Control)e.CommandSource).Parent.Parent);

lblPopGRNNo.Text = grvGRNList.DataKeys[row.RowIndex]["GrnNumber"].ToString();

lblGRNDate.Text = (Convert.ToDateTime(grvGRNList.DataKeys[row.RowIndex]["GrnDate"])).ToString("dd MMM yyyy");

lblSupplierName.Text = grvGRNList.DataKeys[row.RowIndex]["SupplierName"].ToString();

lblSuratJalanNumber.Text = grvGRNList.DataKeys[row.RowIndex]["SuratJalanNumber"].ToString();

06 November 2008

System colors to dropdownlist

using System.Drawing;

string[] names = System.Enum.GetNames(typeof(System.Drawing.KnownColor));
Array values = System.Enum.GetValues(typeof(System.Drawing.KnownColor));
for (int i = 27; i <= names.Length - 8; i++)
{
ListItem item = new ListItem(names[i], values.GetValue(i).ToString());
ddlColorPicker.Items.Add(item);
ddlColorPicker.Items[i - 26].Attributes.Add("style", "background-color :" + values.GetValue(i).ToString() + "");
}

04 November 2008

Filtering dataset

grvItem.DataSource = dataset1.Tables[0].DefaultView;
DataView myView = dataset1.Tables[0].DefaultView;
myView.RowFilter = "Name like '" + txtSearch.Text + "%'";
grvItem.DataSource = myView;
grvItem.DataBind();

30 October 2008

Change style of file upload button

<
asp:FileUpload id="File1"
runat="server"
style ="position: relative;text-align: right;-moz-opacity:0 ;
filter:alpha(opacity: 0);opacity: 0;z-index: 2;" />

<
asp:LinkButton ID="ImageButton1"
runat="server"
Text="Attach more"
style ="position: absolute;left: 166px; z-index: 1; " />

21 October 2008

Convert to byte

System.IO.Stream inputStream = FileUpload1.PostedFile.InputStream;
byte[] buffer = new byte[FileUpload1.PostedFile.ContentLength];
inputStream.Read(buffer, 0, FileUpload1.PostedFile.ContentLength);

16 October 2008

Select values from dataset

1 . Using Like
---------
foreach (DataRow dr in dsItem.Tables[0].Select("Name like '" + prefixText + "%'"))
{
items.Add(dr["Name"].ToString());
}
2. Using =
-----------
DataRow[] dRow = dsPurchaseRequestItems.Tables[0].Select("ItemId='" + hfItemId.Value + "'");
if (dRow.Length > 0)
{
dRow[0].Delete();
}
dsPurchaseRequestItems.AcceptChanges();

14 October 2008

Photo Upload

// This code used in Target form
StreamReader strReader = new StreamReader(upPhoto.PostedFile.InputStream);
Stream str = strReader.BaseStream;
BinaryReader brRead = new BinaryReader(str);
byte[] sImage = brRead.ReadBytes(Convert.ToInt32(str.Length));
//Save Function
int photoId = CommonData.SavePhoto(0, upPhoto.PostedFile.ContentType, sImage, "Item");
Image1.ImageUrl = "~/ShowImage.aspx?PhotoId=" + photoId;



//Write byte stream to another Form (ShowImage.aspx)

public int PhotoId;
protected void Page_Load(object sender, EventArgs e)
{
PhotoId = Convert.ToInt32(Request.QueryString["PhotoId"]);
// Used for retrive image from database
Photo objPhoto = CommonManager.GetPhoto(PhotoId);
Response.ContentType = objPhoto.ImageType;
Response.BinaryWrite(objPhoto.Image);
//byte[] Photo1 = (byte[])Session["Photo"];
//Response.ContentType = "image/jpeg";
//Response.BinaryWrite(Photo1);
}

06 October 2008

Split a text using javascript

function splitText()
{
var temp=document.getElementById('TextBox1').value.split(' ',3);
document.getElementById('TextBox1').value=temp[0];
document.getElementById('TextBox2').value=temp[1];
document.getElementById('TextBox3').value=temp[2];

document.getElementById('DropDownList1').value=temp[0];
document.getElementById('DropDownList2').value=temp[1];
document.getElementById('DropDownList3').value=temp[2];
}

05 October 2008

Change values in gridView Dynamically

protected void grvStore_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[8].Text == "True")
{
e.Row.Cells[8].Text = "Active";
}
else
{
e.Row.Cells[8].Text = "Inactive";
}
}
}

29 September 2008

Update UpdatePanel Using Javascript

var prm=Sys.WebForms.PageRequestManager.getInstance();
prm._doPostBack('ButtonName','');
or
prm._doPostBack('UpdatePanelName','');

Focus Back to that control -C#

ScriptManager sm = ScriptManager.GetCurrent(this);
sm.SetFocus(TextBox2);

Allow only numeric values in textbox-Javascript

function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 &&
(charCode < 48
Or
charCode > 57) )

return false;
return true;
}

----------------------------------------------
onkeypress="return isNumberKey(event);"

To Find No Of Days in a month -SQL

DECLARE @date datetime
set @date='1/1/2008'
DECLARE @dayCount int
--To Find No Of Days in a month
SET @dayCount=Day(DateAdd(Month, 1, @date) - Day(DateAdd(Month, 1, @date)))
print @daycount

Date Conversion

System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-GB");

String requestTempDate = (Convert.ToDateTime(txtRequestDate.Text, culture)).ToShortDateString();

DateTime requestDate = Convert.ToDateTime(DateTime.Now, culture);
---------------------------------------------------------------

txtRequestDate.Text = DateTime.Now.ToString("dd MMM yyyy");


DateTime Date = Convert.ToDateTime(DateTime.Now.ToShortDateString());

GridView-Row Identification In TemplateField ( TextBox )

//Getting gridview row in textchange of template field
protected void txtGrnQuantity_TextChanged(object sender, EventArgs e)
{
TextBox txtGrnQuantity = (TextBox)grvGRN.Rows[((GridViewRow)((TextBox)sender).Parent.Parent).RowIndex].FindControl("txtGrnQuantity");
TextBox txtRejectedQuantity = (TextBox)grvGRN.Rows[((GridViewRow)((TextBox)sender).Parent.Parent).RowIndex].FindControl("txtRejectedQuantity");
}

21 August 2008

Disable the back functionality of the browser

//disable the back functionality of the browser
window.history.forward(1);

04 August 2008

How to change a value in GridView

protected void grvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[8].Text == "True")
{
e.Row.Cells[8].Text = "Active";
}
else
{
e.Row.Cells[8].Text = "Inactive";
}
}
}

Allow only floating point values in textbox

function isNumberPointKey(evt)
{
//alert(event.keyCode);
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48
or
charCode > 57) && charCode!=46 )
return false;
return true;
}

onkeypress="return isNumberPointKey(event

Assigning a value to textbox when TextMode is Password

TextBox1.Text = "test";
TextBox1.Attributes.Add("value", "test");

29 July 2008

Identify GridView RowIndex

//When we Click a grdView Row, then row index stored in a hidden Field
function GetGrvPersonalRowIndex(GRVRowIndex)
{
document.getElementById('ctl00_ContentPlaceHolder1_tbcDailyReporting_TabPanel1_HFGridviewRowIndex').value=GRVRowIndex;
}
-------In server side of asp page----------
protected void grvPersonal_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "javascript:GetGrvPersonalRowIndex(" + e.Row.RowIndex + ")");
}
}

Allow two decimal palace in textBox

function AllowOnlyTwoDecimalPlace(txtBox)
{
var num = parseFloat(txtBox.value);
txtBox.value = num.toFixed(2);
if (txtBox.value == 'NaN'){
txtBox.value = '';}

}

Focus to the control

ScriptManager sm = ScriptManager.GetCurrent(this);
sm.SetFocus(TextBox1);

To Identify Gridview Row Index in Row command

GridViewRow row = ((GridViewRow)((Control)e.CommandSource).Parent.Parent);
DropDownList ddlTest= (DropDownList)row.Cells[2].FindControl("ddlTest");