hi there,
i've one requirement
i am using a button in three different rows

for example
( <tr id="tr1"> <td/>
  <tr id ="tr2"> <td/> 
  <tr id="tr3> <td/>)

here i am using one button control (instead of 3 buttons) in 3 different rows

now in button onclick event, how can i distinguish the button depend upon the table row or table data id property

for example

public void btn_onclick(object sender,event args e)......
if(tr.id == "tr1")
do this
else if( tr.id == "tr2")
do this
6 Years
Discussion Span
Last Post by kiranbvsn

While I don't believe the example you are attempting is possible (to my knowledge) as you can't determine a button based on it's position within a table (again, to my knowledge)...

I think you could probably get away with 1 event handler that assigns different events based on which button calls it.

Realistically it would be better all around to have separate event handlers if you're going to be performing separate tasks. However, if your tasks are generally identical with minor differences (ie: a different variable value) depending on which button is pressed you could use the following to achieve this:

In the .aspx page my example uses this ~

    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="btn_onclick" />
        <asp:Button ID="Button2" runat="server" Text="Button" onclick="btn_onclick" />
        <asp:Button ID="Button3" runat="server" Text="Button" onclick="btn_onclick" />
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

And in the code-behind it uses this ~

protected void btn_onclick(object sender, EventArgs e)
        if (sender.Equals(Button1))
            TextBox1.Text = "Button1";
        else if (sender.Equals(Button2))
            TextBox1.Text = "Button2";
        else if (sender.Equals(Button3))
            TextBox1.Text = "Button3";

The result is that the btn_onclick checks the sender against the various buttons until it finds a match and performs it's task based on that.

Hope this helps :) Please mark as solved if this resolves your issue.

Edited by Lusiphur: n/a


yeah this approach is also good! but i donno if the first possibility ever exists or not! I want to use less controls in the user form!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.