预输入数据列表文本框未重置

Posted

技术标签:

【中文标题】预输入数据列表文本框未重置【英文标题】:Type-Ahead Datalist Textbox Not Resetting 【发布时间】:2021-11-21 20:47:23 【问题描述】:

我有一个包含预先输入文本框的应用程序。用户希望预先输入使用“开始于”而不是“包含”进行搜索。我的同事写了一些 javascript 来启用“开始于”搜索。它工作得很好......直到我点击添加按钮。然后列表不会刷新所有选项。这就是我的意思: 文本框/数据列表开始(在加载/刷新页面时)填充如下: 苹果 杏子 鳄梨 香蕉 黑莓 蓝莓 樱桃 椰子 蔓越莓

如果我开始输入 C,文本框/数据列表现在包含: 樱桃 椰子 蔓越莓

如果我选择 Cherry,然后单击 Add,我希望列表会返回到所有水果的原始状态,但是它保留 Cherry 的值,删除 Cherry 只会添加以“C”开头的其他水果, 所以列表再次出现: 樱桃 椰子 蔓越莓

添加后,我需要将所有水果都放回列表中。请看以下代码: 来自 aspx:

<datalist id="dlParentFruit">
    <asp:PlaceHolder ID="phparentFruit" runat="server"></asp:PlaceHolder>
</datalist>

<asp:TextBox ID="txtAddStdFruit" list="dlParentFruit" runat="server" Width="200px" MaxLength="250"></asp:TextBox>
<ajax:FilteredTextBoxExtender ID="fteAddFruit" runat="server" TargetControlID="txtAddStdFruit" FilterType="Custom" FilterMode="InvalidChars" InvalidChars="&quot;'[]\|^~`!@#$"></ajax:FilteredTextBoxExtender>
<asp:Panel ID="pnlValAddFruit" runat="server" CssClass="cse-val-tooltip cse-hide">
    <img src="../Images/triangle.png"  />
    <ul id="vulAddFruit" runat="server"></ul>
</asp:Panel>
<asp:RequiredFieldValidator ID="rfvAddStdFruit" runat="server"
    ErrorMessage="Required: Fruit"
    ControlToValidate="txtAddStdFruit"
    Display="None"
    ValidationGroup="Add"
    data-val="pnlValAddFruit">
</asp:RequiredFieldValidator>
(VB) 背后的代码: ``` Private Sub btnAddStdFruit_Click(ByVal sender As Object, ByVal e As EventArgs) 处理 btnAddStdFruit.Click 尝试 If IsErrorShownForAdd Then 重置添加验证() 万一
    Page.Validate("Add")

    If IsAddValid() Then

        Dim dateMin As DateTime = Me.StdDate & " " & Helper.SetTime(txtAddStart.Text)
        Dim dateMax As DateTime
        If txtAddStop.Text = 2400 Then
            dateMax = Helper.setMidnight(Me.StdDate, txtAddStop.Text)
        Else
            dateMax = Me.StdDate & " " & Helper.SetTime(txtAddStop.Text)
        End If

        Dim itemSelected As New ListItem
        Dim ctrAddedFrts As Integer = 0
        Dim bolDupeRow As Boolean = False

        Dim strResourceList As String = String.Empty

        For Each itemSelected In lbLocationResource.Items
            If itemSelected.Selected = True Then

                bolDupeRow = IsFruitDuplicate(-1, itemSelected.Text, dateMin, dateMax, txtAddStdFruit.Text, ddlAddCategory.SelectedValue)

                If bolDupeRow = False Then
                    MRData.AddStdFruit(Me.StdID, txtAddStdFruit.Text, itemSelected.Value, dateMin, dateMax, ddlAddCategory.SelectedValue, Me.UserID, "Edit All Frt - Added Fruit")
                    ctrAddedFrts += 1
                End If
                strResourceList = strResourceList & itemSelected.Value & ","
            End If
        Next

        If strResourceList.Length > 0 Then
            'Remove last comma
            strResourceList = strResourceList.Substring(0, strResourceList.Length - 1)
            Me.SelectedResourceList = strResourceList
        End If


        If ctrAddedFrts > 0 Then

            txtAddStart.Text = String.Empty
            txtAddStop.Text = String.Empty
            pnlValAddLocation.CssClass = Constants.VALIDATOR_PANEL_RESET_STYLE

            ResetStdRequestSet()
            ResetMenuFruitList()
            ResetStandardFruitList()

            BindLists()
            BindStdFruits()


        ElseIf ctrAddedFrts = 0 AndAlso bolDupeRow = False Then
            IsErrorShownForAdd = True
            pnlValAddLocation.CssClass = Constants.VALIDATOR_PANEL_STYLE
            lbLocationResource.CssClass = "cse-select-error"

            Dim listItem As New htmlGenericControl("li")
            listItem.InnerHtml = Helper.FormatErrorMessage("Error: Location must be chosen")
            vulAddLocation.Controls.Add(listItem)


        ElseIf ctrAddedFrts = 0 AndAlso bolDupeRow = True Then
            IsErrorShownForAdd = True
            pnlValAddLocation.CssClass = Constants.VALIDATOR_PANEL_STYLE
            lbLocationResource.CssClass = "cse-select-error"

            Dim listItem As New HtmlGenericControl("li")
            listItem.InnerHtml = Helper.FormatErrorMessage("Error: Duplicate Fruit")
            vulAddLocation.Controls.Add(listItem)
        End If

    End If

    pnlAddStdFruits.Attributes.Add("style", "display: New;")
    hlAddFruits.InnerText = "-Add Fruits"

    upnlStdFruits.Update()

Catch ex As Exception
    AppUtilities.HandleError(ex, Me.Page, Identity.SystemUserID, Constants.SITE_ID, PAGE_NAME)
End Try

结束子

子绑定列表() ddlAddCategory.DataSource = Me.FruitCategoryList ddlAddCategory.DataTextField = "Fruit_CATEGORY" ddlAddCategory.DataValueField = "Fruit_CATEGORY_ID" ddlAddCategory.DataBind() ddlAddCategory.Items.Insert(0, New WebControls.ListItem("", -1)) ddlAddCategory.SelectedIndex = 0

phParentFruit.Controls.Clear()


Dim dtFruits As DataTable = Me.StandardFruitList

Dim hgcParentList As New HtmlGenericControl
hgcParentList.InnerHtml = Helper.BuildDatalist(dtFruits, "Fruit_TEXT", "LU_ID_CAT")
phParentFruit.Controls.Add(hgcParentList)

结束子

Public Sub BindStdFruits(Optional ByVal dt As DataTable = Nothing) 将 dtStdFrt 调暗为数据表 如果 Me.MenuFruitList 什么都不是,那么 ResetMenuFruitList() dtStdFrt = Me.MenuFruitList 别的 dtStdFrt = Me.MenuFruitList 结束如果

If Me.StandardFruitList Is Nothing Then
    ResetStandardFruitList()
End If

If dtStdFrts.Rows.Count > 0 Then
    gvStdFrt.DataSource = dtStdFrt
    gvStdFrt.DataBind()

    lblGridMessage.Text = String.Empty
    lblGridMessage.Visible = False

    If Not Me.IsReadOnly Then
        hlMassTime.Visible = True
        btnDelete.Visible = True
        btnUpdate.Visible = True
    Else
        hlMassTime.Visible = False
        btnDelete.Visible = False
        btnUpdate.Visible = False
    End If

    gvStdFrt.Visible = True
Else
    lblGridMessage.Visible = True
    lblGridMessage.Text = "No menu Fruits"

    If (hlAddFruits.Visible = True) Then
        pnlAddStdFruits.Attributes.Add("style", "display: New;")
        hlAddFruits.InnerText = "-Add Fruits"
    End If

    btnDelete.Visible = False
    btnUpdate.Visible = False
    hlMassTime.Visible = False
    gvStdFrt.Visible = False
End If

upnlStdFruits.Update()

结束子

Sub ResetStandardFruitList() 将 dtStdFrt 调暗为数据表 dtStdFrt = MIMData.GetStandardFruitByType("STD") Me.StandardFruitList = dtStdStdFrt 结束子

<br />
JavaScript:<br />

Methods.addNewFruitsFruitListStartsWith = function (txtFruit)

if (typeof txtFruit !== 'undefined' && txtFruit !== null && txtFruit.nodeName === 'INPUT' &&
    Controls.addPanelFruitOptions !== null && typeof Controls.addPanelFruitOptions !== 'undefined' &&
    Inputs.initialAddPanelFruitDataListContent !== null && typeof Inputs.initialAddPanelFruitDataListContent !== 'undefined') 

    var value = txtFruit.value;

    if (typeof value !== 'undefined' && value !== '') 
        var options = '';

        for (var i = 0; i < Controls.addPanelFruitOptions.length; i++) 
            if (Controls.addPanelFruitOptions[i].value.toLowerCase().startsWith(value.toLowerCase())) 
                options += '<option value="' + Controls.addPanelFruitOptions[i].value + '" />';
            
        

        Controls.addPanelFruitDataList.innerHTML = options;
     else 
        Controls.addPanelFruitDataList.innerHTML = Inputs.initialAddPanelFruitDataListContent;
    


【问题讨论】:

【参考方案1】:

.aspx 中,我不得不将DataList 控件移动到UpdatePanel。愚蠢的错误!

【讨论】:

以上是关于预输入数据列表文本框未重置的主要内容,如果未能解决你的问题,请参考以下文章

C# - 上次编辑的文本框未保存到数据库

Ajax AutoCompleteExtender文本框未在边缘浏览器中触发文本更改事件

访问表单的文本框未显示值

IsEnabled="False" 状态的 WPF 文本框未应用正确的背景颜色

在 this.setState 之后 Typeahead 文本输入未重置

预填充 jquery 令牌输入文本框