2013-08-07 123 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