1

Iam using a listview to display table contents,How can i access a values form listview to code behind in button click

aspx

<LayoutTemplate>
<table runat="server" id="table1">
 <tr id="Tr1" runat="server">
    <th class="tablehead"id="mname">Movie Name</th>
    <th class="tablehead">Movie Genre</th>
    <th class="tablehead">Runtime</th>

 </tr>
<tr runat="server" id="itemPlaceholder"></tr>

<ItemTemplate>
<tr id="Tr2" runat="server" class="tablerw">
 <td style="background-color:#EEEEEE;width:100px;" class="tablerw"><asp:Label ID="Label5" runat="server" Text='<%#Eval("MovieName") %>' /></td>
 <td style="background-color:#EEEEEE;width:100px;"><asp:Label ID="NameLabel" runat="server" Text='<%#Eval("movieGenre") %>' /></td>
 <td style="background-color:#EEEEEE;width:100px;"><asp:Label ID="Label1" runat="server" Text='<%#Eval("Runtime") %>' /></td>
<td>
 <asp:Button ID="Button1" runat="server" Text="Approve" OnClick="Button1_Click"></asp:Button>//Here is my button
 </ItemTemplate>  

aspx.cs

 protected void Button1_Click(Object sender,
                    System.EventArgs e)
      {
       //I want to access value here
      }

I want to get Movie Name,Movie Genre,Runtime to code behind.. Any help is appreciated..

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
Unnikrishnan.S
  • 185
  • 2
  • 17
  • Possible to duplicate of http://stackoverflow.com/questions/1943032/how-to-access-controls-located-in-the-listview-from-codebehind – Kaushik Maheta Nov 18 '15 at 11:36
  • While it possible with FindControl (see what Kaushik has linked to), this should be avoided at all cost. The resulting code will be prone to errors and hard to maintain. Cannot you consider using ItemCommand of ListVIew instead? Button could fire a command and provide an Id as a command argument. Then in the handler fetch the data item from DB. – Andrei Nov 18 '15 at 11:42
  • I agree with the comment by @Andrei and the answer by @Rahul. In your example, however, you want to change the last `` to `` to close your `` tag properly and not have a `` tag without closure. – Brian Payne Nov 18 '15 at 13:28

1 Answers1

1

Proper way to deal with button control click event in data bound controls is by setting a CommandArgument & CommandName properties. Instead of registering a click handler of button you can register a ItemCommand event of ListView. This way whenever a button is clicked then this event is raised and you can find the data correctly like this:-

Add CommandName & CommandArgument properties to your button and remove the click handler:-

<asp:Button ID="Button1" runat="server" Text="Approve" CommandName="GetData"
            CommandArgument='<%# Eval("MovieId") %>' ></asp:Button>

Next, register the ItemCommand event with your listview:-

 <asp:ListView ID="lstMovies" runat="server" OnItemCommand="ListView1_ItemCommand">

Finally in the code behind, in ListView1_ItemCommand method check if event is raised by your button and find all the respective controls:-

 protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
 {
     if (e.CommandName == "GetData")
     {
        if (e.CommandSource is Button)
         {
             ListViewDataItem item = (e.CommandSource as Button).NamingContainer 
                                      as ListViewDataItem;
             Label NameLabel = item.FindControl("NameLabel") as Label;
             Label Label5 = item.FindControl("Label5") as Label;
             //and so on..
         }
    }
}
Rahul Singh
  • 21,585
  • 6
  • 41
  • 56