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
..........
}

Recommended Answers

All 2 Replies

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 ~

<body>
    <form id="form1" runat="server">
    <div>
        <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>
    </div>
    </form>
</body>

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.

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.