未调用时触发事件

时间:2014-07-28 12:43:08

标签: vb.net events

我正在使用VS 2008在VB中进行一个项目,并且在没有调用它们时遇到麻烦。

我有一个名为 txtIMItem 的文本框和一个名为 btnIMCancel 的按钮。当我在文本框中按Enter键时, textChanged 事件会触发,这很好。在完成执行后(我在调试中跟踪),它会立即跳转到 btnIMCancel_Click 事件,该事件会重定向页面,导致页面的其余部分无法正常运行。似乎与这两个事件没有任何联系,我不确定这种情况发生的原因。以下是我的代码。任何帮助,将不胜感激。谢谢。

 Partial Class EditItemsMold
    Inherits System.Web.UI.Page
    Dim mMIIX As New clsMold_Item_Insert_Xref

    Protected Sub PopulateAllMolds()
        Dim oMld As New clsMolds, oMld2 As New clsMolds, li As New ListItem

        If Not oMld.ReadAll(True) Then
            lblIMCaption.Text = oMld.LastError
        End If
        ddlIMMold.Items.Clear()
        li = New ListItem
        li.Text = "Select"
        li.Value = 0
        ddlIMMold.Items.Add(li)
        For Each oMld2 In oMld.mCollection
            li = New ListItem
            li.Text = oMld2.MoldNumber
            li.Value = oMld2.MoldID
            ddlIMMold.Items.Add(li)
        Next
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then

        Else
            PopulateAllMolds()
            ddlIMMold.Enabled = False
        End If
    End Sub

    Protected Sub txtIMItem_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtIMItem.TextChanged
        Dim oI As New clsItems

        oI.ItemNumber = txtIMItem.Text
        If Not oI.Read(oI.ItemNumber, True) Then
            txtIMItem.ForeColor = Drawing.Color.Red
            lblIMCaption.Text = "Item Cannot Be Found"
            lblIMCaption.ForeColor = Drawing.Color.Red
            btnIMUpdate.Enabled = False
            Exit Sub
        Else
            txtIMItem.ForeColor = Drawing.Color.Black
            lblIMCaption.Text = "Item Found"
            lblIMCaption.ForeColor = Drawing.Color.Black
            lblIMDescription.Text = oI.Description
            btnIMUpdate.Enabled = True
            ddlIMMold.Enabled = True
        End If
    End Sub

    Protected Sub ddlIMMold_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlIMMold.SelectedIndexChanged
        Dim oIns As New clsInserts, oIns2 As New clsInserts
        Dim li As New ListItem
        If Not oIns.ReadAllForMold(ddlIMMold.SelectedValue) Then
            lblIMCaption.Text = "Could not read inserts for mold: " & oIns.LastError
            Exit Sub
        End If
        For Each oIns2 In oIns.mCollection
            li = New ListItem
            li.Text = oIns2.InsertNumber
            li.Value = oIns2.InsertID
            ddlIMInsert.Items.Add(li)
            ddlIMInsert.ClearSelection()
        Next
        If oIns2.InsertID = Nothing Then
            ddlIMInsert.Items.Clear()
            li = New ListItem
            li.Text = "No Insert"
            li.Value = 0
            ddlIMInsert.Items.Add(li)
            ddlIMInsert.SelectedValue = 0
        End If
        ddlIMInsert.Enabled = True
    End Sub

    Protected Sub btnIMUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIMUpdate.Click
        mMIIX.ItemNumber = txtIMItem.Text
        mMIIX.MoldID = ddlIMMold.SelectedValue
        mMIIX.InsertID = ddlIMInsert.SelectedValue
        mMIIX.InsertName = ddlIMInsert.SelectedItem.Text

        lblIMCaption.Text = ""
        If Not mMIIX.SaveUpdate Then
            lblIMCaption.Text = "Could not update Item's Mold record: " & mMIIX.LastError
            Exit Sub
        End If
        Response.Redirect("ScheduleJobs.aspx")
    End Sub

    Protected Sub btnIMCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIMCancel.Click
        Response.Redirect("ScheduleJobs.aspx")
    End Sub

    Protected Sub btnCatch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCatch.Click
        Dim i As Int16
        i = 0
    End Sub
End Class

这是vb代码:

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="EditItemsMold.aspx.vb" Inherits="EditItemsMold" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head id="Head1" runat="server">
    <link rel="stylesheet" type="text/css" href="main.css" />
    <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="scripts.js"></script>
    <title>Edit Item's Mold</title>
</head>
<body style="background-color:inherit;">
    <form id="frmEditItemsMold" runat="server">
        <table>
            <tr>
                <td colspan="4" class="alignCenter"><asp:label ID="lblIMCaption" runat="server" Text=""></asp:label></td>
            </tr>
            <tr>  
                <td class="alignLeft"><asp:Label  ID="lblIMItem" runat="server" Text="Item: "></asp:Label></td>
                <td><asp:TextBox ID="txtIMItem" runat="server"></asp:TextBox></td>
                <td><asp:Label ID="lblIMMold" runat="server" Text="Uses Mold: "></asp:Label><asp:DropDownList ID="ddlIMMold" runat="server" AutoPostBack="true"></asp:DropDownList></td>
                <td><asp:Label ID="lblIMInsert" runat="server" Text="And Insert: "></asp:Label><asp:DropDownList ID="ddlIMInsert" runat="server" Enabled="false" AutoPostBack = "true"> </asp:DropDownList></td>
            </tr>
            <tr>
                <td class="alignCenter" colspan="4"><asp:label ID="lblIMDescription" runat="server" Text=""></asp:label></td>
            </tr>
            <tr>
                <td colspan="4" class="alignRight"><asp:Button ID="btnIMUpdate" runat="server" Text="Update" Enabled="false" /><asp:Button ID="btnIMCancel" runat="server" Text="Cancel" /></td>         
                <td><asp:Button ID="btnCatch" runat="server" Visible="false"></asp:Button></td>
                </tr>
        </table>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

您可能已将“取消”按钮设置为表单或容器的默认按钮...?