Monday, November 2, 2009

How to call Dynamic Javascript Function from Gridview - Asp.Net

We can call dynamic java script function from grid view as fallow.say we need to call following java script function dynamically from gride view then we need to create java script function in .aspx page.
<script type="text/jscript">
function funcShow(id)
{
aleart(id);
}
</script>
After that we have to add Template filed with Hyperlink filed to Grdeview as fallowing
<asp:GridView ID="dg1" runat="server"                      
<Columns>
<asp:BoundField DataField="FileName" HeaderText="Description">
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl='<%# RetrunClick(Eval("ID"))%>'>Click</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then we need to add following function to the code behind page that will call the java script function .
public string RetrunClick(object obj)
{
string str = string.Empty;
if (obj != null)
{
str = "javascript:funcShow('" + obj.ToString() + "')";
}
return str;
}
JavaScript Bible, Fifth EditionJavaScript Bible

Wednesday, October 14, 2009

Calculate Running Sum of the Texboxses in the ASP.NET with Javascript

If we need to get running total of the text boxes in asp.net page while allowing automatically calculate the sum when changed the any text box .
Ex:
we have 3 Text Boxes as "txtCallCharges","txtRentTaxies","txtTotalAmount" and we need to get sum of the entered values to the "txtTotalAmount" text box .and also It should be allowed to automatically get total when text changed in nay text box.
Solution:
Add following code to the Code behind page.
protected override void OnLoad(EventArgs e)
{
txtCallCharges.Attributes.Add("onblur", "javascript:calculateTotal();");
txtRentTaxies.Attributes.Add("onblur", "javascript:calculateTotal();");
}

Then add following javascript to .aspx page
function calculateTotal()
{
var ctrl1 = null;
var ctrl2=null;
var ctrl3=null;          
ctrl1= document.getElementById("<%=txtCallCharges.ClientID %>");
ctrl2= document.getElementById("<%=txtRentTaxies.ClientID %>");
ctrl3= document.getElementById("<%=txtTotalAmount.ClientID %>");
var total =null;
if(ctrl1.value != "")
{
total=total+parseFloat(ctrl1.value);
}
if(ctrl2.value != "")
{
total=total+parseFloat(ctrl2.value);
}
ctrl3.value= total;
}

Thursday, October 8, 2009

Update multiple columns of one table from another table- SQL

UPDATE [tableA]
SET
A1 = B.B1+B.B2,
A2=B.B3
FROM tableA, tableB as B WHERE tableA.ID = B.ID

Thursday, June 18, 2009

Refresh a content page without refreshing master page Vs Avoid flicker when click on the link

The master page class derives from the UserControl class. When the application is executing, the master page just like a child control. So we can say the master page is not a true page. “.And, when the page loads, we can notice the navigationURL of the Browser address bar is the content page's, but not the master page's.Because of that we cannot refresh a content page without refreshing master page .But we can avoid flickering with fallowing code by put one <HEAD> tag in the master page.

<meta http-equiv="Page-Enter" content="blendTrans(Duration=0)"/>

<meta http-equiv="Page-Exit" content="blendTrans(Duration=0)"/>

Friday, June 5, 2009

ASP.NET Master page Set Background Image to Table

Problem :When we set ASP Net - Master Page background image it only visible from pages in root directory
Solution : The images which are used in Masterpage should be set in the code behind file as fallow .
The runat='Server" attribute should be added to the controller which are going to set background image and ID should be set to that controller.
<td runat="server" id="tb1">set back ground image with code</td>

In the code behind page image should be set using ResolveClientUrl() method
protected void Page_Load(object sender, EventArgs e)
{
tb1.Style[HtmlTextWriterStyle.BackgroundImage] = ResolveClientUrl("~/App_Themes/Images/1-sri-lanka.JPG");
}


Tuesday, June 2, 2009

Sql Date Time Formatting

Remove Time Part From DateTime
---------------------------------
SELECT GETDATE() /// 2009-06-25 12:03:56.640
Declare @mydate DATETIME
SET @mydate=GETDATE()

-------------------------------------

1.Get Out put as Type of DateTime with zero time
SELECT DATEADD(day, DATEDIFF(day, '20000101',@mydate), '20000101')
or
SELECT DATEADD(DAY, 0, DATEDIFF(DAY,0,@mydate))
or
SELECT DATEADD(dd, DATEDIFF(dd, 0, @mydate), 0)

out put :2009-06-25 00:00:00.000

2.Get Out put as Type of Varchar without time

SELECT CONVERT(VARCHAR(10),@mydate, 103)
out put :25/06/2009

Or
SELECT CONVERT(VARCHAR(10), @mydate, 120)
out put :2009-06-25

Sunday, May 10, 2009

Unique Identifier In .Net & SQL

A GUID (Globally Unique Identifiers) is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated. In the .NET framework the System.Guid.NewGuid method is used to generate the unique identifiers.

System.Guid desiredGuid = System.Guid.NewGuid();

This desiredGuid can be directly save to DB. The data type of the filed should be “uniqueidentifier” or we can cast it as string …. And also we can store GUID in “System.Guid” data type variable(C#).

convert GUID to Sting

System.Guid desiredGuid = System.Guid.NewGuid();
string a=desiredGuid.ToString();

convert a string to a GUID

Guid MyGuid = new Guid(stringValue);

In SQL Server, the “uniqueidentifier” data type is used to store the GUIDs and newid() function will generate GUID.

Thursday, March 19, 2009

TypeForwardedTo (.NET)

We can use this class to move type from one assembly to another while not disrupting the callers compile against the original assembly .
Example : we have a source library (say lib.dll) which included lot of classes and we have use that class library for our application development. After some period of time the we refastening the source library (lib.dll) and it splits in to two source library to (say lib.dll and lib1.dll) .Then what happen to our application will it work? No because our application point to the lib.dll but some of the classes have move to lib1.dll to. So our application will generate error .To prevent this we can use “TypeForwardedTo”

Following 2 classes define in lib.dll
using System;
using System.Collections.Generic;
using System.Text;
namespace lib
{
public class car
{
public static void Do()
{
System.Console.WriteLine("car");

}
}
public class van
{
public static void Do()
{
System.Console.WriteLine("van");
}
}
}

Then the develop application using above lib.dll as following

using System;
using System.Collections.Generic;
using System.Text;
using lib;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
car.Do();
van.Do();
Console.ReadLine();

}
}
}

The Out put is :
car
van


Now we split above lib.dll to 2 dll to as following

lib.dll
using System;
using System.Collections.Generic;
using System.Text;
namespace lib
{
public class car
{
public static void Do()
{
System.Console.WriteLine("car");

}
}
}

lib1.dll
using System;
using System.Collections.Generic;
using System.Text;
namespace lib1
{
public class van
{
public static void Do()
{
System.Console.WriteLine("van");

}
}
}

Then replace the above lib.dll with new two dlls. Now what happen, the van class can’t find in new lib.dll so it generate and error.
So prevent such a error we ca use
“System.Runtime.CompilerServices.TypeForwardedTo” as following
Open the lib project and Open the AssemblyInfo.cs file and add the following line right after the assembly directives.

[assembly: TypeForwardedTo(typeof(lib1.van))]


now build projecj.
Now we have 2dlls, lib.dll and lib1.dll but we can still run oure previouse application without any error .....

Wednesday, February 11, 2009

SQL Select First and Last 100 Record

Select first 100 Records
SELECT RecordId
FROM Table
LIMIT 100


Select Last 100 Records
SELECT RecordId
FROM Table
ORDER BY RecordId DESC
LIMIT 100