This is a migrated thread and some comments may be shown as answers.

Button inside Template Column, can't fire event handler

4 Answers 559 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gavin
Top achievements
Rank 1
Gavin asked on 19 Jul 2011, 01:55 PM
<telerik:RadGrid ID = "RadGrid1" AutoGenerateColumns = "false" runat = "server" AllowPaging="True"
                    Width="97%" GridLines="None" Skin="WebBlue"
                    onneeddatasource="RadGrid1_NeedDataSource"
                    onitemcommand="RadGrid1_ItemCommand"
                    onprerender="RadGrid1_PreRender" >
                    <%--onitemcreated="RadGrid1_ItemCreated"
                    onselectedindexchanged="RadGrid1_SelectedIndexChanged">--%>
                  <ClientSettings EnableRowHoverStyle="false" EnablePostBackOnRowClick = "true">
                            <Selecting AllowRowSelect="true" />
                  </ClientSettings>
 
      <MasterTableView ShowHeader="false">
        <Columns>
            <telerik:GridTemplateColumn UniqueName="TemplateColumn">
                <ItemTemplate>
                  <table>
                    <tr>
                      <%# Eval("CategoryName") %>
                    </tr>
 
                    <tr>
                      <td>
                        <telerik:RadButton ID="Button1" runat="server" ButtonType="LinkButton" Text="Button1"
                                    CommandName="Button1Click" OnClick="Button1_Click" />
                      </td>
                      <td>
                        <telerik:RadButton ID="Button2" runat="server" ButtonType="LinkButton" Text="Button2"
                                    CommandName="Button2Click" OnClick="Button2_Click" />
                      </td>
                    </tr>
                  </table>
                   
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
      </MasterTableView>
    </telerik:RadGrid>
  So, I'm new to this whole Telerik thing and I'm having a little problem...
 I have a RadGrid and one of its columns is a Template Column.. Inside the template column, I have a button... I can't seem to get the event handler of that button to fire....
   I also need to know the button of which row was clicked... I tried the ItemCommand and it didn't work.. any ideas? thnx for the help...

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 20 Jul 2011, 08:39 AM
Hello Gavin,
I tried the same scenario which worked as expected.Please try the same in a seperate page.

aspx:
<telerik:RadGrid ID="RadGrid1" AutoGenerateColumns="false" runat="server" AllowPaging="True"
           Width="97%" GridLines="None" Skin="WebBlue" OnNeedDataSource="RadGrid1_NeedDataSource"
           OnItemCommand="RadGrid1_ItemCommand">
           <MasterTableView>
               <Columns>
                   <telerik:GridTemplateColumn UniqueName="TemplateColumn">
                       <ItemTemplate>
                           <table>
                               <tr>
                               </tr>
                               <tr>
                                   <td>
                                       <telerik:RadButton ID="Button1" runat="server" ButtonType="LinkButton" Text="Button1"
                                           CommandName="Button1Click" OnClick="Button1_Click" />
                                   </td>
                                   <td>
                                       <telerik:RadButton ID="Button2" runat="server" ButtonType="LinkButton" Text="Button2"
                                           CommandName="Button2Click" />
                                   </td>
                               </tr>
                           </table>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
               </Columns>
           </MasterTableView>
</telerik:RadGrid>

Thanks,
Shinu.
0
Gavin
Top achievements
Rank 1
answered on 20 Jul 2011, 10:15 AM
  Thnx! it does work.... So now I have another problem... I need to expand a Details Table on the click of this button.. How can I know which row does the button clicked belongs to? inside the event handler, I tried casting the EventArgs to GridCommandEventArgs but it expectedly didn't work...

    How can I identify the row that the clicked button belongs to?
protected void Button1_Click(object sender, EventArgs e)
        {
                        // Expand/Collapse row
            GridCommandEventArgs elem = (GridCommandEventArgs)e;
            elem.Item.Expanded = !elem.Item.Expanded;
 
                        // Show needed details table
            GridTableView nestedTableView = (elem.Item as GridDataItem).ChildItem.NestedTableViews[0];
            nestedTableView.Visible = true;
 
                        // Hide other details tables
            nestedTableView = (elem.Item as GridDataItem).ChildItem.NestedTableViews[1];
            nestedTableView.Visible = false;
        }

0
Shinu
Top achievements
Rank 2
answered on 21 Jul 2011, 05:19 AM
Hello Gavin,

Try the following code snippet to get the index of clicked row.
C#:
protected void Button1_Click(object sender, EventArgs e)
   {
       Button btn = (Button)sender;
       GridDataItem dataitem = (GridDataItem)btn.NamingContainer;
       int index = dataitem.ItemIndex;
   }

Thanks,
Shinu.
0
Gavin
Top achievements
Rank 1
answered on 21 Jul 2011, 12:31 PM
Thanks again Shinu! that was really helpful but I can't seem to be able to get my details table expanded on that button click... I'm feeling so helpless.. what am I missing here?
   I tried the same code with a ButtonColumn and it worked... what's different here??

protected void Button1_Click(object sender, EventArgs e)
        {
                        // Expand/Collapse row
            RadButton btn = (RadButton)sender;
            GridDataItem dataItem = (GridDataItem)btn.NamingContainer;
            dataItem.Expanded = !dataItem.Expandeds;
 
                        // Show needed details table
            GridTableView nestedTableView = dataItem.ChildItem.NestedTableViews[0];
            nestedTableView.Visible = true;
 
                        // Hide other details table
            nestedTableView = dataItem.ChildItem.NestedTableViews[1];
            nestedTableView.Visible = false;
        }
Tags
Grid
Asked by
Gavin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Gavin
Top achievements
Rank 1
Share this question
or