如何找到置于转发器控件内的按钮的单击事件

时间:2016-03-31 11:42:13

标签: c# html asp.net

我在转发器里面有一个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>

4 个答案:

答案 0 :(得分:0)

检查以下内容,

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

在Code Behind中,

protected void btnTest_Click(object sender, EventArgs e)
{
//Your code
}

答案 1 :(得分:0)

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 
}

试试这个

答案 2 :(得分:0)

我认为您可以直接访问Button的Click事件,即使它在Repeater Control中也是如此。 这是我怎么做的.. 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();
        }

    }

希望这会对你有所帮助。

答案 3 :(得分:0)

而不是&#34; Button_Click&#34;,因为您在转发器中,您可能需要考虑让按钮触发项目事件并将您的代码放入项目事件中。如果您有多个按钮,则可以查看按钮的属性以了解它是哪一个。您还可以访问所点击的项目,以备不时之需。

T item = (T)Activator.CreateInstance(typeof(T));

P.S。对不起,这是VB.NET,而不是C#,但你可以通过使用Visual Studio中的下拉列表找到正确放置ShoesRepeater_ItemCommand代码的地方,然后使用e.CommandName,e.CommandArgument和e.Item.findControl来访问各种价值观。我碰巧使用Select Case语句来分隔不同的按钮&#39;代码。