点击按钮,在转发器内的面板内获取字段

时间:2013-11-18 20:39:36

标签: asp.net panel repeater

我有一个项目中继器,显示用户已经出价的项目。这些项目包含在面板中,因为它们包含用于单独更新每个项目的出价的表单元素。我希望用户能够提交要更新的单个项目,并让我知道他们正在尝试更新哪个项目,以便在处理更新时忽略所有其他字段。

现在,我的转发器看起来像:

<asp:Repeater ID="itemRepeater" runat="server" onitemdatabound="itemRepeater_DataBound">
    <ItemTemplate>
        <!-- Auction Item MASTER-->
        <asp:Panel id="pnlDefaultButton" runat="server" DefaultButton="absenteeBtnBid">
            <div style="position: absolute; left: 0px; top: 0px; transform: translate(0px, 236px);" class="item auction_item firearm user_item isotope-item">
                <div class="item_image">
                    <asp:HyperLink ID="item_img_link" runat="server" Visible="False" EnableViewState="False">
                        <asp:Image ID="item_img" runat="server" Visible="False" EnableViewState="False" Width="224" />
                    </asp:HyperLink>
                </div>

                <div class="item_overlay">
                    <div class="item_buttons"> 
                        <a href="#" class="follow_button">Following</a>

                        <asp:Label ID="absenteeBidLabel" runat="server" Text="" CssClass="absenteePlaceBidLabel" AssociatedControlID="absenteeBid" style="font-size:11px;" Visible="false">
                            <asp:TextBox ID="absenteeBid" runat="server" Wrap="False" CssClass="absenteePlaceBid" placeholder="Enter Bid" />
                        </asp:Label>

                        <asp:TextBox ID="absenteeBidId" runat="server" Wrap="False" CssClass="timedPlaceBid" style="display:none;" Visible="false" />
                        <asp:TextBox ID="absenteeBidClose" runat="server" Wrap="False" CssClass="timedPlaceBid" style="display:none;" Visible="false" />
                        <asp:TextBox ID="absenteeBidSaleId" runat="server" Wrap="False" CssClass="timedPlaceBid" style="display:none;" Visible="false" />

                        <asp:Button runat="server" ID="absenteeBtnBid" cssClass="startbidding_button edit_button" OnClick="onclick_absenteeBid" Text="Edit Bid" />

                        <div class="bid_options"> 
                            <a href="#" class="bid_option first">Live Bid</a>
                            <a href="#" class="bid_option">Bid by Phone</a>
                            <asp:HyperLink ID="bid_withdraw" runat="server" CssClass="withdrawbid"></asp:HyperLink> 
                        </div>
                    </div>
                    <table class="item_bidstatus" border="0" cellpadding="0" cellspacing="0" width="190">
                        <tbody>
                            <tr>
                                <td class="status_label" width="50%">Bids:</td>
                                <td class="status_value" width="50%"><asp:Label ID="bid_count" runat="server" Text="0" /></td>
                            </tr>
                            <tr>
                                <td class="status_label">My Top Bid:</td>
                                <td class="status_value"><asp:Literal ID="bid_amount" runat="server"></asp:Literal></td>
                            </tr>
                            <tr>
                                <td class="status_label">Your Status:</td>
                                <td class="status_value status_low">LOW BID</td>
                            </tr>
                        </tbody>
                    </table>

                    <div class="item_description">
                        <h5>
                            <asp:HyperLink ID="labelLot" runat="server">Lot #<%# Eval("item_lot")%></asp:HyperLink> - <asp:HyperLink ID="item_title" runat="server"><%# Eval("item_title")%></asp:HyperLink>
                        </h5>
                        <asp:Label ID="labelEst" runat="server" Visible="false"></asp:Label>
                        <p class="item_details"><asp:Label ID="labelDesc" runat="server"><%# Eval("item_desc")%></asp:Label></p>
                        <a href="#">&gt; Item Details</a>
                    </div>
                </div>
                <table class="item_footer" width="100%">
                    <tbody>
                        <tr>
                            <td><div class="item_category"><asp:HyperLink ID="item_sale" runat="server"></asp:HyperLink></div></td>
                            <td><div class="item_daysleft">Bid left: <asp:Literal ID="bid_time" runat="server"></asp:Literal></div></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </asp:Panel>
        <!-- /Auction Item MASTER-->
    </ItemTemplate>
</asp:Repeater>

所以我的问题是如何让方法onclick_absenteeBid只查看提交提交的面板中的表单字段?或者我甚至在转发器中使用面板以正确的方式进行此操作?

1 个答案:

答案 0 :(得分:2)

这种方法没有错。您必须在按钮的单击事件中找到容器面板,并在其中查找控件。这是你如何做到的:

protected void onclick_absenteeBid(object sender, EventArgs e)
{
    Panel pnl = ((Button) sender).Parent as Panel;
    if (pnl != null)
    {
        //Access controls inside panel here like this:
        TextBox absenteeBidId = pnl.FindControl("absenteeBidId") as TextBox;

        if(absenteeBidId  != null)
        {
            string myAbsenteeBidId = absenteeBidId.Text;
        }


        //Access Repeater Item
        RepeaterItem itm = pnl.NamingContainer as RepeaterItem;
        if (itm != null)
        {
            // Do stuff
        }
    }
}