Corno said:
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

anel ID="MainPanel" Runat="server"></asp

anel>
<br /><br />
<asp:Label ID="LabelThatProofs" Runat="server">nothing</asp:Label>
</div>
</form>
</body>
</html>
//Rutger