Saturday, February 8, 2014

pass parameter to javascript function

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
 <script type = "text/javascript">
     function GetSelectedRows(lnk) {
         alert(lnk);
     }
 </script>
 </head>
 <body>
 <form id="form1" runat="server">
 <div>
 <asp:ScriptManager ID="Sc1" runat="server"></asp:ScriptManager>
 <asp:UpdatePanel ID="up1" runat="server">
 <ContentTemplate>
 
 
<asp:GridView ID="gvMain" runat="server" AutoGenerateColumns="false" >
 <Columns>
 <asp:TemplateField HeaderText="ID">
 <ItemTemplate>
 <asp:Label ID="lblID" runat="server" Text='<%#Eval("CustomerID") %>' Visible="true"></asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
<asp:TemplateField HeaderText="Name">
 <ItemTemplate>
 <asp:Label ID="lblName" runat="server" Text='<%#Eval("CustomerName") %>'></asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Dob">
 <ItemTemplate>
 <asp:Label ID="lblDOB" runat="server" Text='<%# Eval("City") %>'></asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
<asp:TemplateField>
 <ItemTemplate>
 <input type="button" runat="server" id="Button2" value="Edit" onclick='<%# "javascript:GetSelectedRows(\"" + Eval("CustomerID" ) + "\" );" %>'/>
 </ItemTemplate>
 </asp:TemplateField>
</Columns>
 </asp:GridView>
 </ContentTemplate>
 </asp:UpdatePanel>
 </div>
 </form>
 </body>
 </html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/Customers.xml"));
        gvMain.DataSource = ds;
        gvMain.DataBind();
    }
}


Customers.xml
----------------
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table>
    
    <CustomerID>101</CustomerID>
    <CustomerName>ALFKI1</CustomerName>
    <City>Boise</City>
    <PostalCode>12209</PostalCode>
  
</Table>
  <Table>
    <CustomerID>102</CustomerID>
    <CustomerName>ALFKI2</CustomerName>
    <City>México D.F.</City>
    <PostalCode>05021</PostalCode>
  </Table>
  <Table>
    <CustomerID>103</CustomerID>
    <CustomerName>ALFKI3</CustomerName>
    <City>Montréal</City>
    <PostalCode>05023</PostalCode>
  </Table>
  <Table>
    <CustomerID>104</CustomerID>
    <CustomerName>ALFKI4</CustomerName>
    <City>Mannheim</City>
    <PostalCode>WA1 1DP</PostalCode>
  </Table>
  <Table>
    <CustomerID>105</CustomerID>
    <CustomerName>ALFKI5</CustomerName>
    <City>Luleå</City>
    <PostalCode>S-958 22</PostalCode>
  </Table>
  <Table>
    <CustomerID>106</CustomerID>
    <CustomerName>ALFKI6</CustomerName>
    <City>Mannheim</City>
    <PostalCode>68306</PostalCode>
  </Table>
  <Table>
    <CustomerID>107</CustomerID>
    <CustomerName>ALFKI7</CustomerName>
    <City>Strasbourg</City>
    <PostalCode>67000</PostalCode>
  </Table>
  <Table>
    <CustomerID>108</CustomerID>
    <CustomerName>ALFKI8</CustomerName>
    <City>Madrid</City>
    <PostalCode>28023</PostalCode>
  </Table>
  <Table>
    <CustomerID>109</CustomerID>
    <CustomerName>ALFKI9</CustomerName>
    <City>Marseille</City>
    <PostalCode>13008</PostalCode>
  </Table>
  <Table>
    <CustomerID>110</CustomerID>
    <CustomerName>ALFKI10</CustomerName>
    <City>Tsawassen</City>
    <PostalCode>T2F 8M4</PostalCode>
  </Table>
 
</NewDataSet>

Document 1-0

0 comments:

Post a Comment