add Client Script to Datagrid cell

A

Art S.

If I have a datagrid cell that contain say a webform DropDownList x
how do I add a client's "onclick" attribute to it?

I can't just say x.Attributes.Add("onclick", "alert(this.value)");
because the datagrid is not bind yet.
 
K

Ken Cox [Microsoft MVP]

Hi Art,

You want to catch the ItemCreated event of the datagrid. That's when a row is
being built. At that time, you can look into the row for the control you're
after and start changing its attributes. The following code looks for a button
and adds JavaScript to it:

Private Sub DataGrid1_ItemCreated _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemCreated
Dim btnAdd As New Button
If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then
btnAdd = e.Item.FindControl("Button1")
btnAdd.Attributes.Add("onclick", "alert(this.id)")
End If
End Sub

Ken

--

Help stop SWEN in the newsgroups - Get the W32.Swen.A@mm Removal Tool:
http://securityresponse.symantec.com/avcenter/venc/data/[email protected]


If I have a datagrid cell that contain say a webform DropDownList x
how do I add a client's "onclick" attribute to it?

I can't just say x.Attributes.Add("onclick", "alert(this.value)");
because the datagrid is not bind yet.
 

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

Forum statistics

Threads
474,139
Messages
2,570,806
Members
47,352
Latest member
Maricruz09

Latest Threads

Top