ASP.NET Listview->Click Row->Perform action

无人久伴 提交于 2019-12-02 20:38:41

问题


I need some help with the following.

I have a list view that I fill with code-behind:

Linq->Sql:

        ListView1.DataSource = from x in database.ITEMS
                               select x;
        ListView1.DataBind();

Content of ITEMS:

  • ID
  • Name
  • Quantity

I only show the name of the fields:

        <tr>
            <td>
                <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("Name") %>' />
            </td>
        </tr>

Assuming I have 25 rows in the database, I fill the list view with 25 rows. Next is what I need help with. The user needs to click one of the 25 names (complete row) and redirects to a page where the content of that row is showed. So lets say someone clicks on "Cars", he'll be redirected and he will see: You have selected Cars, Cars has an ID of 19 and a Quantity of 6.

I have browsed the Internet already but I am having trouble finding solutions that I can understand. I am kind of a noob when it comes to list views and other data displaying methods.

My 2 questions:

  1. How do I start a event when I click a row (and not a button or a link in that row)?
  2. When I click a row and start a event, how can I check which one was clicked?

I hope someone can explain to me how this is done in a user friendly noob kind of way or redirect me to a tutorial discussing my issue's and also in a user friendly way. Thanks in advance.


回答1:


Ok - I am not sure if this is the best way to go with this (I'll be interested what other think)... but this will work .

Update your listview to something like this:

        <script type="text/javascript">
            // simple redirect to your detail page, passing the selected ID 
            function redir(id) {
                window.location.href = "mydetailpage.aspx?id=" + id;
            }
        </script>
        <table>
        <asp:ListView ID="ListView1" runat="server">
            <ItemTemplate>
                <tr onclick="redir('<%# Eval("ID") %>');">
                    <td>
                        <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("Name") %>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:ListView>
        </table>

Then create another page to show your detail (e.g. mydetailpage.aspx) - this should requery your database and show the other fields, given the id passed in the querystring.




回答2:


you can define onclick attribute to call javascript function on tr element like this :

onclick="yourfunction('<%# Eval("Name") %>');"

Then you can put anything in your javascript function like redirect to another page



来源:https://stackoverflow.com/questions/20098257/asp-net-listview-click-row-perform-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!