1

最近几天我面临这个问题...需要专家建议...

我正在尝试从后面代码中的 URL 读取查询字符串值。如果查询字符串可用,那么我需要对其进行地理编码。并且该功能在同一个 .aspx 页面上。

该函数将从谷歌获取地理编码(LAT,LNG)并将其分配给页面上可用的隐藏变量。之后我需要运行我的 c# 代码,然后获取 LAT、LNG 值并进入数据库以查找最近的商店。

Page_Load

protected void Page_Load(object sender, EventArgs e)
{
    _queryStringVal = Request.QueryString["k"];
    string eventTarget = Request["__EVENTTARGET"];
    string eventArgument = Request["__EVENTARGUMENT"];
    if (!Page.IsPostBack)
    {
      if (!String.IsNullOrEmpty(_queryStringVal))
      {
        qsText.Value = _queryStringVal;
        BindListView();
      }
    }
    else
    {
      if (!String.IsNullOrEmpty(eventArgument) || !String.IsNullOrEmpty(eventTarget))
      {
        Lat = Convert.ToDouble(eventTarget);
        Long = Convert.ToDouble(eventArgument);
        BindListView();
      }
    }
}

protected void BindListView()
{
    var parameter = _queryStringVal;

    if (!string.IsNullOrEmpty(parameter))
    {
      ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "<script type='text/javascript'>alert('hello');</script>", true);
      //ScriptManager.RegisterStartupScript(this, GetType(), "GeoCodeAddress", "codeAddress(" + qsText.Value + ");", true);
    }

    Lat = Convert.ToDouble(currentLat.Value); // this is where error occurs because the value is NULL
    Long = Convert.ToDouble(currentLong.Value);

  if ((Lat == 0.00) || Long == 0.00)
    {
          ShowErrorPanel();
          return;
    }
}

如您所见,当使用查询字符串参数加载页面时,也不会显示简单的警报。

我需要codeAddress(param)在 c# 尝试分配 Lat、Long 值之前运行该函数。

请帮忙。

编辑

当查询字符串值不存在时,页面与文本框一起显示。当用户点击按钮时,通过javascript我重定向到没有查询字符串的同一页面,它工作正常。我的主页需要查询字符串。我在页面上有一个通用搜索框,如果您在其中输入邮政编码或城镇,它将通过查询将其重定向到 stroes 页面并显示最近的商店。所以这就是为什么我需要查询才能工作..

JAVASCRIPT

$(document).ready(function () {
    var param = getParameterByName("k"); // function to get querystring param
    if (param.length > 1) {
          codeAddress(param);
    }
});

function codeAddress(addressToGeoCode) {
    var geocoder = new window.google.maps.Geocoder();
    geocoder.geocode({ 'address': addressToGeoCode }, function (results, status) {
      if (status == window.google.maps.GeocoderStatus.OK) {
            var firstLoc = results[0].geometry.location;
            lathv.val(firstLoc.lat());
            lnghv.val(firstLoc.lng());
            PageRedirect();
      }
      else {
            console.log('No results found: ' + status);
      }
    });
}

function PageRedirect() {
    //window.location.href = window.location.href.split('?')[0] + "?k=" + $('.AddressSearch').val();
   __doPostBack(lathv.val(), lnghv.val());
}
4

0 回答 0