代码之家  ›  专栏  ›  技术社区  ›  Paul Michaels

ASP.NET中继器控件-引用单个元素

  •  0
  • Paul Michaels  · 技术社区  · 15 年前

    我有一个中继器,它在窗体上多次显示自定义用户控件,如下所示:

    <asp:Repeater runat="server" ID="MyRepeater" 
        ondatabinding="MyRepeater_DataBinding" >
        <ItemTemplate>
            <a name='<%# Eval("[\"Key\"]") %>' style="display: none;"></a>            
    
            <uc1:MyControl ID="Control1" runat="server" 
                Info='<%#Eval("[\"Info\"]") %>' Date='<%#Eval("[\"Date\"]") %>' 
                 Key='<%#Eval("[\"Key\"]") %>' />/>
        </ItemTemplate>
    </asp:Repeater>
    

    将此绑定到SQL数据源将正确显示我期望的信息:

        SqlDataSource.SelectCommand =
                    "SELECT Info, Date, Key " +
                    "FROM [dbo].[Test] ";
        SqlDataSource.SelectCommandType = SqlDataSourceCommandType.Text;
        DataView resultsdv = (DataView)SqlDataSource.Select(DataSourceSelectArguments.Empty);
    
        MyRepeater.DataSource = resultsdv.Table.Rows;
        MyRepeater.DataBind();
    

    我无法解决的是如何引用其中的单个元素,以直接转到我想要的特定项,例如,链接另一个网站的项。我试图使用类似Facebook的功能,因此我相信我需要一个网址,将直接带我到该项目的问题。有人能告诉我这件事的正确方向吗?

    编辑:

    我要找的是一种从网站外部引用数据转发器中单个项的方法。例如:

    http://www.mywebsite.com/MyPage/InfoItem3
    

    编辑:

    更改以上代码以反映使用标记给出的答案。尝试使用以下方法引用页:

    http://www.mywebsite.com/MyPage#Key
    

    例如

    http://www.mywebsite.com/MyPage#10
    

    只需重新加载页面

    编辑:

    来自用户控件的HTML:

    <script type="text/javascript" >
    $(function() {
        var iframe = $("#likeButton");
        var newSrc = iframe.attr("src");
        newSrc += encodeURIComponent(location.href) + "<%= lblKey.Text %>";
    
        iframe.attr("src", newSrc);
    
    });
    </script>
    
    <asp:Label runat="server" ForeColor="blue" Text="Date" Font-Bold="true" Font-Size="Smaller" ID="lblDate" Width="100%" />
    <br />
    <asp:Label runat="server" Text="Info" ID="lblInfo" Font-Size="Smaller" />
    <br />
    <asp:Label runat="server" Text="Key" ID="lblKey" Font-Size="Smaller" Visible="true" />
    <br />
    <iframe id="likeButton"
        src="http://www.facebook.com/plugins/like.php?href=" 
        scrolling="no" 
        frameborder="0"         
        style="border:none; overflow:hidden; 
        width:450px; height:80px;">
    </iframe>
    <br />
    
    2 回复  |  直到 15 年前
        1
  •  2
  •   Alexey Smolyakov    15 年前

    请为用户控件尝试以下代码(不需要Javascript):

    <asp:Label runat="server" ForeColor="blue" Text="Date" Font-Bold="true" Font-    Size="Smaller" ID="lblDate" Width="100%" /> 
    <br /> 
    <asp:Label runat="server" Text="Info" ID="lblInfo" Font-Size="Smaller" /> 
    <br /> 
    <asp:Label runat="server" Text="Key" ID="lblKey" Font-Size="Smaller" Visible="true" /> 
    <br /> 
    <iframe id="likeButton" 
        src="http://www.facebook.com/plugins/like.php?href=<%# lblKey.Text %>"  
        scrolling="no"  
        frameborder="0"          
        style="border:none; overflow:hidden;  
        width:450px; height:80px;"> 
    </iframe> 
    <br />
    
        2
  •  2
  •   Tom Halladay Amy B    15 年前

    如果包含锚标记,则可以通过锐利标记在URL中引用它:

    http://www.site.com/page.aspx#Info

    <asp:Repeater runat="server" ID="MyRepeater">
        <ItemTemplate>
            <a name='<%# Eval("Info") %>' style="display: none;"></a>
            <uc1:MyControl ID="Control1" runat="server" 
                     Date='<%# Eval("Date") %>' 
                     Info='<%# Eval("Info") %>' />
        </ItemTemplate>
    </asp:Repeater>
    
    推荐文章