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");