Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Code behind and onclick on html table row

Reply
Thread Tools

Code behind and onclick on html table row

 
 
Corno
Guest
Posts: n/a
 
      09-03-2004
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


 
Reply With Quote
 
 
 
 
Rutger Smit
Guest
Posts: n/a
 
      09-03-2004
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>
<aspanel ID="MainPanel" Runat="server"></aspanel>
<br /><br />
<asp:Label ID="LabelThatProofs" Runat="server">nothing</asp:Label>
</div>
</form>
</body>
</html>



//Rutger
 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      09-05-2004
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:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Non-code behind to code behind John ASP .Net 2 02-19-2007 07:08 PM
row bottom border in inner table not matching up with row border in outer table phl HTML 1 06-08-2006 03:43 PM
ok I can do a totals row but how about a percentage row after each data row D ASP .Net Datagrid Control 0 05-23-2005 04:10 PM
Code behind and onclick on html table row Corno ASP .Net Web Controls 1 09-05-2004 09:19 AM
Re: Code Behind vs. no code behind: error Ben Miller [msft] ASP .Net 1 06-28-2003 01:46 AM



Advertisments