检查特殊字符的数据源数据&空领域 [英] Check Datasource Data For Special Characters & Empty Fields

查看:96
本文介绍了检查特殊字符的数据源数据&空领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张3k左右的地址表。



我使用google maps api为这些地址绘制标记。



我有一个标记数组它由一个链接到我的数据库表的转发器控件填充。



这一切都正常但我遇到了一个问题。当任何转发器数据字段(即'name'或'address1')为空或包含特殊字符(如撇号)时,地图不会显示。



我该如何修复空白和特殊字符问题?我是否需要先将其实际放入数据集中?



以下代码,感谢您的帮助。





< script type =text / javascript> 
var markers = [

< asp:Repeater ID =Repeater1runat =serverDataSourceID =SqlDataSource1>
< ItemTemplate>
{
custcode:'<%#Eval(custcode)%>',
lat:'<%#Eval(lat)%> ;',
lng:'<%#Eval(lng)%>',
//tel:'<%#Eval(telephone)%> ;',
//地址:'<%#Eval(address1)%>',
//邮政编码:'<%#Eval(邮政编码) %>',
name:'<%#Eval(fullname)%>',
category:'<%#Eval(category)%> ;'

}
< / ItemTemplate>
< SeparatorTemplate>

< / SeparatorTemplate>
< / asp:Repeater>
];
< / script>
< script type =text / javascript>

window.onload = function(){
var mapOptions = {
center:new google.maps.LatLng(markers [0] .lat,markers [0] .lng ),
zoom:6,
mapTypeId:google.maps.MapTypeId.ROADMAP
};

var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById(map-canvas),mapOptions);
for(i = 0; i< markers.length; i ++){
var data = markers [i]
var myLatlng = new google.maps.LatLng(data.lat,data .lng);
var marker = new google.maps.Marker({
position:myLatlng,
map:map,
title:data.title
});
(函数(标记,数据){
google.maps.event.addListener(marker,click,function(e){
infoWindow.setContent(+ data .name +< br />+ data.custcode +< br />+
+ data.postcode +< br />+ data.tel + < br />+ data.category);
infoWindow.open(map,marker);
});
})(标记,数据);
}
}
< / script>

解决方案

您需要正确编码数据:

< ItemTemplate> 
{
custcode <%#HttpUtility.JavaScriptStringEncode(Eval( custcode { 0 } ))%>
lat <%#HttpUtility.JavaScriptStringEncode(Eval( lat { 0 } ))%>
lng <%#HttpUtility.JavaScriptStringEncode(Eval( lng { 0 } ))%>
// tel:<%#HttpUtility.JavaScriptStringEncode(Eval(telephone,{0}))%>,
// address:<%#HttpUtility.JavaScriptStringEncode(Eval(address1,{0}))% >,
// postcode:<%#HttpUtility.JavaScriptStringEncode (Eval(postcode,{0}))%>,
name <%#HttpUtility.JavaScriptStringEncode(Eval( fullname) { 0 } ))%>
category <%#HttpUtility.JavaScriptStringEncode(Eval( category { 0 } ))%>
}
< / ItemTemplate >


Hi, I have a table with 3k or so addresses.

I am using google maps api to plot markers for these addresses.

I have a markers array which is populated by a repeater control which is linked to my Database table.

This all works fine but I have come across a problem. When any of the repeater data field i.e 'name' or 'address1' is blank or contains a special character such as an apostrophe, the map doesn't show.

How would I go about fixing the blank and special chars issue? Do I need to pragmatically put this into a dataset first?

Code below, thanks for your help.


   <script type="text/javascript">
var markers = [

  <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
 <ItemTemplate>
            {
            "custcode": '<%# Eval("custcode") %>',
            "lat": '<%# Eval("lat") %>',
            "lng": '<%# Eval("lng") %>',
            //"tel": '<%# Eval("telephone") %>',
            //"address": '<%# Eval("address1") %>',
            //  "postcode": '<%# Eval("postcode") %>',
            "name": '<%# Eval("fullname") %>',
              "category": '<%# Eval("category") %>'

        }
</ItemTemplate>
<SeparatorTemplate>
    ,
</SeparatorTemplate>
</asp:Repeater>
];
    </script>
    <script type="text/javascript">

        window.onload = function () {
            var mapOptions = {
                center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
                zoom: 6,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            var infoWindow = new google.maps.InfoWindow();
            var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
            for (i = 0; i < markers.length; i++) {
                var data = markers[i]
                var myLatlng = new google.maps.LatLng(data.lat, data.lng);
                var marker = new google.maps.Marker({
                    position: myLatlng,
                    map: map,
                    title: data.title
                });
                (function (marker, data) {
                    google.maps.event.addListener(marker, "click", function (e) {
                        infoWindow.setContent("" + data.name + "<br />" + data.custcode + "<br />" +
                    +data.postcode + "<br />" + data.tel + "<br />" + data.category);
                        infoWindow.open(map, marker);
                    });
                })(marker, data);
            }
        }
    </script>

解决方案

You need to properly encode your data:

<ItemTemplate>
{
    "custcode": "<%# HttpUtility.JavaScriptStringEncode(Eval("custcode", "{0}")) %>",
    "lat": "<%# HttpUtility.JavaScriptStringEncode(Eval("lat", "{0}")) %>",
    "lng": "<%# HttpUtility.JavaScriptStringEncode(Eval("lng", "{0}")) %>",
    //"tel": "<%# HttpUtility.JavaScriptStringEncode(Eval("telephone", "{0}")) %>",
    //"address": "<%# HttpUtility.JavaScriptStringEncode(Eval("address1", "{0}")) %>",
    //"postcode": "<%# HttpUtility.JavaScriptStringEncode(Eval("postcode", "{0}")) %>",
    "name": "<%# HttpUtility.JavaScriptStringEncode(Eval("fullname", "{0}")) %>",
    "category": "<%# HttpUtility.JavaScriptStringEncode(Eval("category", "{0}")) %>"
}
</ItemTemplate>


这篇关于检查特殊字符的数据源数据&amp;空领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆