473,385 Members | 1,326 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.

Code behind and onclick on html table row

Hi all,

I have a page with a table of which the rows have to be added dynamically.
What I also want is 'code behind' the onclick event of a row (the page
should be posted back to the server, which should handle the onclick).
However, as the rows are added dynamically, I cannot give them an ID at
designtime and attach a function to it. Is there a way to write one function
for all added rows but that is able to distinguish which of the rows were
clicked? Or is there another way to do this?

TIA,

Corno
Nov 18 '05 #1
2 12124
Corno wrote:
Hi all,

I have a page with a table of which the rows have to be added dynamically.
What I also want is 'code behind' the onclick event of a row (the page
should be posted back to the server, which should handle the onclick).
However, as the rows are added dynamically, I cannot give them an ID at
designtime and attach a function to it. Is there a way to write one function
for all added rows but that is able to distinguish which of the rows were
clicked? Or is there another way to do this?

TIA,

Corno

Allright, let me say this first: I do not suggest this way of working...
(I think) but the following example does work (on 2.0 beta 1).
<%@ Page Language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

void Page_Load(object sender, EventArgs e)
{
// Declare some table pieces, useful for building tables ;)
Table tbl = new Table();
TableRow tr = new TableRow();
TableCell td0 = new TableCell();
TableCell td1 = new TableCell();
TableCell td2 = new TableCell();

// LinkButton that will cause the actual postback
LinkButton LnkBtn = new LinkButton();
LnkBtn.Click +=new EventHandler(LnkBtn_Click);
LnkBtn.ID = "HiddenLink";

// Construct a table with the table elements
td0.Controls.Add(LnkBtn);

// just for proper displaying on the client
td0.Style.Add("display","none");

// Duhh..
td1.Text = "Foo";
td2.Text = "Bar";

tr.Cells.Add(td0);
tr.Cells.Add(td1);
tr.Cells.Add(td2);

// The trick, add a onclick attribute and you can track the
click as if the LinkButton was clicked.
tr.Attributes.Add("onclick", "__doPostBack('" + LnkBtn.ClientID
+ "','')");
//tr.Attributes.Add("onclick", "__doPostBack('','')");
tbl.Rows.Add(tr);

MainPanel.Controls.Add(tbl);
}

void LnkBtn_Click(object sender, EventArgs e)
{
// Touchdown!
LabelThatProofs.Text = "Touchdown!";
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="MainPanel" Runat="server"></asp:Panel>
<br /><br />
<asp:Label ID="LabelThatProofs" Runat="server">nothing</asp:Label>
</div>
</form>
</body>
</html>

//Rutger
Nov 18 '05 #2
Corno,

You will have to use a client-side "onclick" event. You can pass it a
parameter "this" which will refer to the clicked html table row.

Eliyahu

"Corno" <Corno@dds%FAKE%.nl> wrote in message
news:eK**************@TK2MSFTNGP12.phx.gbl...
Hi all,

I have a page with a table of which the rows have to be added dynamically.
What I also want is 'code behind' the onclick event of a row (the page
should be posted back to the server, which should handle the onclick).
However, as the rows are added dynamically, I cannot give them an ID at
designtime and attach a function to it. Is there a way to write one function for all added rows but that is able to distinguish which of the rows were
clicked? Or is there another way to do this?

TIA,

Corno

Nov 18 '05 #3

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

Similar topics

7
by: David. E. Goble | last post by:
Hi all; I have the following files; index.html, sigsheader.js, sigsboby.js, smilesbody.js and smiles.js. The sourse is below. The page displays two manual slide shows... Each slideshow has a set...
9
by: David. E. Goble | last post by:
Arrrh! some buttons work while others don't, but I can't see why. I have tried comparing the files that do work, with the ones that don't. But to no help. The funny thing is the parts that work...
5
by: Konrad | last post by:
Hi How to write in c# href code so it looks in source Slide - pokaz programu - Remonty firmy Delta Software pls. look source of this link. Thanks
4
by: Carlo Marchesoni | last post by:
I have a button that opens a new Window (some kind of search-window), which is fired using JavaScript (btnSearch.Attributes=".....";) Now I need to run some code behind code BEFORE this JavaScript...
5
by: Richard Dixson | last post by:
I created a new C# web application. Basically all I am trying to do is create a table that consists of a few rows with two columns in each. And then to set those columns to text values from my...
2
by: Ric | last post by:
im new to asp.net. from what i understand, you have the aspx file (presentation), user-control(ascx file), code-behind(vb file) and components(compiled vb and dll files). the aspx file contains a...
0
by: Edward | last post by:
I'm strictly a high-level programmer, so please excuse my ignorance of the inner magic of ASP etc. I have what my company calls a "builder form". It is used to maintain static lookup lists. It...
1
by: elavarasum | last post by:
Hi, Can any one help me. i have created one color pallet. if i click particular color, it will set as background-color of CardTable. I want to access the particular set in the CardTable from...
5
by: Mr. X. | last post by:
Hello. In Visual Studio 2008, there is on the toolbox -login -login, which create a login form. On that form there is a LoginButton, which click on it is evented on the "default.aspx.vb"...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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.