473,385 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Set TableCell onclick event dynamically

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
Nov 18 '05 #1
0 3123

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
4
by: RobG | last post by:
I have a function whose parameter is a reference the element that called it: function someFunction(el) { ... } The function is assigned to the onclick event of some elements in the HTML...
6
by: hb | last post by:
Hi, I have a page bill.aspx and its code-behind bill.aspx.cs. On bill.aspx I have: === Select a month: <asp:dropdownlist runat="server" id="lstDate" autopostback="True" /> <br> <asp:table...
0
by: Diane Yocom | last post by:
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...
9
by: RA | last post by:
While dynamically creating the table; I am adding a hyperlink as a TableCell where text is "Delete". Initially it is disabled and if a checkbox is selected from a table then it gets enabled. Even...
1
by: Nathan Sokalski | last post by:
I have navigation that is made out of a table. I did this so that I could dynamically create my "buttons" (the cells formatted using CSS) and avoid creating a new graphic every time I changed the...
11
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
11
by: Daz | last post by:
Hello everyone. I am sure the answer to my question is simple, but I can't seem to dynamically add an onClick event to my script. I have a table which is generated dynamically, I am just...
3
by: Michael_R_Banks | last post by:
I'm trying to dynamically build a table that allows users to remove rows when they click a corresponding button. For some reason, whenever I add the button to the table, it never fires the onclick...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.