首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >回发时不重新加载窗体控件值

回发时不重新加载窗体控件值
EN

Stack Overflow用户
提问于 2013-08-08 00:26:00
回答 1查看 235关注 0票数 0

我正在将用户控件从项目的一个版本迁移到新版本。在尝试更改版本时,项目中存在错误,因此我创建了一个新项目,并开始复制页面和控件。我终于把所有的控件都复制过来了,我正在复制页面。我边走边测试所有的东西。

话虽如此,我正在处理一个控件,我注意到我在使用所需的字段验证器时遇到了问题。这些字段不是动态的,因此它们始终存在。来自所需验证器的未通过测试的字段中包含数据。有三个有问题。这三个字段都是文本字段。我能够将其中两个验证器转换为自定义验证器,并且能够让它们工作,但第三个仍然不起作用。

不工作的自定义验证器失败,因为未在控件中重新加载页面中的值。当我使用控件的唯一id检查发送到窗体的内容时,数据是存在的。是什么导致了这种连接中断?我知道在触发控件的事件之前,应该用post数据重新加载控件。看起来这个过程被打破了。可能会发生什么?

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

HTML:

代码语言:javascript
复制
<%@ 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>

代码隐藏:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2013-08-08 00:36:39

将函数更改为:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18108661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档