如何从VB.NET代码后面调用javascript [英] How do I call javascript from VB.NET code behind
问题描述
我有一个网页,我在其中显示一个asp网格,其中包含来自sql server数据库的数据和它下面的Leaflet地图。我想要做的是让用户点击网格中的项目,然后在Leaflet地图上放置一个图钉,根据所选项目显示数据。
我遇到的问题是应该设置引脚的功能似乎没有被调用。
到目前为止,我的aspx文件的代码是:
I have a webpage that where I display an asp grid filled with data from a sql server database and a Leaflet map below it. What I would like to do is have the user click on an item in the grid, then put a pin on the Leaflet map showing data based on the chosen item.
The problem that I am having is that the function that should be setting the pin does not appear to be called.
What I have so far is this code for my aspx file:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="VB.aspx.vb" Inherits="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Leaflet Web Map</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<style>
#map {
width: 100%;
height:500px;
}
</style>
</head>
<body>
<form id="frmACDC" runat="server">
<div style="text-align: center;">
<asp:Label ID="ACDCHdr" runat="server" Font-Names="Georgia,Times New Roman"
ForeColor="Blue" Font-Size="36pt">ACDC</asp:Label>
<hr/>
</div>
<div>
<tr>
<td align="center" style="padding: 50px">
<div style="width: 100%; text-align=center;">
<asp:GridView ID="gvF25" runat="server" AllowPaging="True"
AutoGenerateColumns="False" PageSize="12" AllowSorting="True" CellPadding="2"
CellSpacing="2" HorizontalAlign="center" Width="50%">
<Columns>
<asp:ButtonField ButtonType="Link" DataTextField="Reference_Number" CommandName="SelectRef" HeaderText="Reference Number" ItemStyle-Width="19%" />
<asp:BoundField DataField="From_PS" HeaderText="State Code" ReadOnly="true" ItemStyle-Width="9%"/>
</Columns>
<HeaderStyle BackColor="#E6E6FA" Font-Bold="True" />
</asp:GridView>
<HeaderStyle BackColor="#E6E6FA" Font-Bold="True" />
</asp:GridView>
<asp:Label ID="lblJava" runat="server" ForeColor="Blue" Font-Bold="true"></asp:Label>
</td>
</tr>
</div>
<div id="map" ></div>
<script language=javascript>
var map = L.map('map',{zoom: 15});
map.setView(new L.LatLng(59.334591, 18.063240),5);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var marker = L.marker([59.334591, 18.063240]).addTo(map).bindPopup("Here").openPopup();
function SetMap(Lat, Long, strText) {
map.setView(new L.LatLng(Lat, Long),5);
marker = L.marker([Lat, Long]).addTo(map).bindPopup(strText).openPopup();
}
</script>
</form>
</body>
</html>
然后我的代码落后:
and then my code behind:
Imports System.Data.SqlClient
Imports System.Data
Partial Class VB
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
gvF25.DataSource = GetData("select Reference_number, From_PS from F25_master")
gvF25.DataBind()
End If
End Sub
Private Shared Function GetData(query As String) As DataTable
Dim strCON = "Server=MyServer;Database=MyDB;Uid=xxx;Pwd=xxx;MultipleActiveResultSets=true"
Using con As New SqlConnection(strCON)
Using cmd As New SqlCommand()
cmd.CommandText = query
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
Dim dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Public Sub gvF25_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvF25.RowCommand
Dim intRow As Integer
Dim ex As exception
Dim strMessage As String
Dim selRow As GridViewRow
Dim cellText As TableCell
Dim btn As LinkButton
Dim strText as string
Try
If e.CommandName = "SelectRef" Then
introw = Convert.ToInt32(e.CommandArgument)
selRow = gvF25.rows(intRow)
btn = selRow.Cells(0).Controls(0)
cellText = selRow.Cells(1)
if introw = 0 then
System.Web.UI.ScriptManager.RegisterStartupScript(Me, [GetType](), "check_Javascript", "SetMap(59.334591, 18.063240,First Text);", True)
else
System.Web.UI.ScriptManager.RegisterStartupScript(Me, [GetType](), "check_Javascript", "SetMap(59.334591, 18.063240,Different Text);", True)
end if
End If
Catch ex
'Me.lblerror.text = "An error has occured. Please have TK admin review the error log."
End Try
End Sub
End Class
我尝试了什么:
我已经尝试将脚本放入标签,但这也不起作用。像这样:
What I have tried:
I have tried putting the script into a label but that doesn't work either. Like this:
Me.lblJava.Text = "<script type='text/javascript'>SetMap(59.334591, 18.063240,Different notfif);</script>"
推荐答案
传递字符串
值引用
Pass thestring
value insidequotes
System.Web.UI.ScriptManager.RegisterStartupScript(Me, [GetType](), "check_Javascript", "SetMap(59.334591, 18.063240,'First Text');", True)
这篇关于如何从VB.NET代码后面调用javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!