繁体   English   中英

如何查找放置在转发器控件中的按钮的单击事件

[英]How to find click event of button placed inside a repeater control

我在转发器内部有一个div,在div内部有一个ASP按钮,我想访问按钮单击的按钮单击事件,这是我的html代码

<asp:Repeater ID="ShoesRepeater" runat="server">
    <ItemTemplate>
        <div class="col-md-4 grid-stn simpleCart_shelfItem">
            <!-- normal -->
            <div class="ih-item square effect3 bottom_to_top">
                <div class="bottom-2-top">
                    <div class="img">
                        <img src="images/<%#Eval("ImagePath") %>" alt="/" class="img-responsive gri-wid">
                    </div>
                    <div class="info">
                        <div class="pull-left styl-hdn">
                            <h3><%#Eval("Category") %></h3>
                        </div>
                        <div class="pull-right styl-price">
                            <p><a href="#" class="item_add"><span class="glyphicon glyphicon-shopping-cart grid-cart" aria-hidden="true"></span><span class=" item_price"><%#Eval("Price") %></span></a></p>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                </div>
            </div>
            <!-- end normal -->
            <div class="quick-view">
                <a href="Single.aspx"><span class="MenuItem" runat="server" onclick="LogOff_Click">Quick view</span></a>
            </div>
        </div>

    </ItemTemplate>
</asp:Repeater>

检查下面,

<asp:Repeater ID="ShoesRepeater" runat="server">
    <ItemTemplate>
        <div class="col-md-4 grid-stn simpleCart_shelfItem">
            <!-- normal -->
            <div class="ih-item square effect3 bottom_to_top">
                <div class="bottom-2-top">
                    <div class="img">
                        <img src="images/<%#Eval("ImagePath") %>" alt="/" class="img-responsive gri-wid">
                    </div>
                    <div class="info">
                        <div class="pull-left styl-hdn">
                            <h3><%#Eval("Category") %></h3>
                        </div>
                        <div class="pull-right styl-price">
                            <p><a href="#" class="item_add"><span class="glyphicon glyphicon-shopping-cart grid-cart" aria-hidden="true"></span><span class=" item_price"><%#Eval("Price") %></span></a></p>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                </div>
            </div>
            <!-- end normal -->
            <div class="quick-view">
                <a href="Single.aspx"><span class="MenuItem" runat="server" onclick="LogOff_Click">Quick view</span></a>
                <!-- ADD BUTTON HERE -->
                <asp:Button ID="btnTest" Text='Test' runat="server" OnClick="btnTest_Click" />
            </div>
        </div>

    </ItemTemplate>
</asp:Repeater>

在后面的代码中

protected void btnTest_Click(object sender, EventArgs e)
{
//Your code
}
void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == Repeater1.AlternatingItem || e.Item.ItemType == Repeater1.Item)
    {
        var btn = e.Item.FindControl("btnSave") as Button;
        if (btn != null)
        {  // adding button event 
            btn.Click += new EventHandler(btn_Click);
        }
    }
}

void btn_Click(object sender, EventArgs e)
{
 //write your code 
}

尝试这个

我认为即使在Repeater Control中,您也可以直接访问Button的Click Event。这是我的方法。ASPX代码

 <asp:Repeater runat="server" ID="REPCOURSER">
                    <ItemTemplate>

                        <div class="thumbnail">    
                            <div class="descrip_progms">
                                <h3 class="text-danger">
                                    <asp:Button CommandArgument='<%#Eval("Cr_id") %>' class="btn btn-border yellow-2" Text="Add To My Courses" ID="btnAddToCourse" runat="server" OnClick="btnAddToCourse_OnClick_unat_" />
                                </h3>
                                <hr class="divider" />
                                <div style="background: #B6DA7C;height: 39px; padding: 9px;">
                                <div class="col-md-6 text-left" style="font-size: 15px;">
                                    <i class="fa fa-book"></i>
                                    <asp:Label ID="LblCrsName" runat="server" Text='<%# Eval("Cr_title") %>' />
                                </div>
                                <div class="col-md-6 text-right" style="font-size: 15px;">
                                    <i class="fa fa-inr "></i>
                                    <asp:Label ID="lblprice" CssClass="" runat="server" Text='<%# (Convert.ToInt32(Eval("Cr_price").ToString())==0) ? "Free":Eval("Cr_price").ToString()+" Rs."  %>' />
                                </div>
                                    </div>
                            </div>
                        </div>
                        </div>

                        <br />
                    </ItemTemplate>
                </asp:Repeater>

后面的代码

 protected void btnAddToCourse_OnClick_unat_(object sender, EventArgs e)
    {
        if (Session["Email"] != null)
        {
            foreach (RepeaterItem re in REPCOURSER.Items)
            {
                Label rs = re.FindControl("lblprice") as Label;
                Button Addcourse = re.FindControl("btnAddToCourse") as Button;
                if (rs != null)
                { 
                    if (rs.Text.ToLower() == "free" && Addcourse != null)
                    {
                        Courses courses = new Courses();
                        int res = courses.EnrollFreeCourses(new Course()
                         {
                             CId = Convert.ToInt32(Addcourse.CommandArgument.ToString()),
                         }, new Users()
                         {
                             id = Convert.ToInt64(Session["Id"].ToString())
                         });

                        Addcourse.Attributes.Add("disabled", "disabled");
                        Addcourse.Text = "Course Added To My Courses";
                    }
                    else
                    {
                        //Procedure for paid courses

                    }
                }

            }
        }
        else
        {
            Master.LoginPopupExtender.Show();
        }

    }

希望这会帮助你。

由于您位于中继器中,因此您可能要考虑让按钮触发项目事件并将代码放入项目事件中,而不是“ Button_Click”。 如果您有多个按钮,则可以查看按钮的属性以了解它是哪个按钮。 如果需要,您还可以访问单击的项目。

Protected Sub ShoesRepeater_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles ShoesRepeater.ItemCommand
  Select Case e.CommandName  'This is the clicked button's CommandName
    Case "b1"
      'Do stuff for Button1
      'e.CommandArgument is the clicked button's CommandArgument
      'e.Item.FindControl("lblPrice") allows you to access other controls in this same Repeater item as the button that was clicked.
    Case "b2"
      'Do stuff for Button2
  End Select
  'There's a decent chance you'll want to rebind the grid when you're done.
End Sub

PS对不起,那是VB.NET,不是C#,但是您可以通过使用Visual Studio中的下拉列表找到正确的位置来放置ShoesRepeater_ItemCommand的代码,然后使用e.CommandName,e.CommandArgument和e.Item.findControl来访问各种值。 我只是碰巧使用Select Case语句来分隔不同按钮的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM