2017-12-26 25 views
-1

"장바구니에 담기"버튼을 클릭 할 때 제품을 추가하고 싶지만 중계기에서 어떻게 할 수 있습니까? onclick 이벤트를 생성하는 방법은 무엇입니까?C# - 리피터에서 클릭 이벤트를 생성하는 방법?

<div class="product"> 
    <div class="text"> 
      <h3><%#Eval("Name")%></h3> 
      <p style="text-align:center;"><b> <%#Eval("qty") %></b></p> 
      <p class="price">Rs.<%#Eval("Price") %></p> 
      <p class="buttons"> 
      <button runat="server" id="b1" onclick="b1_cl" class="btn btn-primary"><i class="fa fa-shopping-cart"></i>Add to cart</button> 
      </p> 
     </div> 
</div> 

답변

1

웹 양식

이 항목 당 하나 개의 버튼을 생성합니다. 리피터가 끝에 인덱스를 연결하기 때문에 HTML 레벨 ID는 고유하게 유지됩니다.

<asp:Repeater ID="Repeater1" runat="server" OnItemCreated="Repeater1_ItemCreated" > 
    <ItemTemplate> 
     <button type="submit" runat="server" id="myButton" class="btn btn-primary"> 
      <i class="fa fa-shopping-cart"></i>Add to cart 
     </button> 
    </ItemTemplate> 
</asp:Repeater> 

코드 뒤에

나중에 필요하므로 핸들러 및 항목 인덱스를 추가.

protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e) 
{ 
    var button = (HtmlButton)e.Item.FindControl("myButton"); 
    if (button != null) 
    { 
     button.Attributes["index"] = e.Item.ItemIndex.ToString(); 
     button.ServerClick += new EventHandler(MyButton_Click); 
    } 
} 

그리고 마지막으로 클릭 처리기

:

protected void MyButton_Click(object sender, EventArgs e) 
{ 
    string index = ((HtmlButton)sender).Attributes["index"]; 
} 

변수 index는 클릭 된 항목을 알려줍니다. 또 다른 옵션은 인덱스를 속성으로 설정하는 대신 처리기로 전달하는 것입니다.