Attach server side click event handler for HTML Table TR (Row) in ASP.Net

Last Reply on Feb 10, 2014 12:03 AM By Azim

Posted on Feb 09, 2014 11:36 PM

I hv a tr inside repeater i want a onclick function on tr which all some method in code behind.?

You are viewing reply posted by: Azim on Feb 10, 2014 12:03 AM.
Posted on Feb 10, 2014 12:03 AM Modified on on Feb 10, 2014 12:03 AM

You have  use  IPostBackEventHandler interface for this

This Way:

 Reference:

http://stackoverflow.com/questions/5419115/table-row-onclick-event-that-runs-codebehind

<form id="form1" runat="server">
    <asp:Repeater ID="rptDemo" runat="server">
        <ItemTemplate>
            <table>
                <tr onclick="<%= _jsPostBackCall %>;">
                    <td>
                        <asp:Label Text='<%# Eval("Alphabets") %>' runat="server" />
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:Repeater>
    <h1>
        <asp:Literal ID="litMessage" runat="server" />
    </h1>
    <asp:Button Text="Save" runat="server" />
    </form>

C#:

 

public partial class TableRowEvent : System.Web.UI.Page, IPostBackEventHandler
{
    protected String _jsPostBackCall;

    protected void Page_Load(object sender, EventArgs e)
    {
        // "myTr" will be passed as an argument to RaisePostBackEvent method
        _jsPostBackCall = ClientScript.GetPostBackEventReference(this, "myTr");
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Alphabets", typeof(string));
            dt.Rows.Add("A");
            dt.Rows.Add("B");
            dt.Rows.Add("C");
            this.rptDemo.DataSource = dt;
            this.rptDemo.DataBind();
        }
        
    }

    public void RaisePostBackEvent(string eventArgument)
    {
        switch (eventArgument)
        {
            case "myTr":
                HandleTrClick();
                break;

            // you can add other controls that need postback processing here

            default:
                throw new ArgumentException();
        }
    }

    private void HandleTrClick()
    {
        litMessage.Text = "Tr clicked.";
    }
}

Thank You.