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