How to Access Controls In CommandItemTemplate From ItemCommand

8 posts, 0 answers
  1. saba
    saba avatar
    6 posts
    Member since:
    Aug 2010

    Posted 31 Aug 2010 Link to this post

    Hi there.I have a RadGrid With 2 linkbuttons in CommandItemTemplate .i have a panel too that  when user click on row,it become visible . i want to visible these linkbuttons when this panel is visible.Anyone has ideas?
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 31 Aug 2010 Link to this post

    Hello Saba,

    Check out the following code snippet which shows how to access controls in CommandItemTemplate from ItemCommand event.

    ASPX:
    <CommandItemTemplate>
      <asp:LinkButton ID="LinkButton1" runat="server" >LinkButton3</asp:LinkButton>
      <asp:LinkButton ID="LinkButton2" runat="server">LinkButton4</asp:LinkButton>
    </CommandItemTemplate>

    C#:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
       {
           if (e.CommandName == "RowClick")
           {
               GridCommandItem cmdItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
               LinkButton linkBtn1 = (LinkButton)cmdItem.FindControl("LinkButton1");
               linkBtn1.Visible = false;
               LinkButton linkBtn2 = (LinkButton)cmdItem.FindControl("LinkButton2");
               linkBtn2.Visible = false;
            }
       }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Paulo
    Paulo avatar
    30 posts
    Member since:
    Mar 2013

    Posted 18 Mar 2014 in reply to Princy Link to this post

    Princy

    I have two buttons in the GridCommandsItem which I would like to make visible or not.
    I applied the code you have showed but it does not hide the buttons. If I click on the button it's functionality works but I cannot turn visible to false. 
    if (e.CommandName == "HideStatus")
    {
    RadGrid1.MasterTableView.GetColumn("finalstatus").Display = false;
    GridCommandItem cmd1 = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
    RadButton btn1 = cmd1.FindControl("RadButton2") as RadButton;
    btn1.Visible = true;
    RadButton btn2 = cmd1.FindControl("RadButton3") as RadButton;
    btn2.Visible = false;
    RadGrid1.MasterTableView.Rebind();
    }
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Mar 2014 in reply to Paulo Link to this post

    Hi Paulo,

    Please remove the Rebind() from your code for the functionality to work.

    C#:
    if (e.CommandName == "HideStatus")
    {      
     GridCommandItem cmd1 = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
     RadButton btn1 = cmd1.FindControl("RadButton2") as RadButton;
     btn1.Visible = true;
     RadButton btn2 = cmd1.FindControl("RadButton3") as RadButton;
     btn2.Visible = false
    }

    Thanks,
    Princy
  6. Paulo
    Paulo avatar
    30 posts
    Member since:
    Mar 2013

    Posted 20 Mar 2014 in reply to Princy Link to this post

    Hi Princy
    I have removed the Rebind() from the code behind and still the functionality to hide the column does work but it does not hide or show the buttons.
    This is the front end:
    <MasterTableView CommandItemDisplay="Top" CommandItemStyle-Height="30px" >
        <CommandItemTemplate>
           <table>
              <tr>
                  <td style="padding:1px 1px;">
                        <telerik:RadButton ID="Button1" AutoPostBack="true" runat="server" CommandName="Rebind" Text="Clear All Filters">
                       </telerik:RadButton>
                </td>
                <td style="padding:1px 1px;">
                      <telerik:RadButton ID="Button2" AutoPostBack="true" runat="server" Visible="true" CommandName="ShowStatus" Text="Show Status Date">
                      </telerik:RadButton>
               </td>
               <td style="padding:1px 1px;">
                     <telerik:RadButton ID="Button3" AutoPostBack="true" runat="server" Visible="false" CommandName="HideStatus" Text="Hide Status Date" >
                    </telerik:RadButton>
              </td>
          </tr>
      </table>
    </CommandItemTemplate>
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 20 Mar 2014 in reply to Paulo Link to this post

    Hi Paulo,

    I'm not clear about your requirement. I see that you have set Visible="false", so it wont be shown at first, so I guess you are setting its visibility to true some where else. Here is the code snippet I tried, which works as expected.

    ASPX:
    <CommandItemTemplate>
        <table>
            <tr>
                <td>
                    <telerik:RadButton ID="Button2" runat="server" CommandName="ShowStatus" Text="Show Status Date">
                    </telerik:RadButton>
                </td>
                <td>
                    <telerik:RadButton ID="Button3" runat="server" CommandName="HideStatus" Visible="false" Text="Hide Status Date">
                    </telerik:RadButton>
                </td>
            </tr>
        </table>
    </CommandItemTemplate>

    C#:
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
      if (e.CommandName == "HideStatus")
       {
         GridCommandItem cmd1 = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];      
         RadButton btn = cmd1.FindControl("Button3") as RadButton;
         btn.Visible = false;
     }
     if (e.CommandName == "ShowStatus")
     {
         GridCommandItem cmd1 = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
         RadButton btn = cmd1.FindControl("Button3") as RadButton;
         btn.Visible = true;
       }
    }

    Thanks,
    Princy
  8. Paulo
    Paulo avatar
    30 posts
    Member since:
    Mar 2013

    Posted 24 Mar 2014 in reply to Princy Link to this post

    Princy

    I have copied and paste your code (which is similar to mine) into my project and still does not work. In my project I have two buttons one hides column A and the other button shows it. At first the button to hide the column A would be not seen by the user and as soon as the user click on "Show Column A" the button "Show Column A" would set its visibility to false and would set the "Hide Column A" to visible. The functionality to show and hide the column works fine, but the visibility of the button does not.

    Thanks
  9. Kostadin
    Admin
    Kostadin avatar
    1711 posts

    Posted 25 Mar 2014 Link to this post

    Hi Paulo,

    I prepared a small sample which demonstrates the same scenario as the one which you have described. Please give it a try and let me know about the result.

    Regards,
    Kostadin
    Telerik
     

    Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017