2013-04-06 66 views
0

我有一个具有asp:ListView的网页,我试图弄清楚如何以编程方式滚动到某个项目。这里是我的ASPX标记:以编程方式滚动/移动到.net asp中的项目:listview控件

<asp:ListView ID="lvGlossary" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="lvGlossary_ItemDataBound"> 
    <LayoutTemplate> 
     <div class="glossaryTitle">Glossary</div> 
     <div id="ItemPlaceholder" runat="server"></div> 
     <br /> 
    </LayoutTemplate> 
    <ItemTemplate> 
     <div class="glossaryEntry"> 
      <span class="glossaryWord"> 
       <asp:Label ID="lbWord" runat="server" Text='<%# Bind("Word") %>' />: 
      </span> 
      <span class="glossaryDefinition"> 
       <asp:Label ID="lbDefinition" runat="server" Text='<%# Bind("Definition") %>' /> 
      </span> 
      <br /> 
     </div> 
    </ItemTemplate> 
    <AlternatingItemTemplate> 
     <div class="glossaryEntryAlternating"> 
      <span class="glossaryWord"> 
       <asp:Label ID="lbWord" runat="server" Text='<%# Bind("Word") %>' />: 
      </span> 
      <span class="glossaryDefinition"> 
       <asp:Label ID="lbDefinition" runat="server" Text='<%# Bind("Definition") %>' /> 
      </span> 
      <br /> 
     </div> 
    </AlternatingItemTemplate> 
</asp:ListView> 

在我的C#代码后面我填充文本,当用户选择一个的话,我想自动滚动到这个词在ListView所以它是可见的文本框中。它很可能需要做客户端,所以我认为我需要一些JavaScript来做到这一点。任何人都可以给我一个如何做到这一点的例子吗?

任何帮助表示赞赏!

谢谢

回答

0

你可以使用一个元素的scrollIntoView JavaScript方法:

document.getElementById('id').scrollIntoView(true); 
+0

如何指定哪个项目?说listview有100个项目,我想显示#12?我需要为每一行创建一个唯一的ID吗? – LilMoke 2013-04-06 21:58:45

+0

你可以使用jquery来查找第12项,像$(“ul li:nth-​​child(12)”)[0] .scrollIntoView(true); – 2013-04-07 10:02:25