在vb.net中获取确认值

时间:2012-11-07 10:17:23

标签: javascript asp.net vb.net

我在Repeater中有一个隐藏的asp按钮。在后面的VB.NET代码中,我使用Rerpeater_ItemCommand来获取Repeater中的click事件。检查用户是否已经在录制项目。如果是,他想开始一个新的,应该出现一个确认框,询问“你确定吗?”如何从确认中访问点击值?

<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事件和隐藏字段。但问题是,我不希望每次单击按钮时都出现确认框。仅当empRecs包含员工时。

感谢您的帮助

2 个答案:

答案 0 :(得分: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

HTH

答案 1 :(得分:0)

不确定我的问题是否正确,但我知道您希望能够确定何时选择了确认对话框中的“是”选项。您可以通过直接在标记上添加以下内容而不是代码隐藏来实现:

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

无论该按钮触发什么事件,在用户确认对话框之前都不会启动。