Code behind and onclick on html table row

Discussion in 'ASP .Net' started by Corno, Sep 3, 2004.

  1. Corno

    Corno Guest

    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
     
    Corno, Sep 3, 2004
    #1
    1. Advertising

  2. Corno

    Rutger Smit Guest

    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
     
    Rutger Smit, Sep 3, 2004
    #2
    1. Advertising

  3. 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:...
    > 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
    >
    >
     
    Eliyahu Goldin, Sep 5, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    648
    Alphonse Giambrone
    Jun 28, 2003
  2. phl
    Replies:
    1
    Views:
    4,434
    Martin Jay
    Jun 8, 2006
  3. Replies:
    0
    Views:
    494
  4. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    532
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  5. Corno

    Code behind and onclick on html table row

    Corno, Sep 3, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    167
    Eliyahu Goldin
    Sep 5, 2004
Loading...

Share This Page