怎样给内嵌套的Repeater中的Button添加事件?

时间:2021-09-22 10:03:10
我有两个Repeater,外面那个叫“rptHotel”,里面那个是“rptRoom”,rptRoom根据rptHotel绑定不同数据,rptRoom每行都有个btnBook按钮,请问如何给这个btnBook按钮添加上onClick事件?
<div id="collapsiblepanel">
                <asp:Repeater ID="rptHotel" runat="server" DataSourceID="dsHotelList" 
                    onitemdatabound="rptHotel_ItemDataBound">
                    <HeaderTemplate>
                        <table width="100%">
                            <thead>
                                <tr>
                                    <th>
                                        酒店名称
                                    </th>
                                    <th>
                                        最低价
                                    </th>
                                    <th>
                                        展开/收拢
                                    </th>
                                </tr>
                            </thead>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tbody>
                            <tr style="background-color: #33CCFF">
                                <td>
                                    <%#Eval("HotelName") + "(联系电话:" + Eval("Telephone")+")"%>
                                </td>
                                <td>
                                    最低<%#Eval("LowestPrice")%>元起
                                </td>
                                <td>
                                    <asp:Label ID="lblDetails" runat="server" Text="详情"></asp:Label>
                                    <asp:Image ID="imgbtnExpansion" runat="server" ImageUrl="~/Images/zhankai.jpg" />
                                </td>
                            </tr>
                            <tr>
                                <td colspan="3">
                                    <asp:Panel ID="pnlDetails" runat="server">
                                        <div>
                                            <asp:Label ID="lblHotelId" runat="server" Text='<%#Eval("id") %>'></asp:Label></div>
                                        <div>
                                            <%#Eval("Description")%></div>
                                        <div>
                                            <%# Eval("Address") %></div>
                                        <div>
                                            <asp:Repeater ID="rptRoom" runat="server" OnItemCommand="rptRoom_ItemCommand">
                                                <HeaderTemplate>
                                                    <table width="100%">
                                                        <thead>
                                                        </thead>
                                                </HeaderTemplate>
                                                <ItemTemplate>
                                                    <tbody>
                                                        <tr>
                                                            <td>
                                                                <%#Eval("RoomNo") %>
                                                            </td>
                                                            <td>
                                                                <%#Eval("RoomName") %>
                                                            </td>
                                                            <td>
                                                                <%#Eval("TypeName") %>
                                                            </td>
                                                            <td>
                                                                <%#Eval("Price") %>
                                                            </td>
                                                            <td>
                                                                <%#Eval("CheckinTime") %>
                                                            </td>
                                                            <td>
                                                                <%#Eval("LeaveTime") %>
                                                            </td>
                                                            <td>
                                                                <%--<asp:Button ID="btnBook" runat="server" Text="预订" OnClick="btn_Click" />--%>
                                                                <asp:Button ID="btnBook" runat="server" Text="Button" onclick="btnBook_Click" />
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </ItemTemplate>
                                                <FooterTemplate>
                                                    <tfoot>
                                                    </tfoot>
                                                    </table>
                                                </FooterTemplate>
                                            </asp:Repeater>
                                        </div>
                                    </asp:Panel>
                                    <asp:CollapsiblePanelExtender ID="CollapsiblePanelExtender1" runat="server" TargetControlID="pnlDetails"
                                        Collapsed="True" ImageControlID="Image1" ExpandedImage="~/Images/shousuo.jpg"
                                        CollapsedImage="~/images/zhankai.jpg" AutoCollapse="False" AutoExpand="False"
                                        ScrollContents="false" CollapsedSize="0" ExpandedSize="0" ExpandControlID="imgbtnExpansion"
                                        CollapseControlID="imgbtnExpansion">
                                    </asp:CollapsiblePanelExtender>
                                </td>
                            </tr>
                        </tbody>
                    </ItemTemplate>
                    <FooterTemplate>
                        <tfoot>
                        </tfoot>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
                <asp:SqlDataSource ID="dsHotelList" runat="server" ConnectionString="<%$ ConnectionStrings:dbHotelOnlineConnectionString %>">
                </asp:SqlDataSource>
                <%--<asp:Button ID="btnBook" runat="server" Text="Button" onclick="btnBook_Click" />--%>
<%--                <asp:SqlDataSource ID="dsRoom" runat="server" ConnectionString="<%$ ConnectionStrings:dbHotelOnlineConnectionString %>"
                    SelectCommand="SELECT * FROM [RoomList]"></asp:SqlDataSource>--%>
            </div>


        protected void rptHotel_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            bool flag = true;
            HotelDataOperate hdo = new HotelDataOperate();
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rpt = e.Item.FindControl("rptRoom") as Repeater;
                DataRowView rowv = (DataRowView)e.Item.DataItem;
                int typeid = Convert.ToInt32(rowv["id"]);
                IEnumerable<RecordList> lst=hdo.getRoomList(typeid);
                rpt.DataSource = lst;
                rpt.DataBind();
                foreach (RepeaterItem itm in rpt.Items)
                {
                    Button btn = (Button)itm.FindControl("btnBook");//确认按钮
                    foreach (RecordList rrd in lst)
                    {
                        if (flag)
                        {
                            btn.Text = "留有客房,欢迎预订";
                            btn.Enabled = true;
                            flag = !flag;
                        }
                        else
                        {
                            btn.Text = "客房已满,不可预订";
                            btn.Enabled = false;
                            flag = !flag;
                        }
                    }
                }
            }
        }

        protected void btnBook_Click(object sender, EventArgs e)
        {
            string temp = ((Button)sender).Text;
        }


        protected void rptRoom_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }


拜托各位大神了,初来乍到,菜鸟一只,也没有多少分,还希望大神们海涵!
多谢!

8 个解决方案

#1


http://www.ie512.com/news.aspx?id=205&no=1

参与例子。打开可能有点慢

#2


就跟普通的button按钮添加事件一样啊。你可以再repeater外面拉个button,然后双击,记住它的代码,然后仿造它的代码格式加到你要的button上就可以了。

#3


一直报错,只要点击那个按钮就报错了。真不知道该怎么办了。
引用 2 楼 godhelpmea 的回复:
就跟普通的button按钮添加事件一样啊。你可以再repeater外面拉个button,然后双击,记住它的代码,然后仿造它的代码格式加到你要的button上就可以了。

#4


我也这么做了呀,我在外面拖了一个button并双击,然后把button移到了我的子repeater中去。
引用 1 楼 msdnxgh 的回复:
http://www.ie512.com/news.aspx?id=205&amp;no=1

参与例子。打开可能有点慢

#5


报啥错

#6


把错误信息贴出来

#7


Server Error in '/' Application.

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.]
   System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +8653142
   System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +113
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +35
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

#8


页面指令改一下
<%@ Page Language="C#" ... EnableEventValidation="false" %>

#1


http://www.ie512.com/news.aspx?id=205&no=1

参与例子。打开可能有点慢

#2


就跟普通的button按钮添加事件一样啊。你可以再repeater外面拉个button,然后双击,记住它的代码,然后仿造它的代码格式加到你要的button上就可以了。

#3


一直报错,只要点击那个按钮就报错了。真不知道该怎么办了。
引用 2 楼 godhelpmea 的回复:
就跟普通的button按钮添加事件一样啊。你可以再repeater外面拉个button,然后双击,记住它的代码,然后仿造它的代码格式加到你要的button上就可以了。

#4


我也这么做了呀,我在外面拖了一个button并双击,然后把button移到了我的子repeater中去。
引用 1 楼 msdnxgh 的回复:
http://www.ie512.com/news.aspx?id=205&amp;no=1

参与例子。打开可能有点慢

#5


报啥错

#6


把错误信息贴出来

#7


Server Error in '/' Application.

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.]
   System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +8653142
   System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +113
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +35
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

#8


页面指令改一下
<%@ Page Language="C#" ... EnableEventValidation="false" %>