gpt4 book ai didi

javascript - 避免在 ASP.NET 中单击具有 onClick 和 onClientClick 事件的按钮时重新加载页面

转载 作者:行者123 更新时间:2023-11-28 20:51:32 24 4
gpt4 key购买 nike

我有一个 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);
}
}

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

最佳答案

这里有一个错误:

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);

关于javascript - 避免在 ASP.NET 中单击具有 onClick 和 onClientClick 事件的按钮时重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12336917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com