Set TableCell onclick event dynamically

D

Diane Yocom

I'm very new to ASP.Net and probably jumped in a little over my head, but...

I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where there are tabs at the top with "submenu" buttons showing below the selected tab. The data that defines the tabs and submenus is stored in an XML file and I'm using nested repeaters to build them dynamically.

I've got it working pretty well, except for the submenu buttons. I've used a asp:hyperlink inside a table cell, which works fine as long as the user clicks *exactly* on the hyperlink text. I want the user to be able to click anywhere in the parent tablecell, though, so I thought I'd change it so the page change is activated from the onclick event of the parent tablecell. Unfortunately, since this is all built dynamically, I don't know the name of the page to jump to until runtime, so I thought I'd set the onclick event of the cell in the code behind.

I started looking at the AddHandler method, but realized that the asp:tablecell element doesn't have an onclick event to add a handler too, so I'm at a loss.

So, is there some way that I can dynamically set an asp:tablecell to change pages when clicked? I've included my code and HTML below (sorry for the formatting). I'm trying to do this in the rptrButtons_itemdatabound method, which is at the very bottom here.

TIA,
Diane


HTML:

<asp:Repeater ID="rptrTabHeader" Runat="server" OnItemDataBound="rptrTabHeader_itemdatabound">
<HeaderTemplate>
<table border="0" width="100%" cellspacing="0" cellpadding="0" class="NoPrint">
<tr>
<td width="100%">
<center>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr valign="bottom">
<td colspan="3" align="center">
<table align="center" border="0" cellpadding="0" cellspacing="0">
<tr valign="bottom">

</HeaderTemplate>
<ItemTemplate>
<td Runat="server">
<asp:HyperLink Runat="server" id="hlnkTab">
<asp:Image ID="imgTab" Runat="server" CssClass="TabHeader"></asp:Image>
</asp:HyperLink>
</td>
</ItemTemplate>
<FooterTemplate>
</tr>
</table>
</td>
</tr>
</table>
</center>
</td>
</tr>
<asp:Repeater ID="rptrButtons" Runat="server" DataSource=<%#GetButtons()%> OnItemDataBound="rptrButtons_itemdatabound">
<HeaderTemplate>
<tr height="28px" align="center" >
<td width="100%">
<table style="text-align: left" cellspacing="0" cellpadding="0" >
<tr>
</HeaderTemplate>
<ItemTemplate>
<td width="85px" class="TabButton" align="center">
<asp:Table Runat="server" CellPadding="8" CellSpacing="0">
<asp:TableRow Runat="server">
<asp:TableCell Runat="server" ID="tdSubMenu">
<asp:HyperLink Runat="server" ID="hlnkButton"></asp:HyperLink>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</td>
</ItemTemplate>
<FooterTemplate>
<td style="background-color: <%=MenuColor%>" width="100%">&nbsp;</td>
</tr></table></td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>

</FooterTemplate>
</asp:Repeater>


<script language="javascript">
<!--
function JumpTo(strURL){
// if (CheckChange()==true) {
// return (false);
// }
window.navigate(strURL);
return(true);
}



//-->
</script>


Code Behind:
Imports System.Xml

Namespace HSDYocom

Public Class Header

Inherits System.Web.UI.UserControl

Protected WithEvents rptrTabHeader As System.Web.UI.WebControls.Repeater

Private m_lngCurrentTab As Integer

Private m_lngCurrentButton As Integer

Private m_strMenuColor As String

Private m_strSelColor As String

Public Property CurrentTab() As Integer

Get

CurrentTab = m_lngCurrentTab

End Get

Set(ByVal Value As Integer)

m_lngCurrentTab = Value

End Set

End Property

Public Property CurrentButton() As Integer

Get

CurrentButton = m_lngCurrentButton

End Get

Set(ByVal Value As Integer)

m_lngCurrentButton = Value

End Set

End Property

Protected Property MenuColor() As String

Get

MenuColor = m_strMenuColor

End Get

Set(ByVal Value As String)

End Set

End Property

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

Dim dsTabs = New DataSet

If m_lngCurrentTab = 0 Then m_lngCurrentTab = 1

If m_lngCurrentButton = 0 Then m_lngCurrentButton = 1

If Not Page.IsPostBack Then

dsTabs.ReadXml(MapPath("TabImages.xml"))

rptrTabHeader.DataSource = dsTabs

rptrTabHeader.DataBind()

End If

End Sub



Protected Sub rptrTabHeader_itemdatabound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)

If Not IsNothing(e.Item.DataItem) Then

Dim myRow As DataRowView = CType(e.Item.DataItem, DataRowView)

Dim imgCurrent As System.Web.UI.WebControls.Image = CType(e.Item.FindControl("imgTab"), System.Web.UI.WebControls.Image)

Dim lnkCurrent As HyperLink = CType(e.Item.FindControl("hlnkTab"), HyperLink)

'Grab the value we want to select from the current row of data in our DataGrid/List/Repeater

Dim intTabIndex As Integer = CType(e.Item.DataItem("tabindex"), Integer)

If intTabIndex = m_lngCurrentTab Then

imgCurrent.ImageUrl = e.Item.DataItem("onimageurl")

m_strMenuColor = e.Item.DataItem("oncolor")

m_strSelColor = e.Item.DataItem("selcolor")

Else

imgCurrent.ImageUrl = e.Item.DataItem("offimageurl")

End If

lnkCurrent.NavigateUrl = "javascript:JumpTo('" & e.Item.DataItem("jumpurl") & "')"



End If

End Sub

Public Function GetButtons() As DataRow()

Dim dsTabs As New DataSet

dsTabs.ReadXml(Server.MapPath("TabImages.xml"))

Dim drTabs As DataRow() = dsTabs.Tables("tab").Select("tabindex=" & m_lngCurrentTab)

Dim drSubItems As DataRow()

Dim drSubItem As DataRow()

If drTabs.GetUpperBound(0) >= 0 Then

drSubItems = drTabs(0).GetChildRows("tab_subitems")

End If

If drSubItems.GetUpperBound(0) >= 0 Then

drSubItem = drSubItems(0).GetChildRows("subitems_subitem")

End If



Return drSubItem



End Function

Protected Sub rptrButtons_itemdatabound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)

If Not IsNothing(e.Item.DataItem) Then

Dim strButtonColor As String

Dim lnkCurrent As HyperLink = CType(e.Item.FindControl("hlnkButton"), HyperLink)

Dim intButtonIndex As Integer = CType(e.Item.DataItem("buttonindex"), Integer)

Dim tdButton As TableCell = CType(e.Item.FindControl("tdSubMenu"), TableCell)

'Set up the link and text for the button anchor

lnkCurrent.NavigateUrl = "javascript:JumpTo('" & e.Item.DataItem("buttonurl") & "')"

lnkCurrent.Text() = e.Item.DataItem("buttonname")

'Determine the color of the button

strButtonColor = m_strMenuColor

If m_lngCurrentButton = intButtonIndex Then

strButtonColor = m_strSelColor

lnkCurrent.CssClass = "NavigationLinkOn"

Else

lnkCurrent.CssClass = "NavigationLink"

End If

tdButton.BackColor = Color.FromName(strButtonColor)

End If

End Sub

End Class

End Namespace
 

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

Top