2012-11-07 74 views
2

我在Repeater中有一個隱藏的asp按鈕。在後面的VB.NET代碼中,我使用Rerpeater_ItemCommand來獲取Repeater中的click事件。有一個檢查用戶是否已經錄製了一個項目。如果是,他想開始一個新的,確認框應該出現問「你確定嗎?」我如何從確認中訪問點擊價值?背後在vb.net獲取確認值

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"> 
     <ItemTemplate> 

     <div class="tile user_view user_<%# Eval("employeeName") %>"> 
     <div class="tilesheight"></div> 
     <div class="element"> 

     <asp:Button ID="Button1" CssClass="hiddenbutton" runat="server" /> 


      Index: 
      <asp:Label ID="Label1" runat="server" 
       Text='<%# Eval("index") %>' /><br /> 
      <hr class="hr" /> 

      customer: 
      <asp:Label ID="CustomerLabel" runat="server" 
       Text='<%# Eval("customer") %>' /><br /> 
      <hr class ="hr" /> 
      order: 
      <asp:Label ID="OrderNoLabel" runat="server" 
       Text='<%# Eval("orderNo") %>' /><br /> 
      <asp:Label ID="DescriptionLabel" runat="server" 
       Text='<%# Eval("description") %>' /><br /> 
      <hr class="hr" /> 

     </div> 
     </div> 
     </ItemTemplate> 
     </asp:Repeater> 

代碼:

If empRecs.Contains(projects.Item(index.Text).employeeID) Then 

      'Catch index of recording order 
      i = empRecs.IndexOf(projects.Item(index.Text).employeeID) 


       Page.ClientScript.RegisterStartupScript(Me.GetType, "confirm", "confirm('Order " & empRecs(i + 2) & " already recording. Would you like to start a new one?')",True) 

       'If users clicks ok insertData() 

     End If 

其他的解決方案是使用Click事件和隱藏字段。但問題是,我不希望每次單擊按鈕時都會出現confirmmbox。只有當僱員包含僱員時。

感謝您的幫助

回答

1

你可以嘗試把以下

Proteted Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound 
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then 
    ''not sure where you get the empRecs and projects from 
    ''but you can get the data item bound to this iteration of the repeater thus 
    ''if you need it for the empRecs/projects bit 
    Dim data = DirectCast(e.Item.DataItem, TypeOfBoundData) 
    ''grab the button like this 
    dim but as Button = e.Item.FindControl("Button1") ''cant remember if that will work, if not try the next line 
    ''dim but = DirectCast(e.Item.Findcontrol("Button1"), Button) 
    ''then do your bit 
    If empRecs.Contains(projects.Item(index.Text).employeeID) Then 
     AddHandler but.Click, AddressOf Button1_Click 
     but.OnClientClick = "return confirm('Order " & empRecs(i + 2) & " already recording. Would you like to start a new one?')" 
    End If 
    End If 
End Sub 

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
    insertData() 
End Sub 

心連心

+0

在這裏發佈您的html標記代碼。 – ravidev

+0

更改標記原因,確認消息始終顯示在點擊,這正是我不想要的。我無法直接訪問按鈕,所以我嘗試過CType(e.CommandSource,Button).OnClientClick,但這不起作用。 – swine

0

不知道如果我收到了你的問題的權利,但我得到你希望能夠告訴的時候已經選擇確認對話框中的「是」選項。你可以通過添加直接在標記下,而不是隱藏代碼:

<asp:Button ID="Button1" CssClass="hiddenbutton" runat="server" OnClientClick="javascript:if(!confirm('Your confirm text here')) {return false;} /> 

無論事件是由按鍵觸發,不會火起來,直到用戶已經確認對話框。