2013-08-07 124 views
0

我正在将用户控件从一个项目版本迁移到新版本。在试图更改版本时,项目中存在错误,所以我创建了一个新项目并开始复制页面和控件。我终于把所有的控件都复制过来了,而且我正在复制页面。我正在测试一切。表单控制值不会在回发中重新加载

说到这里,我正在研究一个控件,并且我注意到我遇到了需要的字段验证器的问题。这些字段不是动态的,所以它们始终存在。来自所需验证器的测试失败的字段中包含数据。有三个有问题。所有三个都是文本字段。我能够将两个验证器转换为自定义验证器,并能够让他们工作,但第三个仍然不起作用。

不工作的自定义验证程序失败,因为页面中的值未重新加载到控件中。当我使用控件的唯一ID检查发布到表单的内容时,数据就存在了。什么可能导致这种脱节?我明白,在控件事件触发前,控件应该被重新加载后的数据。看来这个过程被打破了。会发生什么?

有问题的字段是txtTitle,txtDescription和txtSendNoticeTo。

HTML:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="ProjectInformation.ascx.vb" Inherits="KDoc.ProjectInformation" %> 
<%@ Register TagPrefix="cc1" Namespace="SqlInjectionControls" Assembly="SqlInjectionControls" %> 

<script type="text/javascript"> 
<!-- 
function addSendToName() { 

    var textField = document.getElementById('ctl08_txtSendNoticeTo'); 
    //  alert(document.getElementById(textFieldId)); 

    if (textField.value.length > 0) { 
     textField.value += "; " + document.getElementById('ctl08_ddlSendToNames').value; 
    } else { 
     textField.value = document.getElementById('ctl08_ddlSendToNames').value; 
    } 
} 
//--> 
</script> 

<table class="fullWidth padding-2 spacing-3" border="0"> 
<tr> 
    <td class="nowrap" style="width: 15%">Platform&nbsp;<span class="Required">*</span></td> 
    <td style="width: 35%"> 
     <asp:dropdownlist id="ddlPlatform" Runat="server"/><br/> 
     <asp:requiredfieldvalidator id="reqPlatform" Runat="server" ControlToValidate="ddlPlatform" Display="Dynamic" ErrorMessage="Required"/> 
    </td> 
    <td colSpan="2" class="right"> 
     <asp:LinkButton ID=btnChangeNotice CausesValidation=false runat=server Text="Change Notice Report" Visible=false /> 
    </td> 
</tr> 
<tr> 
    <td class="nowrap" style="width: 15%">Area Manager&nbsp;<span class="Required">*</span></td> 
    <td style="width: 35%"> 
     <asp:dropdownlist id="ddlSystem" Width="100%" Runat="server"/><br/> 
     <asp:requiredfieldvalidator id="reqSystem" Runat="server" ControlToValidate="ddlSystem" Display="Dynamic" ErrorMessage="Required"/> 
    </td> 
    <td class="nowrap" style="width: 15%">Application</td> 
    <td style="width: 35%"> 
     <asp:textbox id="txtApplication" Width="100%" MaxLength="100" Runat="server"/> 
     <cc1:sqlinjectionvalidator id="sqlApplication" runat="server" ControlToValidate="txtApplication" ErrorMessage="Error in input field"/> 
    </td> 
</tr> 
<tr> 
    <td class="nowrap" style="width: 15%">Menu</td> 
    <td style="width: 35%"> 
     <asp:textbox id="txtMenu" Width="100%" MaxLength="100" Runat="server"/> 
     <cc1:sqlinjectionvalidator id="sqlMenu" runat="server" ControlToValidate="txtMenu" ErrorMessage="Error in input field"/> 
    <td class="nowrap" style="width: 15%">Step</td> 
    <td style="width: 35%"> 
     <asp:textbox id="txtMenuStep" Width="100%" MaxLength="100" Runat="server"/> 
     <cc1:sqlinjectionvalidator id="sqlMenuStep" runat="server" ControlToValidate="txtMenuStep" ErrorMessage="Error in input field"/> 
    </td> 
<tr> 
    <td class="nowrap" style="width: 15%">Title&nbsp;<span class="Required">*</span></td> 
    <td colSpan="3"> 
     <asp:textbox id="txtTitle" Width="100%" MaxLength="100" Runat="server"/> 
     <asp:CustomValidator ID="cvTitle" runat="server" ControlToValidate="txtTitle" Display="Dynamic" ErrorMessage="Required" /> 
     <cc1:sqlinjectionvalidator id="sqlTitle" runat="server" ControlToValidate="txtTitle" ErrorMessage="Error in input field"/> 
    </td> 
</tr> 
<tr class="top"> 
    <td class="nowrap" style="width: 15%">Description&nbsp;<span class="Required">*</span></td> 
    <td colSpan="3"> 
     <asp:textbox id="txtDescription" Width="100%" Runat="server" Height="150" TextMode="MultiLine"/> 
     <asp:CustomValidator ID="cvDescription" runat="server" ControlToValidate="txtDescription" Display="Dynamic" ErrorMessage="Required" /> 
     <cc1:sqlinjectionvalidator id="sqlDescription" runat="server" ControlToValidate="txtDescription" ErrorMessage="Error in input field"/> 
    </td> 
</tr> 
<tr class="top"> 
    <td style="width: 15%">Special Instructions</td> 
    <td colSpan="3"> 
     <asp:textbox id="txtSpecialInstructions" Width="100%" Runat="server" Height="150" TextMode="MultiLine"/> 
     <cc1:sqlinjectionvalidator id="sqlSpecialInstructions" runat="server" ControlToValidate="txtSpecialInstructions" ErrorMessage="Error in input field"/> 
    </td> 
</tr> 
<tr> 
    <td class="BoxHeader" colspan="4">Notification Information</td> 
</tr> 
<tr> 
    <td class="nowrap" style="width: 15%">Requesting Branch</td> 
    <td style="width: 35%"> 
     <asp:TextBox ID="txtRequestingBranch" runat="server" MaxLength="4"/><br/> 
     <cc1:SqlInjectionValidator ID="sqlRequestingBranch" runat="server" ErrorMessage="Error in input field" ControlToValidate="txtRequestingBranch" /> 
    </td> 
    <td class="nowrap" style="width: 15%">Requestor</td> 
    <td style="width: 35%"> 
     <asp:TextBox ID="txtRequestor" runat="server" MaxLength="30"/><br/> 
     <cc1:SqlInjectionValidator ID="sqlRequestor" runat="server" ErrorMessage="Error in input field" ControlToValidate="txtRequestor"/> 
    </td> 
</tr> 
<tr> 
    <td class="nowrap" style="width: 15%">IS Contact&nbsp;<asp:label ID=lblContactRequired runat=server cssclass="Required" Text="*"/></td> 
    <td style="width: 35%"> 
     <asp:TextBox ID="txtContact" runat="server" MaxLength="30"/><br/> 
     <asp:CustomValidator ID="cvContact" runat="server" Display="Dynamic" ControlToValidate="ddlPlatform" ErrorMessage="Required" /> 
     <cc1:SqlInjectionValidator ID="sqlContact" runat="server" ErrorMessage="Error in input field" ControlToValidate="txtContact" /> 
    </td> 
    <td class="nowrap" style="width: 15%">Branches</td> 
    <td style="width: 35%"> 
     <asp:TextBox ID="txtBranches" runat="server" MaxLength="50"/><br/> 
     <cc1:SqlInjectionValidator ID="sqlBranches" runat="server" ErrorMessage="Error in input field" ControlToValidate="txtBranches" /> 
    </td> 
</tr> 
<tr> 
    <td class="nowrap" style="width: 15%">Default Names</td> 
    <td colspan="3"> 
     <asp:DropDownList ID="ddlSendToNames" runat="server" DataTextField="Description" DataValueField="Description"/> 
     &nbsp;<input type="button" id="btnAddName" value="Add name" runat="Server" name="btnAddName" onclick="addSendToName();" /> 
    </td> 
</tr> 
<tr> 
    <td class="nowrap" style="width: 15%">Send Notice To&nbsp;<asp:label ID=lblSendToRequired runat=server cssclass="Required" Text="*"/></td> 
    <td colspan="3"> 
     <asp:TextBox ID="txtSendNoticeTo" runat="server" Width="100%" TextMode="MultiLine" Height="120" /> 
     <asp:CustomValidator ID="cvSendNoticeTo" runat="server" Display="Dynamic" ControlToValidate="ddlPlatform" ErrorMessage="Required" /> 
     <cc1:SqlInjectionValidator ID="sqlSendNoticeTo" runat="server" ErrorMessage="Error in input field" ControlToValidate="txtSendNoticeTo"/> 
    </td> 
</tr> 
</table> 

后面的代码:

Public Class ProjectInformation 
Inherits System.Web.UI.UserControl 


#Region "Attributes" 
Private myPackage As ProjectPackage 
#End Region 

#Region "events" 
''' <summary> 
''' transfer the page over to the change notice report 
''' </summary> 
''' <remarks> 
''' </remarks> 
Protected Sub btnChangeNotice_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnChangeNotice.Click 
    Dim queryString As New StringBuilder 
    queryString.Append("reportloader.aspx?") 
    queryString.Append(QUERYSTRING_REPORT).Append("=") 
    queryString.Append(REPORT_NAME_CHANGENOTICE) 
    'queryString.Append("&").Append(QUERYSTRING_PARAMETER).Append("=").Append(CType(Me.Parent.FindControl("txtPackageId"), TextBox).Text) 
    queryString.Append("&").Append(QUERYSTRING_PARAMETER).Append("=").Append(myPackage.PackageId) 
    queryString.Append("&").Append(QUERYSTRING_FORMAT).Append("=") 
    queryString.Append(REPORT_FORMAT_ACROBAT) 

    Response.Redirect(queryString.ToString, True) 
End Sub 

Protected Sub cvContact_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvContact.ServerValidate 
    Dim pt As String = CType(Parent.FindControl("ddlProjectType"), DropDownList).SelectedValue 

    If pt.ToLower.Equals("chg") And txtContact.Text.Equals(String.Empty) Then 
     args.IsValid = False 
    Else 
     args.IsValid = True 
    End If 
End Sub 

Private Sub cvDescription_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvDescription.ServerValidate 
    args.IsValid = CBool(txtDescription.Text.Length > 0) 
End Sub 

Protected Sub cvSendNoticeTo_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvSendNoticeTo.ServerValidate 
    Dim pt As String = CType(Parent.FindControl("ddlProjectType"), DropDownList).SelectedValue 

    If pt.ToLower.Equals("chg") And txtSendNoticeTo.Text.Length = 0 Then 
     args.IsValid = False 
    Else 
     args.IsValid = True 
    End If 
End Sub 

Private Sub cvTitle_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvTitle.ServerValidate 
    args.IsValid = CBool(txtTitle.Text.Length > 0) 
End Sub 
#End Region 

#Region "Functions" 
Private Sub BindData() 
    BindPlatform() 
    BindSystems() 
    BindSendToNames() 
End Sub 

Private Sub BindPlatform() 
    Dim pl As New BusinessLayer.PlatformCollection 
    pl.Load() 

    ddlPlatform.DataSource = pl 
    ddlPlatform.DataTextField = "Description" 
    ddlPlatform.DataValueField = "PlatformId" 
    ddlPlatform.DataBind() 

    ddlPlatform.Items.Insert(0, "") 

    Try 
     ddlPlatform.SelectedValue = myPackage.Platform.PlatformId 
    Catch ex As Exception 
     ddlPlatform.Items.Clear() 
     ddlPlatform.Items.Insert(0, New Web.UI.WebControls.ListItem("Select Platform", "")) 
     ddlPlatform.SelectedIndex = 0 
    End Try 
End Sub 

Private Sub BindSendToNames() 
    Dim st As New BusinessLayer.SendToCollection 
    st.Load() 

    ddlSendToNames.DataSource = st 
    ddlSendToNames.DataBind() 
End Sub 

Private Sub BindSystems() 
    Dim sm As New SystemManagerCollection 
    sm.Load() 

    ddlSystem.DataSource = sm 
    ddlSystem.DataTextField = "Name" 
    ddlSystem.DataValueField = "ManagerId" 
    ddlSystem.DataBind() 

    ddlSystem.Items.Insert(0, "") 

    Try 
     ddlSystem.SelectedValue = myPackage.SystemManager.ManagerId 
    Catch ex As Exception 
     ddlSystem.Items.Clear() 
     ddlSystem.Items.Insert(0, New Web.UI.WebControls.ListItem("Manager not found", "")) 
     ddlSystem.SelectedIndex = 0 
    End Try 
End Sub 

''' <summary> 
''' used to set permissions on the controls 
''' </summary> 
''' <remarks> 
''' </remarks> 
Public Sub DisableControls() 
    myPackage = CType(Context.Session(SESSION_PACKAGE), ProjectPackage) 
    Dim bum As New SystemManager 
    bum.LoadBackUpManagerByManager(myPackage.SystemManager.ManagerId) 

    If myPackage.SystemManager.ManagerId.ToLower.Equals(Context.User.Identity.Name.ToLower) Or _ 
     bum.ManagerId.ToLower.Equals(Context.User.Identity.Name.ToLower) Then 
     If (myPackage.Status.Id = PackageStatusEnum.ReadyForManager Or _ 
      myPackage.Status.Id = PackageStatusEnum.DeniedForManager) Then 
      ddlPlatform.Enabled = False 
      ddlSystem.Enabled = False 
      txtApplication.Enabled = False 
      txtMenu.Enabled = False 
      txtMenuStep.Enabled = False 
      txtTitle.Enabled = False 
      txtSpecialInstructions.Enabled = False 
     Else 
      ddlPlatform.Enabled = False 
      ddlSystem.Enabled = False 
      txtApplication.Enabled = False 
      txtMenu.Enabled = False 
      txtMenuStep.Enabled = False 
      txtDescription.Enabled = False 
      txtTitle.Enabled = False 
      txtSpecialInstructions.Enabled = False 
     End If 

    Else 
     ddlPlatform.Enabled = False 
     ddlSystem.Enabled = False 
     txtApplication.Enabled = False 
     txtMenu.Enabled = False 
     txtMenuStep.Enabled = False 
     txtTitle.Enabled = False 
     txtDescription.Enabled = False 
     txtSpecialInstructions.Enabled = False 
    End If 

    ' notification section 
    txtRequestingBranch.Enabled = False 
    txtRequestor.Enabled = False 
    txtContact.Enabled = False 
    txtBranches.Enabled = False 
    ddlSendToNames.Enabled = False 
    btnAddName.Disabled = True 
    txtSendNoticeTo.Enabled = False 
End Sub 

''' <summary> 
''' unlock the controls that change control is able to update 
''' </summary> 
''' <remarks> 
''' </remarks> 
Public Sub EnableControlsForChangeControl() 
    txtTitle.Enabled = True 
    txtDescription.Enabled = True 
    txtSpecialInstructions.Enabled = True 
    ' notification section 
    ddlSendToNames.Enabled = True 
    btnAddName.Disabled = False 
    txtSendNoticeTo.Enabled = True 
End Sub 

''' <summary> 
''' unlock all controls for the package manager 
''' </summary> 
''' <remarks> 
''' </remarks> 
Public Sub EnableControlsForManager() 
    ddlPlatform.Enabled = True 
    ddlSystem.Enabled = True 
    txtApplication.Enabled = True 
    txtMenu.Enabled = True 
    txtMenuStep.Enabled = True 
    txtTitle.Enabled = True 
    txtDescription.Enabled = True 
    txtSpecialInstructions.Enabled = True 
    txtContact.Enabled = True 
    txtBranches.Enabled = True 
    txtSendNoticeTo.Enabled = True 
End Sub 

''' <summary> 
''' overridden function to load all of the form variables for the control 
''' </summary> 
''' <remarks> 
''' </remarks> 
Public Shadows Sub Load() 
    myPackage = CType(Context.Session(SESSION_PACKAGE), ProjectPackage) 

    BindData() 
    SetFormVars() 

    btnChangeNotice.Visible = CBool(myPackage.Status.Id > PackageStatusEnum.ReadyForManager) 
End Sub 

''' <summary> 
''' toggle visibility of required field markers 
''' </summary> 
''' <param name="Required">required indicator</param> 
''' <remarks> 
''' </remarks> 
Public Sub RequireNotification(ByVal Required As Boolean) 
    lblSendToRequired.Visible = Required 
    lblContactRequired.Visible = Required 
End Sub 

''' <summary> 
''' set the values of the controls with the package and project information 
''' </summary> 
''' <remarks> 
''' </remarks> 
Private Sub SetFormVars() 
    txtApplication.Text = myPackage.Application 
    txtMenu.Text = myPackage.Menu 
    txtMenuStep.Text = myPackage.MenuStep 
    txtTitle.Text = myPackage.Title 
    txtDescription.Text = myPackage.Description 
    txtSpecialInstructions.Text = myPackage.SpecialInstructions 

    myPackage.Project.Load() 
    txtRequestingBranch.Text = myPackage.Project.RequestingLocation 
    txtRequestingBranch.Enabled = False 
    txtRequestor.Text = myPackage.Project.RequestorName 
    txtRequestor.Enabled = False 
    txtContact.Text = myPackage.Contact 
    txtBranches.Text = myPackage.Branches 
    txtSendNoticeTo.Text = myPackage.SendNoticeTo 
End Sub 

''' <summary> 
''' update the project package with the data contained within this module 
''' </summary> 
''' <remarks> 
''' </remarks> 
Public Sub Update() 
    myPackage = CType(Context.Session(SESSION_PACKAGE), ProjectPackage) 

    If myPackage.Status.Id >= PackageStatusEnum.ReadyForDirector Then Exit Sub 

    myPackage.Platform.PlatformId = ddlPlatform.SelectedValue 
    myPackage.SystemManager.ManagerId = ddlSystem.SelectedValue 
    myPackage.Application = txtApplication.Text 
    myPackage.Menu = txtMenu.Text 
    myPackage.MenuStep = txtMenuStep.Text 
    myPackage.Title = txtTitle.Text 
    myPackage.Description = txtDescription.Text 
    myPackage.SpecialInstructions = txtSpecialInstructions.Text 
    myPackage.Contact = txtContact.Text 
    myPackage.Branches = txtBranches.Text 
    myPackage.SendNoticeTo = txtSendNoticeTo.Text 
    myPackage.UpdateInformation(Context.User.Identity.Name) 

    'If ddlSendToNames.Enabled Then 
    ' 'myPackage.Project.RequestingLocation = txtRequestingBranch.Text 
    ' 'myPackage.Project.RequestorName = txtRequestor.Text 
    ' myPackage.Contact = txtContact.Text 
    ' myPackage.Branches = txtBranches.Text 
    ' myPackage.SentNoticeTo = txtSendNoticeTo.Text 
    ' myPackage.Project.UpdateNotificationInfo() 
    'End If 
    Context.Session(SESSION_PACKAGE) = myPackage 
End Sub 
#End Region 

#Region "Properties" 
'Public Property Package() As BusinessLayer.ProjectPackage 
' Get 
'  Return myPackage 
' End Get 
' Set(ByVal Value As BusinessLayer.ProjectPackage) 
'  myPackage = Value 
' End Set 
'End Property 

Public ReadOnly Property Platform() As String 
    Get 
     Return ddlPlatform.SelectedValue 
    End Get 
End Property 

Public ReadOnly Property SystemManager() As String 
    Get 
     Return ddlSystem.SelectedValue 
    End Get 
End Property 

Public ReadOnly Property ApplicationName() As String 
    Get 
     Return txtApplication.Text 
    End Get 
End Property 

Public ReadOnly Property Menu() As String 
    Get 
     Return txtMenu.Text 
    End Get 
End Property 

Public ReadOnly Property MenuStep() As String 
    Get 
     Return txtMenuStep.Text 
    End Get 
End Property 

Public ReadOnly Property Title() As String 
    Get 
     Return txtTitle.Text 
    End Get 
End Property 

Public ReadOnly Property Description() As String 
    Get 
     Return txtDescription.Text 
    End Get 
End Property 

Public ReadOnly Property SpecialInstructions() As String 
    Get 
     Return txtSpecialInstructions.Text 
    End Get 
End Property 
#End Region 

End Class 

回答

0

更改的功能如下:

function addSendToName() { 

    var textField = document.getElementById('<%= txtSendNoticeTo.ClientID %>'); 
    //  alert(document.getElementById(textFieldId)); 

    if (textField.value.length > 0) { 
     textField.value += "; " + document.getElementById('<%= ddlSendToNames.ClientID %>').value; 
    } else { 
     textField.value = document.getElementById('<%= ddlSendToNames.ClientID %>').value; 
    } 
} 

,而这些名字可能会在前面的代码工作过,很可能浏览器正在生成新的/不同的名称,现在你已经改变了事情。以这种方式编写它们可确保您始终在DOM上获得正确的ID。

+0

好的,这是一个好主意,但这并不能解决服务器端发生的事情。 – fizch

+0

我没有发现你的服务器端问题。这是怎么回事? –

+0

窗体控件值不会在任何文本字段的回发中重新加载。 – fizch