用户控件干扰了父页面的按钮

时间:2019-01-14 18:58:55

标签: asp.net vb.net user-controls

我已用用户控件替换了我网站上的一部分代码。原始代码和新控件将通过一个略有不同的方式来计算值;它并不总是必需的。

将新控件添加到页面中而不使用时,父页面的“下一个”按钮会将浏览器滚动到页面顶部,但从断点来看,从不执行回退操作。如果使用了控件,即单击了“计算”按钮,则“下一步”按钮将按预期工作。如果我从页面上删除该元素,则“下一步”按钮将按预期工作。

在使用用户开发工具之前和之后,我已经使用开发工具检查了“下一步”按钮,看不出有什么区别。

该控件在其他页面上使用并且执行没有问题,但是在这种情况下,需要计算值。

什么可能导致自定义用户控件干扰使用该控件的页面上的按钮?

编辑:发布请求的代码

ascx代码

BikeGroupDropdowns是一个自定义下拉列表

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="SpringCalculator.ascx.vb" Inherits="RacetechSite4.SpringCalculator" %>

<%@ Register Src="~/UserControls/BikeGroupDropdown.ascx" TagName="BikeOption" TagPrefix="uc" %>


<table style="border-spacing:1em;">
<uc:BikeOption runat="server" ID="ridingType" GroupId="1" />
<uc:BikeOption runat="server" ID="age" GroupId="2" />
<uc:BikeOption runat="server" ID="skillLevel" GroupId="3" />
<uc:BikeOption runat="server" ID="stiffPref" GroupId="5" />
<uc:BikeOption runat="server" ID="height" GroupId="6" />
<uc:BikeOption runat="server" ID="gasTank" GroupId="7" />  
<uc:BikeOption runat="server" ID="tireStiff" GroupId="8" />
<uc:BikeOption runat="server" ID="hfrSep" GroupId="10" />
<uc:BikeOption runat="server" ID="aarms" GroupId="11" />
<uc:BikeOption runat="server" ID="raceSport" GroupId="12" />
<uc:BikeOption runat="server" ID="frontRebSep" GroupId="13" />
<uc:BikeOption runat="server" ID="springConversion" GroupId="16" />
<tr>
    <td>
    <asp:Label runat="server" Width="125" Text="Rider Weight (without gear):" />
    </td><td>        
    <asp:TextBox runat="server" ID="txtRiderWeight" Width="50"/> 
    <asp:RangeValidator runat="server" Type="Integer" MinimumValue="40" MaximumValue="500" ControlToValidate="txtRiderWeight" />
    <asp:RequiredFieldValidator runat="server" ControlToValidate="txtRiderWeight" ErrorMessage="Rider Weight Required">*</asp:RequiredFieldValidator>

    <asp:RadioButtonList ID="rblWeightType" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" CellPadding="5">
    <asp:ListItem Text="lbs" Value="lbs" Selected="True" style="margin-right: 10px"></asp:ListItem>
    <asp:ListItem Text="kg" Value="kg"></asp:ListItem>
    </asp:RadioButtonList>
    </td>
</tr>
<tr>
    <td colspan="2">
    <asp:Label runat="server" ID="lblRiderWeightError" CssClass="ModifierErr" />
    </td>
</tr>

<asp:Panel runat="server" ID="pnlBikeWeight" Visible="false">
<tr>
    <td>
    <label runat="server" text="Bike Weight (semi-wet):" />
    </td><td>
    <asp:TextBox runat="server" ID="txtBikeWeight" />
    </td>
</tr>
</asp:Panel>
<tr></tr>       
<tr>
    <td colspan="2">
    <h3 style="text-align:left;margin-top:15px">LOWERING</h3>
    Leave blank unless you are lowering the vehicle.<br />
    Flat Track is already lowered (leave blank).
    </td>
</tr>
<tr>
    <td>
    <asp:Label runat="server" Width="125px" text="Fork Lowered Distance:"/>
    </td><td>
    <asp:TextBox runat="server" ID="txtFkLowDist" Width="50" />
    &nbsp;
    <asp:RadioButtonList ID="rblFkDistType" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
        <asp:ListItem Text="in" Value="in" Selected="True" style="margin-right: 10px"></asp:ListItem>
        <asp:ListItem Text="mm" Value="mm"></asp:ListItem>
    </asp:RadioButtonList>
    </td>
</tr>
<tr>
    <td colspan="2">
    <asp:Label runat="server" ID="lblFkLowDistError" CssClass="ModifierErr"/>
    </td>
</tr>
<tr>
    <td>
    <asp:Label runat="server" Width="125" Text="Shock Lowered Distance:" />
    </td><td>
    <asp:TextBox runat="server" ID="txtSkLowDist" Width="50" />
    &nbsp;
    <asp:RadioButtonList ID="rblSkDistType" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
    <asp:ListItem Text="in" Value="in" Selected="True" style="margin-right: 10px"></asp:ListItem>
    <asp:ListItem Text="mm" Value="mm"></asp:ListItem>
    </asp:RadioButtonList>
    </td>
</tr>
<tr>
    <td>
    <asp:Label runat="server" ID="lblSkLowDistError" CssClass="ModifierErr" />
    <br />
    <asp:Label runat="server" ID="lblWeightTypeError" CssClass="ModifierErr" />
    <br />
    <asp:Label runat="server" ID="lblBikeWeightError" CssClass="ModifierErr" />
    </td>
</tr>
<tr>
    <td></td>
    <td>
    <asp:Button runat="server" ID="btnCalculate" Text="Continue" OnClick="btnCalculate_Click" />
    </td>
</tr>
<tr>
    <td>
        <asp:ValidationSummary runat="server" />
    </td>
</tr>
<tr>
    <td>
    <asp:Label runat="server" ID="lblSpringCalcMsg"/><br />
    <asp:Label runat="server" ID="lblFrontFormulaType" Visible="false" /><br />
    <asp:Label runat="server" ID="lblRearFormulaType" Visible="false" />
    <asp:Label runat="server" ID="lblBikeSize" Visible="false" />
    </td>
</tr>

ascx.vb代码

Public Class SpringCalculator
Inherits System.Web.UI.UserControl

'Getters and Setters removed for brevity
Public Property Bike As Vehicle        
Public Property Genre As Integer        
Public Property BikeId As Integer
Private Property Brand As String        
Private Property Model As String       
Private Property Yr As String
Public Property Oil As String
Public Property PressureSpring As String
Public Property Modifiers As String
Public Property FrontSpringRate As String
Public Property FrontStockRate As String
Public Property RearSpringRate As String
Public Property RearStockRate As String
Public Property FrontSelectedSpring As String
Public Property RearSelectedSpring As String
Public Property isFrontSpringAlternate As Boolean
Public Property isRearSpringAlternate As Boolean
Public Property FrontPreLoad As String
Public Property FrontPreLoadMm As Integer
Public Property FrontPreLoadLabel As String
Public Property RearPreLoad As String
Public Property RearPreLoadMm As Integer
Public Property RearPreLoadLabel As String
Public Property RiderWeightLbs As Decimal
Public Property BikeWeightLbs As Decimal
Public Property FrontLoweredMm As Decimal
Public Property RearLoweredMm As Decimal

Public Event Calculate As EventHandler


Private Const OneKGToLbFactor = 2.20462262
Private Const minLB = 31
Private Const maxLB = 499
Private minKG As Double = Math.Round(minLB / OneKGToLbFactor, 2)
Private maxKG As Double = Math.Round(maxLB / OneKGToLbFactor, 2)


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

        If Not RiderSettings.isValidString(Bike.ReturnValue("FSCS")) Then
            springConversion.Visible = False
        End If

        If Not Bike.Type = Genres.ATV Then
            aarms.Visible = False
            raceSport.Visible = False
            frontRebSep.Visible = False
        End If

        Brand = Bike.ReturnValue("Brand")
        Model = Bike.ReturnValue("Model")
        Yr = Bike.ReturnValue("Yr")

        For Each c As Control In Controls
            If c.GetType().ToString() = "ASP.usercontrols_bikegroupdropdown_ascx" And c.Visible Then
                Dim uc As BikeGroupDropdown = c
                assignBike(uc)
            End If
        Next
    End If
End Sub

Private Sub assignBike(ByRef sender As BikeGroupDropdown)
    sender.Brand = Brand
    sender.Model = Model
    sender.Year = Yr
End Sub


Private Function validateModifiers() As Boolean
    Dim valid As Boolean = True

    For Each c As Control In Controls
        If c.GetType().ToString() = "ASP.usercontrols_bikegroupdropdown_ascx" And c.Visible Then
            Dim myControl As BikeGroupDropdown = c
            If myControl.VarId <= 0 Then
                valid = False
            End If
        End If
    Next

    Return valid
End Function

Protected Sub btnCalculate_Click(sender As Object, e As EventArgs)
    CalculateSprings()
    RaiseEvent Calculate(sender, e)
End Sub

Public Sub CalculateSprings()

    Dim valid As Boolean = True
    If RiderWeightLbs = 0 Then
        RiderWeightLbs = weightInLbs(txtRiderWeight.Text, rblWeightType.SelectedItem.Value)
    End If
    Try
        BikeWeightLbs = weightInLbs(txtBikeWeight.Text, rblWeightType.SelectedItem.Value)
    Catch ex As NullReferenceException
        BikeWeightLbs = 0
    End Try
    Try
        FrontLoweredMm = distInMm(txtFkLowDist.Text, rblFkDistType.SelectedItem.Value)
    Catch ex As NullReferenceException
        FrontLoweredMm = 0
    End Try
    Try
        RearLoweredMm = distInMm(txtSkLowDist.Text, rblSkDistType.SelectedItem.Value)
    Catch ex As Exception
        RearLoweredMm = 0
    End Try


    Dim frontStockspringRate As String = ""
    Dim frontRecSpringRate As String = ""
    Dim rearStockspringRate As String = ""
    Dim rearRecSpringRate As String = ""

    Dim mods As String = ""

    Try
        lblSpringCalcMsg.Text = ""
    Catch ex As NullReferenceException
        'using as class not control
    End Try


    Dim Genre As String = CInt(Bike.Type)

    Dim springCalc As SpringRateCalculator = New SpringRateCalculator("ConnectionString")


    valid = (validateRideWeight())
    If valid Then
        'modifiers.Append(getModifiers()) 'Modifier numbers no longer printed
        mods = getModifiers()
        ViewState("Modifiers") = mods
        If mods.Contains("87") Then
            FrontLoweredMm = 0
            RearLoweredMm = 0
        End If

        Dim pl = ""

        'FORK
        Try 'front preload            
            If Bike.Type = Genres.ATV Then
                If mods.Contains("64") Then 'Sport
                    pl = Bike.ReturnValue("PL_S_F")
                    FrontPreLoad = String.Format("Preload {0}", pl)

                    FrontPreLoadLabel = "Preload"
                ElseIf mods.Contains("65") Then 'Race
                    pl = Bike.ReturnValue("RaceGap_F")
                    FrontPreLoad = String.Format("Gap {0}", pl)

                    FrontPreLoadLabel = "Gap"
                End If
            Else
                If mods.Contains("105") Then 'spring conversion
                    pl = Bike.ReturnValue("PL_Fscs")
                Else
                    pl = Bike.ReturnValue("PL_F")
                End If
                FrontPreLoad = String.Format("Preload {0}", pl)

                FrontPreLoadLabel = "Preload"
            End If
        Catch ex As Exception
            pl = ""
        Finally
            If RiderSettings.isValidString(pl) Then
                FrontPreLoad = FrontPreLoad + " mm"
                FrontPreLoadMm = pl
            End If
        End Try

        'spring rate
        Try
            frontRecSpringRate = String.Format("<tr><td class='RecSpringRate'>Recommended Front Spring Rate: </td><td><b>{0}</b> {1}</td></tr>", springCalc.CalcSpringRate(Brand, Model, Yr, "Front", mods, RiderWeightLbs, BikeWeightLbs, FrontLoweredMm).Replace(Environment.NewLine, ", "), springCalc.Note)
        Catch ex As Exception
            frontRecSpringRate = ex.Message
        Finally
            FrontSpringRate = frontRecSpringRate
        End Try
        frontStockspringRate = String.Format("<tr><td>Stock Front Spring Rate: </td><td><b>{0}</b> (stock)</td></tr>", springCalc.FrontStockRate(Bike))
        FrontStockRate = frontStockspringRate
        FrontSelectedSpring = springCalc.SelectedSpring

        'SHOCK
        Try 'rear preload
            pl = ""
            If Bike.Type = Genres.ATV And mods.Contains("65") Then 'Race
                pl = Bike.ReturnValue("RaceGap")
                RearPreLoad = String.Format("Gap {0}", pl)

                RearPreLoadLabel = "Gap"
            Else
                pl = Bike.ReturnValue("PL_S")
                RearPreLoad = String.Format("Preload {0}", pl)

                RearPreLoadLabel = "Preload"
            End If
        Catch ex As Exception
            pl = ""
        Finally
            If RiderSettings.isValidString(pl) Then
                RearPreLoad = RearPreLoad + " mm"
                RearPreLoadMm = pl
            End If
        End Try

        'spring rate
        Try
            rearRecSpringRate = String.Format("<tr><td class='RecSpringRate'>Recommended Spring Rate: </td><td><b>{0}</b> {1}</td></tr>", springCalc.CalcSpringRate(Brand, Model, Yr, "Rear", mods, RiderWeightLbs, BikeWeightLbs, RearLoweredMm), springCalc.Note)
        Catch ex As Exception
            rearRecSpringRate = ex.Message
        Finally
            RearSpringRate = rearRecSpringRate
        End Try
        rearStockspringRate = String.Format("<tr><td>Stock Spring Rate: </td><td><b>{0}</b> (stock)</td></tr>", springCalc.RearStockRate(Bike))
        RearStockRate = rearStockspringRate
        RearSelectedSpring = springCalc.SelectedSpring

        Dim pressureSpringExists = getPressureSpringSeries(Genre, Brand, Model, Yr)

        If RiderSettings.isValidString(pressureSpringExists) Then
            PressureSpring = "Spring Series " + getPressureSpringSeries(Genre, Brand, Model, Yr) + "; "
            If mods.Split(",").Intersect("14,15,86".Split(",")).Any() Then 'Supercross; Arenacross; Freestyle MX
                PressureSpring += "2.2 kg/mm"
            ElseIf mods.Contains("11") Then 'Trail/Enduro
                PressureSpring += "1.4 kg/mm"
            ElseIf mods.Split(",").Intersect("12,13,54,55,85".Split(",")).Any() Then
                PressureSpring += "1.8 kg/mm"
            End If

        End If

        Oil = BikeSetting.Oil.Level(mods, True, Bike)

    End If

End Sub

Private Function weightInLbs(ByVal weight As String, ByVal label As String) As Decimal
    Dim rtn As Decimal

    Decimal.TryParse(weight, rtn)
    If label.ToLower = "kg" Then
        rtn *= 2.20462262 '1 kilogram = 2.20462262 pounds        
    End If
    Return rtn
End Function
Private Function distInMm(ByVal dist As String, ByVal label As String) As Decimal
    Dim rtn As Decimal

    Decimal.TryParse(dist, rtn)
    If label.ToLower = "in" Then
        rtn *= 25.4 '1 inch = 25.4 milimeters
    End If
    Return rtn
End Function
Private Function validateRideWeight() As Boolean

    Dim valid As Boolean = True
    Dim lbs As Double = -1

    Try
        lblRiderWeightError.Text = ""
        lblWeightTypeError.Text = ""
    Catch ex As NullReferenceException
        Return True 'using as a class not control
    End Try

    If Not Helper.isDouble(RiderWeightLbs) Then
        lblRiderWeightError.Text = "Enter a numeric value for Rider weight"
        valid = False
    Else
        lbs = CDbl(RiderWeightLbs)

    End If

    If rblWeightType.SelectedIndex = -1 Then
        lblWeightTypeError.Text = "Select Lbs or Kg for Weight type"
        valid = False
    Else

        If lbs <> -1 Then
            If rblWeightType.SelectedItem.Value.ToLower = "kg" Then
                lbs = lbs * OneKGToLbFactor
            End If
        End If
        If lbs < minLB Or lbs > maxLB Then
            If rblWeightType.SelectedItem.Value.ToLower = "kg" Then
                lblRiderWeightError.Text = "Weight must be between " & minKG & " and " & maxKG & " Kgs"
            Else
                lblRiderWeightError.Text = "Weight must be between " & minLB & " and " & maxLB & " Lbs"
            End If
            valid = False
        End If
    End If

    Return valid

End Function
Private Function getModifiers() As String

    Dim modifiers As New StringBuilder

    For Each c As Control In Controls
        If c.GetType().ToString() = "ASP.usercontrols_bikegroupdropdown_ascx" And c.Visible Then
            Dim uc As BikeGroupDropdown = c
            If modifiers.Length > 0 Then
                modifiers.Append(",")
            End If
            modifiers.Append(uc.VarId)
        End If

    Next

    Return modifiers.ToString()
End Function
Private Function getPressureSpringSeries(genre As Int32, brand As String, model As String, yr As String) As String
    Dim series As String = ""

    Try
        If genre = 1 Then 'FRPS not found in Street Table, Quad is currently not surrported
            series = Bike.ReturnValue("FRPS")
        End If
    Catch ex As Exception
        Helper.SendError(String.Format("Session: {5}{4}{0} {4} brand:{1}\r\n model:{2}\r\n yr:{3}", ex.ToString, brand, model, yr, System.Environment.NewLine, Session.SessionID))
    End Try

    Return series

End Function
End Class

1 个答案:

答案 0 :(得分:1)

控件中的字段验证器阻止父页面执行回发。

通过仅将控件拉到测试页面中的“下一步”按钮,我就发现了这一点。由于父页面的默认视图不再隐藏该控件,所以我可以看到在忽略控件时会出现验证器的消息。