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

Hide linkbuttons in commandItemTemplate

10 Answers 766 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 16 Jul 2012, 06:14 PM
Hi,

Have a custom template for my CommandItemTemplate and I am trying to hide or unhide a linkbutton when they click another one, but nothing has worked so far and I am out of ideas.  it just does not find the linkbuttons, when i click on one I want to hide it then unhide the other to return.
<CommandItemTemplate>
                                           <table width="100%">
                                               <tr>
                                                   <td align="left">
                                                       <asp:LinkButton ID="lnkAddNew" runat="server" Text="Add New Record" Font-Bold="true" Font-Underline="true" CommandName="AddNew"></asp:LinkButton>
                                                             
                                                       <asp:LinkButton ID="lnkExisting" runat="server" Text="View History" Font-Bold="true" Font-Underline="true" CommandName="ViewHist"></asp:LinkButton>
                                                       <asp:LinkButton ID="lnkCurrent" runat="server" Text="View Current" Font-Bold="true" Font-Underline="true" CommandName="ViewCurrent" Visible="false"></asp:LinkButton>
                                                   </td>
                                               </tr>
                                           </table>
                                       </CommandItemTemplate>


If (e.CommandName = "ViewHist") Then
          Dim cmdItem As GridCommandItem = DirectCast(e.Item, GridCommandItem)
          Dim hist As LinkButton = DirectCast(cmdItem.FindControl("lnkExisting"), LinkButton)
          Dim curr As LinkButton = DirectCast(cmdItem.FindControl("lnkCurrent"), LinkButton)
          hist.Visible = False
          curr.Visible = True
          HFHist.Value = 1
          myRadGrid.Rebind()
          myRadGrid.Columns(0).Visible = False
      End If


tried this as well
Dim cmdItem As GridCommandItem = DirectCast(myRadGrid.MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)

10 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 16 Jul 2012, 07:43 PM
Hello,

<CommandItemTemplate>
               <asp:LinkButton ID="LinkButton1" runat="server" CommandName="LinkButton1">LinkButton1</asp:LinkButton>
               <asp:LinkButton ID="LinkButton2" runat="server" CommandName="LinkButton2">LinkButton2</asp:LinkButton>
           </CommandItemTemplate>
protected void dgPassanger_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "LinkButton1")
            {
                GridCommandItem item = dgPassanger.MasterTableView.GetItems(GridItemType.CommandItem)[0] as GridCommandItem;
                (item.FindControl("LinkButton2") as LinkButton).Visible = false;
            }
        }


Thanks,
Jayesh Goyani
0
Kevin
Top achievements
Rank 1
answered on 16 Jul 2012, 08:06 PM
HI,

ok don't see much differencet in what i was doing and tried it but same results, it does not hide the 1st linkbutton and unhide the other link button.  Checked my e.commandname to verify and everything looks fine, just does not change the visibility of the linkbuttons.
<CommandItemTemplate>
                                           <table width="100%">
                                               <tr>
                                                   <td align="left">
                                                       <asp:LinkButton ID="lnkAddNew" runat="server" Text="Add New Record" Font-Bold="true" Font-Underline="true" CommandName="AddNew"></asp:LinkButton>
                                                             
                                                       <asp:LinkButton ID="lnkExisting" runat="server" Text="View History" Font-Bold="true" Font-Underline="true" CommandName="ViewHist"></asp:LinkButton>
                                                       <asp:LinkButton ID="lnkCurrent" runat="server" Text="View Current" Font-Bold="true" Font-Underline="true" CommandName="ViewCurrent" Visible="false"></asp:LinkButton>
                                                   </td>
                                               </tr>
                                           </table>
                                       </CommandItemTemplate>


If (e.CommandName = "ViewHist") Then
          Dim cmdItem As GridCommandItem = TryCast(myRadGrid.MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)
          TryCast(cmdItem.FindControl("lnkExisting"), LinkButton).Visible = False
          TryCast(cmdItem.FindControl("lnkCurrent"), LinkButton).Visible = True
          HFHist.Value = 1
          myRadGrid.Rebind()
          myRadGrid.Columns(0).Visible = False
      End If
0
Shinu
Top achievements
Rank 2
answered on 17 Jul 2012, 04:57 AM
Hi Kevin,

Please try removing the 'myRadGrid.Rebind()' and check whether the LinkButton is hiding or not.

VB:
Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
    If (e.CommandName = "ViewHist") Then
        Dim cmdItem As GridCommandItem = TryCast(RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)
        TryCast(cmdItem.FindControl("lnkExisting"), LinkButton).Visible = False
        TryCast(cmdItem.FindControl("lnkCurrent"), LinkButton).Visible = True
        HFHist.Value = 1
        myRadGrid.Columns(0).Visible = False
    End If
End Sub

Thanks,
Shinu.
0
Kevin
Top achievements
Rank 1
answered on 17 Jul 2012, 12:26 PM
Hi,

ok that is what is happening is when rebinding its not hiding or showing the link buttons, but the problem is I need it to rebind, what I am doing is showing the history tables in the same grid, the grid is rebound based on the value I set on the hidden field.
0
Kevin
Top achievements
Rank 1
answered on 17 Jul 2012, 12:32 PM
HI,
A method i usually use with asp grids is a checkbox outside the grid to vieew history, is there a way to find the checkbox event in the commandItemtemplate.
0
Shinu
Top achievements
Rank 2
answered on 18 Jul 2012, 04:08 AM
Hi Kevin,

You can attach the CheckChanged event for the CheckBox in CommandItemTemplate directly from its properties or can attach the event from ItemCreated event as shown below.

ASPX:
<CommandItemTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" />
</CommandItemTemplate>

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridCommandItem)
    {
        GridCommandItem citem = (GridCommandItem)e.Item;
        CheckBox checkbox = (CheckBox)citem.FindControl("CheckBox1");
        checkbox.CheckedChanged += new EventHandler(checkbox_CheckedChanged);
    }
}
void checkbox_CheckedChanged(object sender, EventArgs e)
{
    //your code
}

Thanks,
Shinu.
0
Kevin
Top achievements
Rank 1
answered on 18 Jul 2012, 01:26 PM
Hi shinu,

Thanks for the response, but does not work for me on conversion to VB.

On this protion I get a blue underline
 CheckBox.CheckedChanged += New EventHandler(AddressOf checkbox_CheckedChanged)

is an event and cannot be called directly, use a raiseevent statement to raise the event.


Protected Sub myRadGrid_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGrid.ItemCreated
       If TypeOf e.Item is GridCommandItem the
           Dim citem As GridCommandItem = DirectCast(e.Item, GridCommandItem)
           Dim Cb As CheckBox = DirectCast(citem.FindControl("cbhist"), CheckBox)
           CheckBox.CheckedChanged += New EventHandler(AddressOf checkbox_CheckedChanged)
       End If
   End Sub
   Private Sub checkbox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
       'your code
   End Sub
0
Kevin
Top achievements
Rank 1
answered on 18 Jul 2012, 01:27 PM
Hi,

changed this line to line to follwoing naming but still same error

Cb.CheckedChanged += New EventHandler(AddressOf checkbox_CheckedChanged)
0
Accepted
Shinu
Top achievements
Rank 2
answered on 19 Jul 2012, 04:06 AM
Hi kevin,

I apologize for giving the code in C#. The proper way to add an event handler in VB code-behind is the following.

VB:
AddHandler Cb.CheckedChanged, AddressOf Cb_CheckedChanged

Please take a look into this help documentation for more details.

Thanks,
Shinu.
0
Kevin
Top achievements
Rank 1
answered on 19 Jul 2012, 12:36 PM
Hi,
No its no problem, i know enough c# to be damergous and can read most of it and usually put it in a converter, this is just one of those instances where i have never run into doing this.  I am surprised that the c# convertor I used did not catch this.  thanks for your help.
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Kevin
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or