How to identify a row when a button is clicked

2 posts, 1 answers
  1. DuelingCats
    DuelingCats avatar
    49 posts
    Member since:
    Jul 2012

    Posted 01 Aug 2012 Link to this post

    I have a listview and each row has a RadButton in a column. I need to identify which row's button was clicked on the click event. The row describes a specific row in my database and I need to set a disable/enable column based upon the button clicked. Here is my item template for my ListView. I just need to figure out which row the button belongs on to get the ID. Thanks. 

    <ItemTemplate>
        <tr class="rlvI">
            <td>
                <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
            </td>
            <td>
                <asp:Label ID="TASK_NAMELabel" runat="server" Text='<%# Eval("TASK_NAME") %>' />
            </td>
            <td>
                <asp:Label ID="NEXT_RUNLabel" runat="server" Text='<%# Eval("NEXT_RUN") %>' />
            </td>
            <td>
                <asp:Label ID="FREQUENCYLabel" runat="server"  Text='<%# Eval("FREQUENCY") %>' />
            </td>
            <td>
                <telerik:RadButton ID="ENABLEDToggle" runat="server" Width=75 ButtonType="StandardButton" 
                ToggleType="CustomToggle" Checked='<%# Enabled_Converter(Eval("ENABLED")) %>' OnCheckedChanged="TaskStateChange_Clicked">
                    <ToggleStates>
                        <telerik:RadButtonToggleState Text="Enabled" />
                        <telerik:RadButtonToggleState Text="Disabled" />
                    </ToggleStates>
                </telerik:RadButton>
            </td>
        </tr>
    </ItemTemplate>
  2. Answer
    Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 03 Aug 2012 Link to this post

    Hello Justin,

    You could try the following approach:
      mark-up:
    <telerik:RadButton ... OnClick="TaskStateChange_Clicked">
      code-behind:
    protected void TaskStateChange_Clicked(object sender, EventArgs e)
    {
        RadButton button = sender as RadButton;
        RadListViewDataItem item = button.Parent as RadListViewDataItem;
        int displayIndex = item.DisplayIndex;
        int dataItemIndex = item.DataItemIndex;
    }

    You could also extract the values from the current item as demonstrated in the following demo:
     ListView / Selecting items

    I hope this will prove helpful.

    Greetings,
    Eyup
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top