如何为Repeater中的LinkBut​​ton执行AsyncPostBackTrigger

发布于 2024-12-25 02:12:52 字数 2946 浏览 0 评论 0原文

在我的页面中,中继器内有一个 LinkBut​​ton,但 UpdatePanel 找不到 AsyncPostBackTrigger 的 LinkBut​​ton。

这里是mycode.aspx

<asp:ScriptManager ID="Test1" runat="server" />
<asp:UpdatePanel ID="TestUpdate" runat="server" UpdateMode="Always">
<ContentTemplate>
<table width="100%">
<tr valign="top">
    <td width="50%">
        <asp:Repeater ID="productList" runat="server" onitemcommand="productList_ItemCommand">
        <HeaderTemplate>
        <ul type="disc">
        </HeaderTemplate>
        <ItemTemplate>
        <li>
            <asp:Label id="L1" runat="server" Text='<%# Eval("productName") %>'></asp:Label><br />
            Price:
            <asp:Label runat="server" Text='<%# Eval("productPrice") %>' ></asp:Label>&nbsp;Bath<br />
            <img alt="" src="Images/product/product<%# Eval("productID") %>.png" style="width: 200px; height: 130px" /><br />
            <asp:TextBox ID="num_product" runat="server" Text="0"></asp:TextBox><br />
            <asp:LinkButton ID="order_button" runat="server"><img alt="" src="~/Images/button/order.png" /></asp:LinkButton>
        </li>
        </ItemTemplate>
        <FooterTemplate>
        </ul>
        </FooterTemplate>
        </asp:Repeater> 
    <td>
    <span class="labelText">Order list</span>
        <asp:BulletedList ID="orderList" runat="server" BulletStyle="Numbered">
        </asp:BulletedList> 
    </td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>

这里是mycode.aspx.cs

protected void productList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //button
        /*LinkButton btn = new LinkButton();
        btn.ID = "order_button";
        btn.Click += LinkButton1_Click;
        Test1.RegisterAsyncPostBackControl(btn);*/

        LinkButton btn = (LinkButton)e.Item.FindControl("order_button");
        btn.Click += LinkButton1_Click;
        Test1.RegisterAsyncPostBackControl(btn);

            /*AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
            trigger.ControlID = btn.ClientID;
            trigger.EventName = "Click";
            TestUpdate.Triggers.Add(trigger);*/

    }
   protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //string name = ProductName1.Text.ToString();
        //int price = System.Convert.ToInt32(ProductPrice1.ToString(), 10);
        //int number = System.Convert.ToInt32(TextBox1.ToString(),10);
        //orderList.Items.Clear();
        //orderList.Items.Add(new ListItem(name));
        //ListItem product1 = new ListItem();
        //product1.Text = name;
        orderList.Items.Add("test");
    }

我尝试了很多方法,但是页面仍然刷新。您有什么建议吗?

In my page, I have an LinkButton inside repeater, but the UpdatePanel cannot find the LinkButton to AsyncPostBackTrigger.

Here is mycode.aspx

<asp:ScriptManager ID="Test1" runat="server" />
<asp:UpdatePanel ID="TestUpdate" runat="server" UpdateMode="Always">
<ContentTemplate>
<table width="100%">
<tr valign="top">
    <td width="50%">
        <asp:Repeater ID="productList" runat="server" onitemcommand="productList_ItemCommand">
        <HeaderTemplate>
        <ul type="disc">
        </HeaderTemplate>
        <ItemTemplate>
        <li>
            <asp:Label id="L1" runat="server" Text='<%# Eval("productName") %>'></asp:Label><br />
            Price:
            <asp:Label runat="server" Text='<%# Eval("productPrice") %>' ></asp:Label> Bath<br />
            <img alt="" src="Images/product/product<%# Eval("productID") %>.png" style="width: 200px; height: 130px" /><br />
            <asp:TextBox ID="num_product" runat="server" Text="0"></asp:TextBox><br />
            <asp:LinkButton ID="order_button" runat="server"><img alt="" src="~/Images/button/order.png" /></asp:LinkButton>
        </li>
        </ItemTemplate>
        <FooterTemplate>
        </ul>
        </FooterTemplate>
        </asp:Repeater> 
    <td>
    <span class="labelText">Order list</span>
        <asp:BulletedList ID="orderList" runat="server" BulletStyle="Numbered">
        </asp:BulletedList> 
    </td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>

Here is mycode.aspx.cs

protected void productList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //button
        /*LinkButton btn = new LinkButton();
        btn.ID = "order_button";
        btn.Click += LinkButton1_Click;
        Test1.RegisterAsyncPostBackControl(btn);*/

        LinkButton btn = (LinkButton)e.Item.FindControl("order_button");
        btn.Click += LinkButton1_Click;
        Test1.RegisterAsyncPostBackControl(btn);

            /*AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
            trigger.ControlID = btn.ClientID;
            trigger.EventName = "Click";
            TestUpdate.Triggers.Add(trigger);*/

    }
   protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //string name = ProductName1.Text.ToString();
        //int price = System.Convert.ToInt32(ProductPrice1.ToString(), 10);
        //int number = System.Convert.ToInt32(TextBox1.ToString(),10);
        //orderList.Items.Clear();
        //orderList.Items.Add(new ListItem(name));
        //ListItem product1 = new ListItem();
        //product1.Text = name;
        orderList.Items.Add("test");
    }

I tried many methods, but the page is still refresh. Do you have any suggestion?

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

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

发布评论

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

评论(3

居里长安 2025-01-01 02:12:52

Repeater 控件的 ItemCreated 事件内部向 ScriptManager 注册按钮。

//Inside ItemCreatedEvent
ScriptManager scriptMan = ScriptManager.GetCurrent(this);
LinkButton btn = e.Item.FindControl("order_button") as LinkButton;
if(btn != null)
{
    btn.Click += LinkButton1_Click;
    scriptMan.RegisterAsyncPostBackControl(btn);
}

Inside ItemCreated event of the Repeater control register the button with ScriptManager.

//Inside ItemCreatedEvent
ScriptManager scriptMan = ScriptManager.GetCurrent(this);
LinkButton btn = e.Item.FindControl("order_button") as LinkButton;
if(btn != null)
{
    btn.Click += LinkButton1_Click;
    scriptMan.RegisterAsyncPostBackControl(btn);
}
染柒℉ 2025-01-01 02:12:52

我有类似的问题,但我不想更新整个中继器,只想更新中继器之外的内容......所以我所做的是

1。添加中继器

<asp:Repeater ID="productList" runat="server">
  <!-- my repeater -->
<asp:Repeater>

2.添加包含可更新内容的更新面板和触发器

<asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
        <!-- when click on repeater's links, this content will be updated -->
    </ContentTemplate>
    <Triggers>
        <!-- trigger will be the repeater's links/btn that generate postback -->
        <asp:AsyncPostBackTrigger ControlID="productList" />
    </Triggers>
</asp:UpdatePanel>

I had similar problem, but I didn't want to update the whole repeater, only a content outside of the repeater... so what I did was

1. Add the repeater

<asp:Repeater ID="productList" runat="server">
  <!-- my repeater -->
<asp:Repeater>

2. Add the Update Panel with the updatable content, and the trigger

<asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
        <!-- when click on repeater's links, this content will be updated -->
    </ContentTemplate>
    <Triggers>
        <!-- trigger will be the repeater's links/btn that generate postback -->
        <asp:AsyncPostBackTrigger ControlID="productList" />
    </Triggers>
</asp:UpdatePanel>
此生挚爱伱 2025-01-01 02:12:52

将以下属性添加到包含中继器和链接按钮的页面指令也将起作用:

<%@ page ClientIDMode="AutoID" %>

我有一个需要异步工作和完整回发的控件,所以使用 ScriptManager.RegisterAsyncPostBackControl 对我来说不起作用。通过将控件(包含中继器和链接按钮)封装在 UpdatePanel 内,链接按钮将导致异步回发。如果没有更新面板,链接按钮将导致完整回发。

希望这对其他人有帮助。

Adding the following attribute to the page directive containing the repeater and linkbutton will also work:

<%@ page ClientIDMode="AutoID" %>

I had a control that needed to work both asynchronously and full postback, so using the ScriptManager.RegisterAsyncPostBackControl would not work for me. By enclosing the control (which contained a repeater and linkbutton) inside of an UpdatePanel, the linkbutton would cause an asynchronous postback. With no updatepanel, the linkbutton would cause a fullpostback.

Hope this helps someone else.

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