Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Controls / January 2006

Tip: Looking for answers? Try searching our database.

javascript within TEMPLATECOLUMNS

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian Lorraine - 24 Jan 2006 01:58 GMT
I've learned how to add javascript to a web control in .NET using the
".Attributes.Add" method.

example:
MYBUTTON.Attributes.add("onClick", "blablafunction")

I'm having a problem adding javascript to a control that is inside a
TEMPLATE COLUMN of a datagrid. The name of my button is "button1". Because it
is in a template column, you can't just type:

button1.attributes.add........

You also can't go this route

DataGrid.FindControl("button1").Attributes.Add.... because the control item
doesn't have an ATTRIBUTES property.

I tried casting it:

Dim testbutton As Button
testbutton = CType(DataGrid.FindControl("button1"), Button)

But testbutton still doesn't have the ATTRIBUTES property.

I tried having a javascript function fire off when the page loads which
which would add some javascript to the button:

document.getElementById("BUTTONID").onclick = function() { blablabla}

This works when a button has a STATIC ID, but if a control is INSIDE a
templatecolumn, .NET will actually give the ID of this control something
completely different everytime you run it depending upon how much information
is in the datagrid.

I've spent all day trying to figure this out, digging throughMSDN articles,
newsgroups, but can't find anywhere else to look. The only thing I can think
of that SHOULD work is to cast the control to a button, but for some reason
this isn't working. Does anyone know a way I can convert this control using
vb.net which would expose the .attributes tag so I can finally add an onclick
event to this buton?

Any help or links appreciated

A desperate,
B
Sergey Poberezovskiy - 24 Jan 2006 03:40 GMT
In the DataGrid.ItemDataBound event you can write something similar to the
following:

Dim yourButtonColumnOrdinal As Integer = 1 ' or whatever it is
Dim yourButtonControlOrdinal As Integer = 0 ' or whatever it is
Dim item As DataGridItem = e.Item
Select Case item.Type
 Case ListItemType.AlternatingItem, ListItemType.Item,
ListItemType.SelectedItem
   With
CType(item.Cells(yourButtonColumnOrdinal).Controls(yourButtonControlOrdinal),
Button)
    .Attributes.Add("onclick", "myJavascript")
   End With
End Select

If you use HtmlButton, then change the cast to that. And you are right,
genetic Control does not have Attributes collection, but WebControl and
HtmlControl do.

HTH

> I've learned how to add javascript to a web control in .NET using the
> ".Attributes.Add" method.
[quoted text clipped - 41 lines]
> A desperate,
> B

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.