使用 Asp.Net ListView 和 Asp.Net Ajax 更新面板

发布于 2024-12-12 05:33:32 字数 2833 浏览 0 评论 0原文

您好,我希望这对某人来说很容易回答。首先,我试图将 ListView 绑定到 DataPager 并放入 ASP.NET UpdatePanel 来制作一个 AJAX 支持的寻呼机我的数据库中的记录。我抓住了一个 UpdatePanel 并放置了:

  1. SqlDataSource
  2. ListView - 具有 ItemTemplate 包括一个 ImageButton 中的其他几个 ASP.NET 控件
  3. 中的DataPager

> 以及ContentTemplate 。将 DataPager ID 分配给 UpdatePanel 触发器字段中的 AsyncPostbackTrigger 效果非常好。

我还想在 ImageButton Click 事件中执行完整的回发。但是,由于 ImageButton 位于 ListView 内部,因此 UpdataPanel 会导致部分回发。我尝试将 ImageButton 添加为 UpdatePanel PostBackTrigger,但 UpdatePanel 需要控件 ID,并且不会接受 ImageButton< /code> 因为它位于 ListView 内部。

如何向其传递 ListViewItemTemplate 内元素的控件 ID 并成功导致完整回发?

这是我的代码:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate> 
        <asp:SqlDataSource ... ></asp:SqlDataSource>
        <asp:ListView ID="ListViewForAlbums" runat="server" >
            <ItemTemplate>
                <div class="album">
                    <asp:ImageButton ID="albumPhoto" class="albumPhotosStyle" runat="server" ImageUrl="<%# Bind('albumPhotoPath') %>" AlternateText="<%# Bind('album_id') %>" ToolTip="<%# Bind('albumDetails') %>" onclick="albumPhotos_Click"  />
                    <div class="albumInfoHolder">

                    ...

                    </div>
                </div> <!-- End Album -->
            </ItemTemplate>
            <EmptyDataTemplate>
                <p>No Albums Yet. Check Back Soon!</p>
            </EmptyDataTemplate>
        </asp:ListView>

        <asp:DataPager ID="DataPagerForAlbums" runat="server" PagedControlID="ListViewForAlbums" PageSize="3" >
            <Fields>
                <asp:NextPreviousPagerField ShowFirstPageButton="True" FirstPageText="&laquo" ShowNextPageButton="False"  ShowPreviousPageButton="false" />
                <asp:NumericPagerField />
                <asp:NextPreviousPagerField ShowLastPageButton="True" LastPageText="&raquo" ShowPreviousPageButton="False"  ShowNextPageButton="false" />
            </Fields>
        </asp:DataPager>
        </p>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="DataPagerForAlbums" />
    </Triggers>
</asp:UpdatePanel> 

Hello there, I hope this will be easy for someone to answer. First off I was trying to make a ListView bound to a DataPager and put in an ASP.NET UpdatePanel to make an AJAX-powered pager for the records from my database. I grabbed an UpdatePanel and I placed:

  1. SqlDataSource
  2. ListView - having ItemTemplate including one ImageButton and several other ASP.NET controls in it
  3. DataPager

in the ContentTemplate. Assigning the DataPager ID to the AsyncPostbackTrigger in the UpdatePanel's trigger fields worked perfectly.

I also wanted to perform a full post-back in the ImageButton Click event. However, because the ImageButton is inside the ListView, the UpdataPanel causes a partial post-back. I tried adding the ImageButton as an UpdatePanel PostBackTrigger, but the UpdatePanel demands a control ID, and won't accept the ImageButton since it is inside the ListView.

How do I pass it a control ID for an element inside ItemTemplate of the ListView and successfully cause a full post back?

Here is my code:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate> 
        <asp:SqlDataSource ... ></asp:SqlDataSource>
        <asp:ListView ID="ListViewForAlbums" runat="server" >
            <ItemTemplate>
                <div class="album">
                    <asp:ImageButton ID="albumPhoto" class="albumPhotosStyle" runat="server" ImageUrl="<%# Bind('albumPhotoPath') %>" AlternateText="<%# Bind('album_id') %>" ToolTip="<%# Bind('albumDetails') %>" onclick="albumPhotos_Click"  />
                    <div class="albumInfoHolder">

                    ...

                    </div>
                </div> <!-- End Album -->
            </ItemTemplate>
            <EmptyDataTemplate>
                <p>No Albums Yet. Check Back Soon!</p>
            </EmptyDataTemplate>
        </asp:ListView>

        <asp:DataPager ID="DataPagerForAlbums" runat="server" PagedControlID="ListViewForAlbums" PageSize="3" >
            <Fields>
                <asp:NextPreviousPagerField ShowFirstPageButton="True" FirstPageText="«" ShowNextPageButton="False"  ShowPreviousPageButton="false" />
                <asp:NumericPagerField />
                <asp:NextPreviousPagerField ShowLastPageButton="True" LastPageText="»" ShowPreviousPageButton="False"  ShowNextPageButton="false" />
            </Fields>
        </asp:DataPager>
        </p>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="DataPagerForAlbums" />
    </Triggers>
</asp:UpdatePanel> 

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

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

发布评论

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

评论(2

巡山小妖精 2024-12-19 05:33:32

您可以使用 ScriptManager 将控件注册为回发控件。在 ItemDataBound 事件中执行类似的操作:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ImageButton button = e.Item.FindControl("ImageButton1") as ImageButton;
    if (button != null)
    {
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(button);            
    }
}

You can use the ScriptManager to register the control as a postback control. Do something like this in the ItemDataBound event:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ImageButton button = e.Item.FindControl("ImageButton1") as ImageButton;
    if (button != null)
    {
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(button);            
    }
}
岁月静好 2024-12-19 05:33:32

使用 ItemDataBound ListView 的事件获取 ImageButton 并使用 ScriptManager 的 RegisterPostBackControl 方法将其注册为回发控件。

Use a ItemDataBound ListView's event to get ImageButton and register it as a postback control with ScriptManager's RegisterPostBackControl method.

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