Jquery自动完成搜索无法在asp.net中的usercontrol(ascx)页面中工作

时间:2014-11-06 07:54:49

标签: c# jquery asp.net

我在Asp.net网站的UserControle(ascx)页面上有文本框,我在这个文本框中使用automcomplete搜索,当我把这个文本框放在aspx页面上时,这工作正常。但是在ascx(usercontrol)页面中它无效。

我的文本框是:

  <asp:TextBox ID="txtCityUsed" Style="width: 91.2%; height: 13px; float: left;"
                                                            Width="" runat="server"> 

我在用户控制页面的页面加载下调用以下两种方法,

protected void SetAutocompleteTextbox()
{
    StringBuilder sbLVAutocomplete;
    try
    {
        sbLVAutocomplete = new StringBuilder();
        sbLVAutocomplete.AppendLine("<script type=\"text/javascript\">");
        sbLVAutocomplete.AppendLine("           function autoFetchRecord_txtCityUsed() {");
        sbLVAutocomplete.AppendLine("               if (document.getElementById(\"" + txtCityUsed.ClientID + "\").value.length >=1) {");
        sbLVAutocomplete.AppendLine("                   $(\"#" + txtCityUsed.ClientID + "\").autocomplete({");
        sbLVAutocomplete.AppendLine("                       source: function(request, response) {");
        sbLVAutocomplete.AppendLine("                           $.ajax({");
        sbLVAutocomplete.AppendLine("                               type: \"POST\",");
        sbLVAutocomplete.AppendLine("                               contentType: \"application/json; charset=utf-8\",");
        sbLVAutocomplete.AppendLine("                               url: \"LandingForPortal.aspx/GetListOfCity\",");
        sbLVAutocomplete.AppendLine("                               data: \"{'Name':'\" + document.getElementById('" + txtCityUsed.ClientID + "').value + \"','strRiskStartDate':'\" + document.getElementById('" + txtPolicyStartDate.ClientID + "').value + \"'}\",");
        sbLVAutocomplete.AppendLine("                               dataType: \"json\",");
        sbLVAutocomplete.AppendLine("                               success: function (data) {");
        sbLVAutocomplete.AppendLine("                                   if (data.d.length == 0){ ");
        sbLVAutocomplete.AppendLine("                                       alert('No Data Found!');");
        sbLVAutocomplete.AppendLine("                                       document.getElementById('" + txtCityUsed.ClientID + "').value =\"\";");
        sbLVAutocomplete.AppendLine("                                       document.getElementById('" + hdnVehicleUsedCityCode.ClientID + "').value =\"\";");
        sbLVAutocomplete.AppendLine("                                       document.getElementById('" + hdnVehicleUsedOfficeCode.ClientID + "').value =\"\";");
        sbLVAutocomplete.AppendLine("                                       document.getElementById('" + hdnVehicleUsedOfficeName.ClientID + "').value =\"\";");
        sbLVAutocomplete.AppendLine("                                       document.getElementById('" + hdnVehicleUsedParentOfficeCode.ClientID + "').value =\"\";");
        sbLVAutocomplete.AppendLine("                                   }");
        sbLVAutocomplete.AppendLine("                                   response($.map(data.d, function (item) {");
        sbLVAutocomplete.AppendLine("                                       return {");
        sbLVAutocomplete.AppendLine("                                           value: item.City,");
        sbLVAutocomplete.AppendLine("                                           CityCode: item.CityCode,");
        sbLVAutocomplete.AppendLine("                                           OfficeCode: item.OfficeCode,");
        sbLVAutocomplete.AppendLine("                                           OfficeLocation: item.OfficeLocation,");
        sbLVAutocomplete.AppendLine("                                           ParentOfficeCode: item.ParentOfficeCode");
        sbLVAutocomplete.AppendLine("                                       }");
        sbLVAutocomplete.AppendLine("                                   }))");
        sbLVAutocomplete.AppendLine("                                   },");
        sbLVAutocomplete.AppendLine("                                   error: function (result) {");
        sbLVAutocomplete.AppendLine("                                       alert(data);");
        sbLVAutocomplete.AppendLine("                                   }");
        sbLVAutocomplete.AppendLine("                           });");
        sbLVAutocomplete.AppendLine("                       },");
        sbLVAutocomplete.AppendLine("                       ");
        sbLVAutocomplete.AppendLine("                       open: function(){");
        sbLVAutocomplete.AppendLine("                           $('.ui-autocomplete').css('width', '300');");
        sbLVAutocomplete.AppendLine("                       },");
        sbLVAutocomplete.AppendLine("                       select: function (event, ui) {");
        sbLVAutocomplete.AppendLine("                           if (ui.item) {");
        sbLVAutocomplete.AppendLine("                               $('#" + txtCityUsed.ClientID + "').val(ui.item.value);");
        sbLVAutocomplete.AppendLine("                               $('#" + hdnVehicleUsedCityCode.ClientID + "').val(ui.item.CityCode);");
        sbLVAutocomplete.AppendLine("                               $('#" + hdnVehicleUsedOfficeCode.ClientID + "').val(ui.item.OfficeCode);");
        sbLVAutocomplete.AppendLine("                               $('#" + hdnVehicleUsedOfficeName.ClientID + "').val(ui.item.OfficeLocation);");
        sbLVAutocomplete.AppendLine("                               $('#" + hdnVehicleUsedParentOfficeCode.ClientID + "').val(ui.item.ParentOfficeCode);");
        sbLVAutocomplete.AppendLine("                               return false;");
        sbLVAutocomplete.AppendLine("                           }");
        sbLVAutocomplete.AppendLine("                       },");
        sbLVAutocomplete.AppendLine("                       minLength: 3");
        sbLVAutocomplete.AppendLine("                   });");
        sbLVAutocomplete.AppendLine("               }");
        sbLVAutocomplete.AppendLine("           }");
        sbLVAutocomplete.AppendLine("</script>");

        if (!Page.ClientScript.IsStartupScriptRegistered("Autocomplete"))
        {
            //Registering startup script Autocomplete
            Page.ClientScript.RegisterStartupScript(typeof(string), "Autocomplete", sbLVAutocomplete.ToString());
        }
    }
    catch (Exception ex)
    {
        //ErrorMessage("ERROR", "ERROR from SetAutocompleteTextbox()" + ex.Message);

    }
    finally
    {
    }
}
protected void AddAttributesAutoComplete()
{
    try
    {
        txtCityUsed.Attributes.Add("onkeyup", "autoFetchRecord_txtCityUsed();");

    }
    catch (Exception ex)
    {
        //  ErrorMessage("ERROR", "ERROR from AddAttributesAutoComplete()" + ex.Message);
    }
    finally
    {
    }
}

这是我在文本框中输入搜索文本时接听电话的方法

[System.Web.Services.WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static object GetListOfCity(string Name, string strRiskStartDate)
{
    ServiceResult arrLVServiceResult;
    GCIntegrationClient objCVGCIntegrationClient = new GCIntegrationClient();
    arrLVServiceResult = objCVGCIntegrationClient.CityWherePrimarilyUsedLOV_GetObjectData(Convert.ToString(strProductCode), Convert.ToString(strTokenId), Convert.ToString(strRiskStartDate), Convert.ToString(""), Convert.ToString(Name));
    return arrLVServiceResult.UserData.CityGrid;
}

但是当我在usercontrol(ascx)页面输入搜索文本进行自动完成搜索时,上面的方法没有得到调用,但是发生了page_load。虽然页面加载不应该发生,只有上面的方法应该调用.... 请任何身体帮助我...我无法更改页面,也无法使用popup进行自动搜索

0 个答案:

没有答案