0

我有一个 Asp 服务器控制按钮,onClick用于处理后面代码中的代码和onClientClick处理 javascript 代码。代码是:

更新:根据 Icarus 解决方案,更新代码:

按钮来源:

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
        style="z-index: 1; left: 648px; top: 202px; position: absolute" 
        Text="Show route" OnClientClick="droute(); return false" />

<asp:HiddenField ID="hdncroute" runat="server" /> 

后面的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    using (con = new MySqlConnection("server=localhost; uid=root;password=as64ws;database=Gmaps"))
    da = new MySqlDataAdapter("select * from routes where uid='" + Session["uname"].ToString() + "'", con);
    da.Fill(ds, "mroute");
    foreach (DataRow r in ds.Tables[0].Rows)
    {
        uroute.Add(r["latlng"].ToString());
    }
    croute = new string[uroute.Count];
    croute = uroute.ToArray();
    hdncroute.Value = string.Join("&", croute);
}

Javascript函数:

function droute()
{
    var route=[];
    var temp;
    temp = eval(document.getElementById('<%= hdncroute.ClientID %>').value);
    route= temp.split('&');
    //Polyline icon n line settings
    var iconsetngs= {path:google.maps.SymbolPath.FORWARD_CLOSED_ARROW, fillColor:'#FF0000'};
    var polylineoptns= {strokeColor:'#3333FF',strokeOpacity:0.8,strokeWeight:3,map:map,icons:[{icon:iconsetngs,offset:'100%'}]};
    polyline= new google.maps.Polyline(polylineoptns);

    //Loop to add locations and draw line
    var path= polyline.getPath();
    for(var i=0;i<route.length;i++)
        {
            var marker= new google.maps.Marker({position:route[i],map:map});
            path.push(route[i]);
            google.maps.event.addListener(marker,'click',showiwindow);
        }

    //Event Listener's
    function showiwindow(event)
    {
    iwindow.setContent("<b>Coordinates are:</b><br/>Latitude:"+event.latLng.lat()+"<br/>Longitude:"+event.latLng.lng());
    iwindow.open(map,this);
    }  
}

我知道编写return falsejavascript 函数将避免刷新,并且 onClick 具有 void 返回类型。但是我的页面仍然会在按钮单击时重新加载。

4

2 回答 2

2

你在这里有一个错误:

route = document.getElementById('<%= croute %>').value;

它应该是:

route = document.getElementById('<%= croute.ClientID %>').value;

更新:

标记 - 在页面中声明一个隐藏元素

<asp:hiddenfield id="hdnCroute" runat="server" />

//code behind

int [] croute = ... 

hdnCroute.Value = "["+string.Join(",",croute)+"]";

现在Javascipt:

//now you have an array back in javascript 
var route= eval(document.getElementById('<%= hdnCroute.ClientID %>').value); 
于 2012-09-09T06:21:03.317 回答
1

为什么页面重新加载?

OnClientClick="droute(); return false" 

在浏览器内部是这样的:

button.onclick = function(){
    droute(); 
    return false
};

虽然 droute 出错了,所以,return false不起作用。

于 2012-09-09T09:57:42.027 回答