在javascript函数中从lable获取null值

时间:2021-08-07 09:51:17

i have written the following code and it gets perfect value of lbllat and lbllon:

我写了下面的代码,它获得了lbllat和lbllon的完美价值:

<asp:UpdatePanel ID="updatepanel_vehicleinfo" runat="server" OnLoad="updatepanel_vehicleinfo_Load">
                <ContentTemplate>
                    <asp:Repeater ID="vehicle_info" runat="server">
                        <ItemTemplate>

            <asp:Label ID="lbllat" runat="server" Text='<%#Eval("lat") %>'></asp:Label><br />

            <asp:Label ID="lbllon" runat="server" Text='<%#Eval("lon") %>'></asp:Label><br />
    </ItemTemplate>
                    </asp:Repeater>
 <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
                    <script type="text/javascript">
                        function initialize() {
                            var lat = document.getElementById('<%=lbllat.ClientID %>').value;
                            var lon = document.getElementById('<%=lbllon.ClientID %>').value;
                            var myLatlng = new google.maps.LatLng(lat, lon)
                            var mapOptions = {
                                center: myLatlng,
                                zoom: 6,
                                mapTypeId: google.maps.MapTypeId.ROADMAP,
                                marker: true
                            };
                            var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
                            var marker = new google.maps.Marker({
                                position: myLatlng
                            });
                            marker.setMap(map);
                        }
                    </script>
                </ContentTemplate>


            </asp:UpdatePanel>

 <br />

        <div id="map_canvas" style="width: 500px; height: 500px">
        </div>

now when i call the javascript function from the codebehind like below:

现在,当我从代码隐藏中调用javascript函数时,如下所示:

 DataSet dsvehicle_info = new DataSet();
        dsvehicle_info = cls.ReturnDataSet("RetriveData_Alias1",
             new SqlParameter("@Field", "lat,lon"),
            new SqlParameter("@TblNm", "current_gps_data left join device_master on device_master.id=current_gps_data.id"),
         new SqlParameter("@WhereClause", "where current_gps_data .id=24"));


        vehicle_info.DataSource = dsvehicle_info;
        vehicle_info.DataBind();
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Javascript", "javascript:initialize();", true);

It gives perfect answer of lat and lot and it is also printed right in repeater but when i am calling javascript then the lat and lon values set to null

它提供了lat和lot的完美答案,它也在转发器中打印,但是当我调用javascript时,lat和lon值设置为null

so how can i get right value of lat and lon in javascript ?

那么如何才能在javascript中获得正确的lat和lon值?

2 个解决方案

#1


2  

    </ContentTemplate>

 Lat: <asp:Label ID="lat" runat="server"></asp:Label><br />
  Lon: <asp:Label ID="lon" runat="server"></asp:Label><br />
 </asp:UpdatePanel>

put this label after and before

之前和之后都贴上这个标签

now add the following code in you cs file: put the below code after the

现在在你的cs文件中添加以下代码:将下面的代码放在后面

  vehicle_info.DataSource = dsvehicle_info;
  vehicle_info.DataBind();

add belo code:

添加belo代码:

   lat.Text = dsvehicle_info.Tables[0].Rows[0]["lat"].ToString();
   lon.Text = dsvehicle_info.Tables[0].Rows[0]["lon"].ToString();

now in the javascript change the below two lines of code:

现在在javascript中更改以下两行代码:

  var lat = document.getElementById('<%=lat.ClientID %>').innerHTML;
  var lon = document.getElementById('<%=lon.ClientID %>').innerHTML;

Now It is done ..!! Enjoy!!

现在已经完成.. !!请享用!!

#2


0  

It looks like you have repeating ID values in your repeater. Have you tried assigning a class name to each label and then storing it in an HTML collection instead?

看起来你的转发器中有重复的ID值。您是否尝试为每个标签分配一个类名,然后将其存储在HTML集合中?

Something like this for the label controls:

对于标签控件,这样的东西:

<asp:Label CssClass="lbllat" runat="server" Text='<%#Eval("lat") %>'></asp:Label><br /><asp:Label CssClass="lbllon" runat="server" Text='<%#Eval("lon") %>'></asp:Label><br />

Then in your JS:

然后在你的JS中:

var lblats = document.getElementsByClassName('lbllat');
var lbllons = document.getElementsByClassName('lbllat');

The lblats and lbllons HTML collections should then be available for you to iterate or access via their indices.

然后,lblats和lbllons HTML集合可供您通过其索引进行迭代或访问。

#1


2  

    </ContentTemplate>

 Lat: <asp:Label ID="lat" runat="server"></asp:Label><br />
  Lon: <asp:Label ID="lon" runat="server"></asp:Label><br />
 </asp:UpdatePanel>

put this label after and before

之前和之后都贴上这个标签

now add the following code in you cs file: put the below code after the

现在在你的cs文件中添加以下代码:将下面的代码放在后面

  vehicle_info.DataSource = dsvehicle_info;
  vehicle_info.DataBind();

add belo code:

添加belo代码:

   lat.Text = dsvehicle_info.Tables[0].Rows[0]["lat"].ToString();
   lon.Text = dsvehicle_info.Tables[0].Rows[0]["lon"].ToString();

now in the javascript change the below two lines of code:

现在在javascript中更改以下两行代码:

  var lat = document.getElementById('<%=lat.ClientID %>').innerHTML;
  var lon = document.getElementById('<%=lon.ClientID %>').innerHTML;

Now It is done ..!! Enjoy!!

现在已经完成.. !!请享用!!

#2


0  

It looks like you have repeating ID values in your repeater. Have you tried assigning a class name to each label and then storing it in an HTML collection instead?

看起来你的转发器中有重复的ID值。您是否尝试为每个标签分配一个类名,然后将其存储在HTML集合中?

Something like this for the label controls:

对于标签控件,这样的东西:

<asp:Label CssClass="lbllat" runat="server" Text='<%#Eval("lat") %>'></asp:Label><br /><asp:Label CssClass="lbllon" runat="server" Text='<%#Eval("lon") %>'></asp:Label><br />

Then in your JS:

然后在你的JS中:

var lblats = document.getElementsByClassName('lbllat');
var lbllons = document.getElementsByClassName('lbllat');

The lblats and lbllons HTML collections should then be available for you to iterate or access via their indices.

然后,lblats和lbllons HTML集合可供您通过其索引进行迭代或访问。