如何使用 ScrollTo jQuery 插件滚动到 ASP.NET 中转发器中的特定行?

发布于 2024-09-08 08:42:33 字数 2105 浏览 4 评论 0原文

我有一个简单的中继器,如下所示:

 <asp:Repeater runat="server" ID="rptOptions" OnItemDataBound="rptOptions_ItemDataBound">
     <HeaderTemplate>
         <thead>
             <tr>
                 <td class="GridHeader">Account</td>    
                 <td class="GridHeader">Margin</td>  
                 <td class="GridHeader">Symbol</td>  
                 <td class="GridHeader">Usymbol</td>  
             </tr>
         </thead>
     </HeaderTemplate>
     <ItemTemplate>
         <tbody>
             <tr runat="server" ID="trOption">
                 <td class="GridRow"><asp:Label runat="server" ID="lblOptionAccount"></asp:Label></td>
                 <td class="GridRow"><asp:Label runat="server" ID="lblOptionMargin"></asp:Label></td>
                 <td class="GridRow"><asp:Label runat="server" ID="lblOptionSymbol"></asp:Label></td>
                 <td class="GridRow"><asp:Label runat="server" ID="lblOptionUsymbol"></asp:Label></td>                         
             </tr>
         </tbody>
     </ItemTemplate>
 </asp:Repeater>

现在,在我的代码隐藏中,我有一个被触发的事件,该事件应该在数据库中添加/插入一行。发生这种情况后,我从数据库中重新获取新的选项列表,并将它们重新绑定到中继器。这发生在更新面板内,因此列表会立即为用户刷新。

protected void lbtnAddOptionSave_Click(object sender, EventArgs e)  
{
    SelectedOption = new Option()
    {
        Account = txtAddOptionAccountNumber.Text,
        Margin = chkAddOptionMargin.Checked,
        Symbol = txtAddOptionSymbol.Text,
        Usymbol = txtAddOptionUsymbol.Text,
    };

    Presenter.OnAddOption(); // Insert new option into database
    RefreshOptions(); // Re-get list of options, bind them to repeater
}

现在,我喜欢做的就是使用 jQuery ScrollTo 插件直接滚动到新添加的行。

在 jQuery 插件中调用 ScrollTo() 方法以便我滚动到刚刚添加的特定行的最佳方法是什么?无论如何,我是否可以在 ItemTemplate 中标记我的行,以便我可以轻松地通过 jQuery 选择要滚动到的元素?

理想情况下,在 RefreshOptions() 之后,我想执行 ScrollTo 函数以向下滚动到新行。

I have a simple repeater that looks like:

 <asp:Repeater runat="server" ID="rptOptions" OnItemDataBound="rptOptions_ItemDataBound">
     <HeaderTemplate>
         <thead>
             <tr>
                 <td class="GridHeader">Account</td>    
                 <td class="GridHeader">Margin</td>  
                 <td class="GridHeader">Symbol</td>  
                 <td class="GridHeader">Usymbol</td>  
             </tr>
         </thead>
     </HeaderTemplate>
     <ItemTemplate>
         <tbody>
             <tr runat="server" ID="trOption">
                 <td class="GridRow"><asp:Label runat="server" ID="lblOptionAccount"></asp:Label></td>
                 <td class="GridRow"><asp:Label runat="server" ID="lblOptionMargin"></asp:Label></td>
                 <td class="GridRow"><asp:Label runat="server" ID="lblOptionSymbol"></asp:Label></td>
                 <td class="GridRow"><asp:Label runat="server" ID="lblOptionUsymbol"></asp:Label></td>                         
             </tr>
         </tbody>
     </ItemTemplate>
 </asp:Repeater>

Now, in my code-behind I have an event which is fired that is supposed to add/insert a row into the database. After this happens, I re-grab the new list of options from the database and re-bind them to the repeater. This takes place inside an update panel so the list refreshes right away for the user.

protected void lbtnAddOptionSave_Click(object sender, EventArgs e)  
{
    SelectedOption = new Option()
    {
        Account = txtAddOptionAccountNumber.Text,
        Margin = chkAddOptionMargin.Checked,
        Symbol = txtAddOptionSymbol.Text,
        Usymbol = txtAddOptionUsymbol.Text,
    };

    Presenter.OnAddOption(); // Insert new option into database
    RefreshOptions(); // Re-get list of options, bind them to repeater
}

Now, what I would ~love~ to do, is use the jQuery ScrollTo plugin to scroll straight to the newly added row.

What would be the best way to call the ScrollTo() method in the jQuery plugin so I scroll to that particular row that was just added? Is there anyway I can mark my rows in my ItemTemplate so I can easily select an element to scroll to via jQuery?

Ideally, right after RefreshOptions() I would like to execute the ScrollTo function to scroll down to the new row.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浴红衣 2024-09-15 08:42:33

如果您知道该行的客户端 ID(可以获取),则只需调用即可相对轻松

$(document).scrollTo("#<row-id-here>", 800);

If you know the client side Id of the row (which you can get), its relatively painless to simply call

$(document).scrollTo("#<row-id-here>", 800);
指尖上得阳光 2024-09-15 08:42:33

当您将对象添加到数据库时(或之后),获取新插入对象的 ID。修改中继器以包含带有 Visible="false" 的标签,以便它不会呈现给客户端。挂钩 ItemDataBound 事件并根据您获取的 ID 检查每个标签。当您找到匹配的行时,您可以获得该行的 id,然后您就可以将其用于滚动参数。

注意:其他数据绑定控件具有 DataKey 属性,该属性可用于对象的 id 并使其更简单。不确定此时您是否与 Repeater 相关,但 GridView 或 ListView 可能值得研究。

When you add the object to the database (or just after that), grab the id of the newly inserted object. Modify the repeater to include a Label with Visible="false" so that it doesn't get rendered to the client. Hook into the ItemDataBound event and check each label against the id you've grabbed. When you find the matching row, you can get the id of the row and then you'll be able to use for the scrolling parameter.

Note: Other data-bound controls have a DataKey property which could be used for the id of the object and make this a bit simpler. Not sure if you're tied to the Repeater at this point, but a GridView or ListView could be worth looking into.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文