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..

3
  • Possible to duplicate of stackoverflow.com/questions/1943032/… Commented Nov 18, 2015 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. Commented Nov 18, 2015 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 <td> to </tr> to close your <tr> tag properly and not have a <td> tag without closure. Commented Nov 18, 2015 at 13:28

1 Answer 1

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..
         }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.