重定向列表视图排点击 [英] Redirect on Listview row click

查看:155
本文介绍了重定向列表视图排点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图和列一个叫 ID_NUMBER
是否有可能重定向到另一个网页,如果用户点击了行吗?
例如,如果 ID_NUMBER 4,比页应该被重定向到〜/ Page.aspx?ID = 4

code:

 < ASP:ListView控件ID =ListView1的=服务器
    的DataKeyNames =ID_NUMBER>
    < EmptyDataTemplate>
        <表ID =表1=服务器的风格=>
            &所述; TR>
                &所述; TD>
                    没有数据被送回< / TD>
            < / TR>
        < /表>
    < / EmptyDataTemplate>
    <&ItemTemplate中GT;
        < TR =风格FONT-FAMILY:宋体;颜色:#FFFFFF的onMouseOver =this.bgColor ='#219DD0';的onmouseout =this.bgColor ='#252526';>
            &所述; TD>
                < ASP:标签ID =Label1的=服务器文本='<%#的eval(ID_NUMBER)%>' />
            < / TD>
            &所述; TD>
                < ASP:标签ID =为textLabel=服务器文本='<%#的eval(文本)%GT;' />
            < / TD>
            &所述; TD>
                < ASP:标签ID =DateLabel=服务器文本='<%#的eval(DATE)%GT;' />
            < / TD>
        < / TR>
    < / ItemTemplate中>
    <&LayoutTemplate模板GT;
        <表ID =表2=服务器>
            < TR ID =TR1=服务器>
                < TD ID =TD1=服务器>
                    <表ID =itemPlaceholderContainer=服务器BORDER =0的风格=>
                        < TR ID =TR2=服务器的风格=>
                            百分位ID =Th1细胞=服务器>
                                名称和LT; /第i
                            百分位ID =Th2细胞=服务器>
                                文字< /第i
                            百分位ID =Th3的=服务器>
                                日期和LT; /第i
                        < / TR>
                        < TR ID =itemPlaceholder=服务器>
                        < / TR>
                    < /表>
                < / TD>
            < / TR>
        < /表>
    < / LayoutTemplate模板>
< / ASP:的ListView>


解决方案

您可以添加HTML链接或JavaScript插入的的ItemTemplate 你的的ListView pretty轻松:

列只是HTML列;在ItemTemplate,您可以访问所有的数据,包括ID_NUMBER的,这样你就可以在任何地方HTML使用它,而不仅仅是一列中。

既然你婉整排被点击,我会使用的HTML表行&LT的的onClick 事件的JavaScript函数; TR> 元素。你已经有2个事件(的onMouseOver,的onmouseout),用JavaScript在其中,所以这也只是多了一个...

JavaScript重定向开发前景 window.location.href 将在同一窗口重定向,假设这是你想要的(而不是一个新的窗口,等):

 <&ItemTemplate中GT;
    < TR =风格FONT-FAMILY:宋体;颜色:#FFFFFF
        的onMouseOver =this.bgColor ='#219DD0';
        的onmouseout =this.bgColor ='#252526';
        的onClick =window.location.href ='一些/路径/ Page.aspx ID =<%#的eval(ID_NUMBER)%>';
        &所述; TD>
            < ASP:标签ID =Label1的=服务器文本='<%#的eval(ID_NUMBER)%>' />
        < / TD>
        &所述; TD>
            < ASP:标签ID =为textLabel=服务器文本='<%#的eval(文本)%GT;' />
        < / TD>
        &所述; TD>
            < ASP:标签ID =DateLabel=服务器文本='<%#的eval(DATE)%GT;' />
        < / TD>
    < / TR>
< / ItemTemplate中>

需要注意的一点是Page.aspx的路径,你必须使用相对路径。

 〜/ page.aspx?ID =

是被编译到该网页的完整相对路径.NET路径。
然而,bacause您的ItemTemplate包含HTML,你需要在那里完全(相对)路径Page.aspx。

因此​​,而不是它应该是(只是一个例子):

  /some/path/page.aspx?id=<  -  page.aspx住在http://www.yoursite.com/some/path/page.aspx
/page.aspx?id=< - page.aspx生活的根,http://www.yoursite.com/page.aspx

或者您可以使用

 的onClick =window.location.href ='<%System.Web.VirtualPathUtility.ToAbsolute(?〜/ Page.Aspx ID =+的eval(ID_NUMBER) );%>';

I have a Listview and one of the columns is called id_Number. Is it possible to redirect to another page, if user clicks on one of the rows? For example if id_Number is 4, than page should be redirected to ~/Page.aspx?id=4.

Code:

<asp:ListView ID="ListView1" runat="server" 
    DataKeyNames="id_Number" >
    <EmptyDataTemplate>
        <table id="Table1" runat="server" style="">
            <tr>
                <td>
                    No data was returned.</td>
            </tr>
        </table>
    </EmptyDataTemplate>               
    <ItemTemplate>
        <tr style="font-family: Arial; color: #FFFFFF" onMouseOver="this.bgColor='#219DD0';" onMouseOut="this.bgColor='#252526';">
            <td>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("id_Number") %>' />
            </td>
            <td>
                <asp:Label ID="TextLabel" runat="server" Text='<%# Eval("Text") %>' />
            </td>
            <td>
                <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
            </td>
        </tr>
    </ItemTemplate>
    <LayoutTemplate>
        <table id="Table2" runat="server">
            <tr id="Tr1" runat="server">
                <td id="Td1" runat="server">
                    <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                        <tr id="Tr2" runat="server" style="">
                            <th id="Th1" runat="server">
                                Name</th>
                            <th id="Th2" runat="server">
                                Text</th>
                            <th id="Th3" runat="server">
                                Date</th>
                        </tr>
                        <tr ID="itemPlaceholder" runat="server">
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </LayoutTemplate> 
</asp:ListView>

解决方案

You can add an HTML link or a JavaScript into the ItemTemplate of your ListView pretty easily:

The "columns" are just HTML columns; in the ItemTemplate, you have access to all of your data, including id_number, so you can use it anywhere in the HTML, not just inside a column.

Since you wan the whole row to be clickable, I'd use a JavaScript function on the onClick event of the HTML table row <TR> element. You already have 2 events (onMouseOver, onMouseOut), with JavaScript in them, so this would just be one more...

A JavaScript redirect utilizes window.location.href which will redirect in the same window, assuming that's what you want (as opposed to a new window, etc.):

<ItemTemplate>
    <tr style="font-family: Arial; color: #FFFFFF" 
        onMouseOver="this.bgColor='#219DD0';" 
        onMouseOut="this.bgColor='#252526';"
        onClick="window.location.href = 'some/path/Page.aspx?id=<%# Eval("id_Number") %>';"
        <td>
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("id_Number") %>' />
        </td>
        <td>
            <asp:Label ID="TextLabel" runat="server" Text='<%# Eval("Text") %>' />
        </td>
        <td>
            <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
        </td>
    </tr>
</ItemTemplate>

One thing to note is for the path of Page.aspx, you have to use a relative path.

~/page.aspx?id=

Is a .net path that gets compiled into the full relative path to that page. However, bacause your ItemTemplate contains HTML, you'd need the full (relative) path to Page.aspx in there.

So instead it should be (just an example):

/some/path/page.aspx?id=   <-- page.aspx lives on http://www.yoursite.com/some/path/page.aspx
/page.aspx?id=             <-- page.aspx lives on root, http://www.yoursite.com/page.aspx

Alternately you can use

onClick="window.location.href = '<% System.Web.VirtualPathUtility.ToAbsolute("~/Page.Aspx?ID=" + Eval("id_number")); %>';"

这篇关于重定向列表视图排点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆