Live Chat Icon For mobile
Live Chat Icon

How can I have an onclick event in the DataGrid for any Column

Platform: ASP.NET| Category: DataGrid

<asp:DataGrid id='DataGrid1' OnItemDataBound ='ItemDB' DataKeyField ='ProductId' runat='server'></asp:DataGrid>

VB.NET


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        	’Put user code to initialize the page here
        	If Not Page.IsPostBack Then
            		’Bind the dataGrid to DataView
        	End If
End Sub

Protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
        	Dim dv As DataView = DataGrid1.DataSource
        	Dim dcCol As DataColumn
        	Dim dc As DataColumnCollection = dv.Table.Columns
        	Dim strID As String
        	For Each dcCol In dv.Table.Columns
            		If e.Item.ItemType = ListItemType.AlternatingItem Or _
            			e.Item.ItemType = ListItemType.Item Then
	                	strID = DataGrid1.DataKeys(e.Item.ItemIndex)
	                	e.Item.Cells(dc.IndexOf(dc(dcCol.ColumnName))).Attributes.Add('style', 'cursor:hand')
                		e.Item.Cells(dc.IndexOf(dc(dcCol.ColumnName))).Attributes.Add('onclick', _
                      		'javascript:window.open(’details.aspx?id=' & strID & '’,' _
                      		& '’MyPage’,’height=300,width=300’)')
            		End If
        	Next
End Sub

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	if(!Page.IsPostBack )
	{
		//Bind Datagrid to DataView
	}
}

protected void ItemDB(object  sender  ,   System.Web.UI.WebControls.DataGridItemEventArgs e )
{
	DataView dv = (DataView)DataGrid1.DataSource;
	DataColumnCollection dc = dv.Table.Columns ;
	string strID;
	foreach (DataColumn dcCol  in dv.Table.Columns)
	{
		if ((e.Item.ItemType == ListItemType.AlternatingItem )||(e.Item.ItemType == ListItemType.Item ))
		{
			strID =  DataGrid1.DataKeys[e.Item.ItemIndex].ToString ();
			e.Item.Cells[dc.IndexOf(dc[dcCol.ColumnName])].Attributes.Add('style', 'cursor:hand');
			e.Item.Cells[dc.IndexOf(dc[dcCol.ColumnName])].Attributes.Add('onclick',  'javascript:window.open(’details.aspx?id=' + strID + '’,' + '’MyPage’,’height=300,width=300’)');
		}
	}
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.