Senthil
Senthil

Reputation: 106

ItemCommand in Listview not firing in the usercontrol

I have user control which has the linkbutton in the Item Template, I am trying to capture the Itemcommand event in the code behind, but the event is not getting fired.

I have gone through the other similar questions, but it didnot help me. Below is my code snippet, could anyone help me on this?

Listview-

<asp:ListView runat="server" ID="lvTherapeuticAlternatives" OnItemCommand="TherapeuticAlternatives_OnItemCommand">

ItemTemplate-

<ItemTemplate>
            <tr class='data'>
                <td style="width:210px;">
                    <asp:LinkButton  ID="lnkMedSelection"  runat="server" CommandName="SelectedMed"  CommandArgument='<%#Eval("NDC") & ", " & Eval("DrugGenericProductID") %>' >
                    <asp:Label ID="lblDrugName" runat="server"  Text='<%# Eval("DrugDescription") %>' /> 
                    </asp:LinkButton >
                </td>
                <td style="width:70px;" align="center">
                    <asp:Label ID="lblBrandGeneric" runat="server" Text='<%# Eval("descBrandGeneric") %>' /> 
                </td>
                <td style="width:110px;" align="center">
                    <asp:Label ID="lblStatus" runat="server" Text='<%# Eval("FormularyStatusDescription") %>' /> 
                </td>
                <td style="width:210px;" align="left">
                    <asp:Label ID="lblFlat" runat="server" Text='<%# Eval("CopayInfo") %>' /> 
                </td>
            </tr>
         </ItemTemplate>  

Codebehind-

    Protected Sub TherapeuticAlternatives_OnItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles lvTherapeuticAlternatives.ItemCommand

    End Sub

Upvotes: 2

Views: 4876

Answers (2)

Senthil
Senthil

Reputation: 106

The Item command was not firing, it is because I had a ISPostback check in Page load event so it was resisting the event handler to call the method registered for ItemCommand event.

When I remove the IsPostback check in the webcontrol, the event is getting fired.

Upvotes: 2

Icarus
Icarus

Reputation: 63966

From MSDN:

The ItemCommand event is raised when a button in the ListView control is clicked. This enables you to perform a custom routine whenever this event occurs.

And you don't have any button or any other type of control on your ListView that may raise a PostBack; therefore, your ItemCommand handler is never raised.

Update

If you declare your LinkButton like this (pay attention only to the OnClick event):

<asp:LinkButton  ID="lnkMedSelection"  OnClick="lnkMedSelection_Click" runat="server" CommandName="SelectedMed"  CommandArgument='<%#Eval("NDC") & ", " & Eval("DrugGenericProductID") %>' >

And you add in your code behind this:

  Protected Sub lnkMedSelection_Click(sender As Object, e As EventArgs)
        ' Do something here  for example:
         Label2.Text = "Linked button clicked"
    End Sub



Protected Sub TherapeuticAlternatives_OnItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles lvTherapeuticAlternatives.ItemCommand
    'Notice how this event is also raised. 
    ' You can put a break point or simply test with a label as so:
     Label1.Text = "ItemCommand Fired"
End Sub

Upvotes: 0

Related Questions