Wednesday 28 December 2011

Download PDF file using code


string str_FilePath = Server.MapPath("UploadedFiles/test.pdf");
System.IO.StreamReader sr = new System.IO.StreamReader(str_FilePath);
Response.BufferOutput = true;
Response.AppendHeader("content-disposition", "attachment; filename=test.pdf");
Response.ContentType = "application/pdf";
Response.TransmitFile(str_FilePath);

Copy and paste the above code in button_click event. When you will click the button test.pdf will be downloaded.

Assign QueryString Value to Control in Design Page

<a href="Default.aspx?RollNo=<%=HttpUtility.HtmlEncode(Request.QueryString["RN"]) %>&Cl=<%=HttpUtility.HtmlEncode(Request.QueryString["Cl"]) %>">Click Here</a>

Saturday 24 December 2011

Check Floating point value using JS


<asp:TextBox ID="txtYearValue" runat="server" onkeyup=" isNFloat (event)"></asp:TextBox>

function isNFloat(e)
{
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
var keyCode = (isNN) ? e.which : e.keyCode;
//alert(keyCode);
if (isNN)
{
if (keyCode == 0)
return true;
}
if((keyCode>47&&keyCode<58)||(keyCode==8)||(keyCode==9)||(keyCode==110)||(keyCode==46))
{
return true;
}
else
{
if (e.returnValue)
{
e.returnValue = false;
return false;
}
else if (e.preventDefault)
{
e.preventDefault();
return false;
}
this.event.returnValue = false;
return false;          
}
}

Saturday 17 December 2011

Check DateTime format using JS


The below function checks date in mm-dd-yyyy format.


<asp:TextBox ID="txtYearValue" runat="server" onBlur="checkdate(this)"></asp:TextBox>

function checkdate(input)
{
var validformat=/^\d{2}\-\d{2}\-\d{4}$/ //Basic check for format validity
var returnval=false
if (!validformat.test(input.value))
alert("Invalid Date Format. Please correct and submit again.")
else{ //Detailed check for valid date ranges
var monthfield=input.value.split("-")[0]
var dayfield=input.value.split("-")[1]
var yearfield=input.value.split("-")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)
if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
else
returnval=true
}
if (returnval==false)
      input.select()
return returnval
}

Saturday 3 December 2011

Response.Redirect Open Page In New Tab


I am going to explain how to open a page in a new tab/window on Button click in asp.net. Suppose the page contains a button called "btnTest" and your requirement is open a specified page in the new tab on the button click then do the following.

<asp:Button ID=”btnTest” runat=”Server” Text=”Test” OnClick=”btnTest_Click”
OnClientClick ="document.forms[0].target = '_blank';"/>

protected void btnTest_Click(object sender, EventArgs e)
{
      Response.Redirect(”New.aspx”);
}

OR

If there is some code to execute on button click and after that you want to open the page in new tab then do the following :

protected void btnTest_Click(object sender, EventArgs e)
{
      .
      .         // Your Code Here
      .
      Response.Write( “<script> window.open( ‘New.aspx’,'_blank' ); </script>”);
      Response.End();
}

OR

If you want to open the page in a new tab as well as pass some values to the specified page using querystring then do the following :

protected void btnTest_Click(object sender, EventArgs e)
{
      .
      .
      .
      string pageurl="Default.aspx?Id=6&RollNo=15";

      Response.Write( “<script> window.open( ‘"+pageurl+"’,'_blank' ); </script>”);
      Response.End();
}