Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

asp.net - How to do AsyncPostBackTrigger for the LinkButton in the Repeater

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>&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>

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...