How to code JavaScript events from HyperLinkColumn

R

Rob Blackmore

Hi,

I have defined a simple grid with a hyperlink column as follows:

<asp:HyperLinkColumn HeaderText="Description"
DataNavigateUrlField="BillTypeID"
DataNavigateUrlFormatString="BillTypeDetail.aspx?BillTypeID={0}"
DataTextFormatString="{0:c}"
DataTextField="Description"></asp:HyperLinkColumn>

Which shows the Description and navigates to the detail page by
passing the id. Now, I want to tidy up the GUI by displaying a
friendly message in the status bar which I can traditionally do by
coding the onmouseover event in asp. However, please can someone let
me know how I can gain access to all of the JavaScript events in
ASP.Net when using a HypeLinkColumn control?


Thanks,


Rob Blackmore

mailto:[email protected]
 
K

Ken Cox [Microsoft MVP]

Hi Rob,

You can get a reference to the datagrid row during the ItemCreated event and
then drill down to get a reference to the hyperlink. After that, you just
need to add in some JavaScript.

Private Sub DataGrid1_ItemCreated _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemCreated
Dim hlnk As HyperLink
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
hlnk = e.Item.Cells(0).Controls(0)
hlnk.Attributes.Add("onmouseover", _
"alert('hello');")
End If
End Sub

Full code below. Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub


Private Sub DataGrid1_ItemCreated _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemCreated
Dim hlnk As HyperLink
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
hlnk = e.Item.Cells(0).Controls(0)
hlnk.Attributes.Add("onmouseover", _
"alert('hello');")
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("SS", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("LastName", GetType(String)))
dt.Columns.Add(New DataColumn _
("FirstName", GetType(String)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i + i
dr(1) = "LastName " + i.ToString()
dr(2) = "FirstName " + i.ToString()
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
 
K

Ken Cox [Microsoft MVP]

Oops forgot the HTML markup:


<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:HyperLinkColumn HeaderText="Description" DataNavigateUrlField="SS"
DataNavigateUrlFormatString="BillTypeDetail.aspx?BillTypeID={0}"
DataTextFormatString="{0:c}"
DataTextField="Lastname"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="SS" ReadOnly="True"
HeaderText="SS"></asp:BoundColumn>
<asp:BoundColumn DataField="Lastname" ReadOnly="True" HeaderText="Last
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="First
Name"></asp:BoundColumn>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"
CommandName="Select"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top