级联下拉,第二个并不总是保持变化

时间:2015-03-03 18:08:31

标签: asp.net vb.net

我有两个级联下拉菜单,供用户选择一个客户端,然后选择一个位置。下拉工作正常。用户选择位置后,我会根据该数据填充文本框。发生的事情是,在选择位置后,大多数时候文本框都填充了正确的数据。然后,位置下拉列表似乎随机选择要显示的其他位置。有时,下拉列表会多次选择相同的错误位置。有时它是一个不同的位置。几乎所有时间都与设置在文本框中的位置不同。

相关的aspx:

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="NewJob.aspx.vb" Inherits="JobsApp.NewJob" EnableEventValidation="false" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style>
.ui-autocomplete { 
    cursor:pointer; 
    height:200px; 
    font-size:medium;
    font-family:Calibri;
    overflow-y:scroll;
    text-align:left;
}    
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </ajaxToolkit:ToolkitScriptManager>
    <script>
        //Script must be placed in body of form so the pagerequestmanager will work.
    $(document).ready(function () {
        BindControls();
    });
    //Calls BindControls on partial page refresh so autocomplete field will work.
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(BindControls);
    function BindControls() {
    $("#txtSearch").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "ServiceCS.asmx/GetScopes",
            data: "{ 'sLookUP': '" + request.term + "' }",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataFilter: function (data) { return data; },
            success: function (data) {
                response($.map(data.d, function (item) {
                    return {
                        value: item,
                    }
                }))
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }
        });
    },
    select: function (e, i) {
        $("#<%=txtScopeID.ClientID%>").val(i.item.value);
    },
    minLength: 0,
    scroll: true
    }).focus(function () {
    $(this).autocomplete("search", "");
    })
    }
</script>
<asp:Table runat="server" BackColor="LightGray">
<asp:TableRow BackColor="LightGray">
    <asp:TableCell ColumnSpan="6">
    <ajaxToolkit:TabContainer ID="TabContainer1" runat="server" TabStripPlacement="Top">
    <ajaxToolkit:TabPanel runat="server" ID="JobPanel" HeaderText="Job Info">
    <ContentTemplate>
    <asp:UpdatePanel ID="updatePanel1" runat="server">
    <ContentTemplate>
    <table>
        <tr>
            <td class="left_column">
                <%--hidden fields needed for table joins to save data--%>
                <asp:Label ID="lblClient" runat="server" Text="Client:"></asp:Label>
                <asp:HiddenField ID="hfClientLocationID" runat="server" />
                <asp:HiddenField ID="hfClientID" runat="server" />
            </td>
            <td class="right_column">
                <asp:DropDownList ID="ddlClient" OnSelectedIndexChanged="ddlClient_SelectedIndexChanged" runat="server">
                </asp:DropDownList>
                <ajaxToolkit:CascadingDropDown ID="cddlClient" runat="server" Category="ClientID" LoadingText="Loading..." PromptText="Select Client" ServiceMethod="GetClients" ServicePath="~/ServiceCS.asmx" TargetControlID="ddlClient">
                </ajaxToolkit:CascadingDropDown>
            </td>
            <td class="right_column">
                <asp:DropDownList ID="ddlLoc" AutoPostBack="true" OnSelectedIndexChanged="ddlLoc_SelectedIndexChanged" runat="server">
                </asp:DropDownList>
                <ajaxToolkit:CascadingDropDown ID="cdlLocs" 
                    runat="server" 
                    Category="ClientLocationID" 
                    LoadingText="Loading..." 
                    ParentControlID="ddlClient" 
                    PromptText="Select Location" 
                    PromptValue="" 
                    ServiceMethod="GetLocations" 
                    ServicePath="~/ServiceCS.asmx" 
                    TargetControlID="ddlLoc">
                </ajaxToolkit:CascadingDropDown>
            </td>
        </tr>
    </table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="updatePanel2" runat="server">
<ContentTemplate>
    <table>
        <tr>
            <td class="right_column">
                <input type="text" id="txtSearch" name="txtSearch" style="text-align:left; width:300px;" />
            </td>
            <td class="right_column">
                <asp:TextBox ID="txtScopeID" CssClass="IDbox" Visible="false" runat="server"></asp:TextBox>
            </td>
        </tr>
    </table>
    </ContentTemplate>
    </asp:UpdatePanel>
    </ContentTemplate>
    </ajaxToolkit:TabPanel>
    </ajaxToolkit:TabContainer>
    </asp:TableCell>
</asp:TableRow>
</asp:Table>

背后的相关vb代码:

Protected Sub ddlLoc_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim con As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("xConnectionString").ConnectionString)
    Dim dt As New DataTable()
    Dim adp As New SqlDataAdapter()
    Dim sql As String = ""
    Dim cliLocTmp As String = ""
    Dim cliCodeTmp As String = ""
    Dim cliLocReset As String = ""
    cliCodeTmp = ddlClient.SelectedItem.Text
    cliCodeTmp = Trim(Left(cliCodeTmp, InStr(cliCodeTmp, " |")))
    cliLocReset = ddlLoc.SelectedItem.Text
    cliLocTmp = ddlLoc.SelectedItem.Text
    cliLocTmp = Trim(Left(cliLocTmp, InStr(cliLocTmp, " |")))
    sql &= "SELECT ClientLocationID"
    sql &= "     , ClientName"
    sql &= "     , CliLocNumber"
    sql &= "     , CliLocName"
    sql &= "     , CliLocCity"
    sql &= "     , CliLocState"
    sql &= "     , CliLocZip"
    sql &= "     , CliLocLocation"
    sql &= "     , CliLocManager"
    sql &= "     , CliLocPhone"
    sql &= "     , CliLocNotes"
    sql &= "  FROM view_ClientLocCombo vclc inner join Client c on vclc.ClientID = c.ClientID"
    sql &= " WHERE c.clientcode = '" & cliCodeTmp & "'"
    sql &= "   AND CliLocNumber = '" & cliLocTmp & "'"
    Dim cmd As New SqlCommand(sql, con)
    adp.SelectCommand = cmd
    adp.Fill(dt)
    If dt.Rows.Count > 0 Then
        txtManager.Text = dt.Rows(0)(8).ToString
        txtPhone.Text = dt.Rows(0)("CliLocPhone").ToString
        txtName.Text = dt.Rows(0)("CliLocName").ToString
        txtAddress.Text = dt.Rows(0)("CliLocLocation").ToString
        txtCity.Text = dt.Rows(0)("CliLocCity").ToString
        txtState.Text = dt.Rows(0)("CliLocState").ToString
        txtZip.Text = dt.Rows(0)("CliLocZip").ToString
        txtNotes.Text = dt.Rows(0)("CliLocNotes").ToString
        hfClientLocationID.Value = dt.Rows(0)("ClientLocationID").ToString
        toptxtCLICODE.Text = cliCodeTmp
        toptxtCLILOCNO.Text = dt.Rows(0)("CliLocNumber").ToString
        toptxtCITYST.Text = dt.Rows(0)("CliLocCity").ToString & ", " & dt.Rows(0)("CliLocState").ToString
        toptxtDESC.Text = dt.Rows(0)("CliLocNotes").ToString
        ' Populates correct value on breakpoint. Is populated with incorrect value in browser.
        ddlLoc.SelectedItem.Text = cliLocReset
    End If
    dt.Clear()
    dt.Dispose()
    adp.Dispose()
End Sub
Protected Sub ddlClient_SelectedIndexChanged(sender As Object, e As EventArgs)
    txtManager.Text = ""
    txtPhone.Text = ""
    txtName.Text = ""
    txtAddress.Text = ""
    txtCity.Text = ""
    txtState.Text = ""
    txtZip.Text = ""
    txtNotes.Text = ""
    hfClientLocationID.Value = ""
End Sub
    Protected Sub btnQuickSearch_Click(sender As Object, e As EventArgs) Handles btnQuickSearch.Click
        Me.btnQuickSearch.Attributes.Add("onclick", "javascript:return QuickSearchPopup()")
    End Sub
End Class

整个网络方法:

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports AjaxControlToolkit
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Web.Script.Services
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class ServiceCS
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetClients(knownCategoryValues As String) As CascadingDropDownNameValue()
Dim query As String = "SELECT ClientCode + ' | ' + ClientName"
query &= "     , ClientID"
query &= "  FROM view_ClientCombo"
query &= " ORDER BY ClientCode"
Dim clients As List(Of CascadingDropDownNameValue) = GetData(query)
Return clients.ToArray()
End Function
<WebMethod()> _
Public Function GetLocations(knownCategoryValues As String) As CascadingDropDownNameValue()
Dim client As String = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)("ClientId")
'Dim query As String = String.Format("SELECT CliLocNumber FROM view_ClientLocation WHERE ClientID = {0}", client)
Dim query As String = String.Format("SELECT CliLocNumber + ' | ' + CliLocCity + ' | ' + CliLocState, replace(CliLocName, '''', '''''') FROM dbo.view_ClientLocCombo WHERE ClientID = {0}", client)
Dim locations As List(Of CascadingDropDownNameValue) = GetData(query)
Return locations.ToArray()
End Function
Private Function GetData(query As String) As List(Of CascadingDropDownNameValue)
Dim conString As String = ConfigurationManager.ConnectionStrings("HANDYMANConnectionString").ConnectionString
Dim cmd As New SqlCommand(query)
Dim values As New List(Of CascadingDropDownNameValue)()
Using con As New SqlConnection(conString)
con.Open()
cmd.Connection = con
Using reader As SqlDataReader = cmd.ExecuteReader()
    While reader.Read()
        values.Add(New CascadingDropDownNameValue() With { _
         .name = reader(0).ToString(), _
         .value = reader(1).ToString() _
        })
    End While
    reader.Close()
    con.Close()
    Return values
End Using
End Using
End Function
<WebMethod()> _
Public Function GetScopes(ByVal sLookUP As String) As String()
Dim scopes As New List(Of String)()
Using conn As New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("HANDYMANConnectionString").ConnectionString
Using cmd As New SqlCommand()
    cmd.CommandText = "SELECT JobScopeDesc, JobScopeID FROM view_JobScopeCode where JobScopeDesc like '%' + @SearchText + '%' ORDER BY JobScopeDesc"
    cmd.Parameters.AddWithValue("@SearchText", sLookUP)
    cmd.Connection = conn
    conn.Open()
    Using sdr As SqlDataReader = cmd.ExecuteReader()
        While sdr.Read()
            scopes.Add(String.Format("{0}-{1}", sdr("JobScopeDesc").ToString(), sdr("JobscopeID")))
        End While
    End Using
    conn.Close()
End Using
Return scopes.ToArray()
End Using
End Function
End Class

根据要求编辑为第一个下拉列表添加所有aspx代码和autopostback代码。

根据要求编辑后面添加所有vb.NET代码。遗漏错误检查代码,这是问题框的限制。

编辑:此代码在使用ddlloc下拉列表中的10多个选项进行测试后停止调用代码隐藏。

根据要求编辑以减少代码。

编辑:在填写文本框后页面最初返回时,下拉列表显示正确的值。然后页面闪烁,并在下拉列表中选择了错误的值。这听起来像第二页加载,但是当我浏览代码时,调试器不会再次转到页面加载方法。如果确实如此,它将在下拉列表中显示“选择位置”条目。我真的想要奖励这笔赏金。

1 个答案:

答案 0 :(得分:0)

听起来好像你的Ajax请求被缓存了。

尝试(缓存:false) 像这样:

source: function (request, response) {
        $.ajax({
            url: "ServiceCS.asmx/GetScopes",
      cache: false,
            data: "{ 'sLookUP': '" + request.term + "' }",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataFilter: function (data) { return data; },
            success: function (data) {
                response($.map(data.d, function (item) {
                    return {
                        value: item,
                    }
                }))
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }
        });
    },