按钮的 OnClientClick 和 Click 事件未触发 - FireFox 中的问题
我将 Onclientclick 事件附加到服务器端代码中的按钮,如下所示,
TopPanelButton.OnClientClick = string.Format("if(!ValidData({0},{1},{2},{3})) return false;", txtOD.ClientID, radCmbOD.ClientID, txtgetMe.ClientID, RadAjaxLoadingPanel1.ClientID);
此外,onClick 事件附加到 aspx 页面中的同一按钮,
<asp:Button ID="TopPanelButton" runat="server" Text="Go"
CssClass="CBtn1" Width="30px" Height="21px" OnClick="TopPanelButton_Click" />
如果 onclientclick 返回 true,则应触发服务器端单击事件。调用“ValidateData()”函数来验证表单中的条目。
这段代码在 IE 中运行良好。但在 Firefox 中,这两个事件都不会触发。如果我评论“TopPanelButton.OnClientClick =...”代码,则 onClick 事件将触发。
我可以在下面的代码中的哪里应用此代码 Page.ClientScript.GetPostBackEventReference() 。
TopPanelButton.OnClientClick = string.Format("if(!ValidData({0},{1},{2},{3})) return false;", txtOD.ClientID, radCmbOD.ClientID, txtgetMe.ClientID, RadAjaxLoadingPanel1.ClientID);
Firefox 没有调用 ValidData 函数。我将警报放入 JavaScript 中,但警报消息未在 Firefox 中显示。但 IE 显示警报消息。
我的 validData 函数:
function ValidData(txtOND, ddlOND, txtgetMe, aPanel) {
alert("Entered");
if (!ValidNumber(txtgetMe)) {
aPanel.hide();
return false;
}
if (ddlOND.value == "Origin" || ddlOND.value == "Destination") {
if (!ValidOriginOrDestination(txtOND, ddlOND.value)) {
aPanel.hide();
return false;
}
}
else if (ddlOND.value == "O&D") {
if (!ValidOND(txtOND)) {
aPanel.hide();
return false;
}
}
if (ddlOND.value == "Region Starting with" || ddlOND.value == "Country Starting with" || ddlOND.value == "Budget Station Starting with") {
if (txtOND.value.length == 0) {
radalert("Enter a value for " + ddlOND.value);
aPanel.hide();
return;
}
}
aPanel.show();
return true;
}
I have Onclientclick event attached to the button in serverside code like below,
TopPanelButton.OnClientClick = string.Format("if(!ValidData({0},{1},{2},{3})) return false;", txtOD.ClientID, radCmbOD.ClientID, txtgetMe.ClientID, RadAjaxLoadingPanel1.ClientID);
Also, the onClick event is attached for the same button in the aspx page,
<asp:Button ID="TopPanelButton" runat="server" Text="Go"
CssClass="CBtn1" Width="30px" Height="21px" OnClick="TopPanelButton_Click" />
The serverside click event should fire if the onclientclick return true. The "ValidateData()" function is called to validate the entries in the form.
This code is working fine in IE. But in Firefox, both events are not firig. If I comment the "TopPanelButton.OnClientClick =..." code then onClick event is firing.
Where can I apply this code Page.ClientScript.GetPostBackEventReference() in my code below.
TopPanelButton.OnClientClick = string.Format("if(!ValidData({0},{1},{2},{3})) return false;", txtOD.ClientID, radCmbOD.ClientID, txtgetMe.ClientID, RadAjaxLoadingPanel1.ClientID);
Firefox is not calling the ValidData function. I put alert inside the javascript, but alert message is not shown in Firefox. But IE shows the alert message.
My validData function:
function ValidData(txtOND, ddlOND, txtgetMe, aPanel) {
alert("Entered");
if (!ValidNumber(txtgetMe)) {
aPanel.hide();
return false;
}
if (ddlOND.value == "Origin" || ddlOND.value == "Destination") {
if (!ValidOriginOrDestination(txtOND, ddlOND.value)) {
aPanel.hide();
return false;
}
}
else if (ddlOND.value == "O&D") {
if (!ValidOND(txtOND)) {
aPanel.hide();
return false;
}
}
if (ddlOND.value == "Region Starting with" || ddlOND.value == "Country Starting with" || ddlOND.value == "Budget Station Starting with") {
if (txtOND.value.length == 0) {
radalert("Enter a value for " + ddlOND.value);
aPanel.hide();
return;
}
}
aPanel.show();
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现了问题并解决了。下面给出了我的问题的解决方案。
我正在使用 telerik 控件,其中 clientID 不被视为 Firefox 中的对象。但IE将clientID视为对象。所以我之前的代码在 IE 中运行良好,而不是在 Firefox 中运行良好。现在我更改了 OnClientClick,通过将 clientID 作为字符串传递,如下所示,在单引号中:
在我的 javascript 中,我使用 document.getElementById 来获取对象。
这在 IE 和 Firefox 中都可以正常工作。
I found the problem and fixed. Given below the solution for my problem.
I am using telerik controls which clientID is not considered as a object in Firefox. But IE consider the clientID as object. So my previous code works fine in IE not in Firefox. Now I changed the OnClientClick, by passing the clientID as string like below in single quoute:
and in my javascript i used document.getElementById to get the object.
This works fine in both IE and Firefox.
FireFox 不处理 OnClientClick 和 OnClick 事件(尽管 Internet Explorer 处理得很好)。为了解决这个问题,可以使用一个方法
Page.ClientScript.GetPostBackEventReference()
来生成客户端 Javascript 以进行控件的回发。因此,以下代码将隐藏页面上除加载图形之外的所有内容,并调用 btnSubmit 回发方法。
它返回 false 的原因是,否则 Internet Explorer 将调用服务器端 OnClick 方法两次。
FireFox not handling both an OnClientClick and OnClick event (although Internet Explorer handled it fine). To fix this there is a method
Page.ClientScript.GetPostBackEventReference()
that can be used that generates client side Javascript for doing a postback to controls.So the following code will hide everything on the page except the loading graphic, and also call the btnSubmit postback method.
The reason it returns false is that otherwise Internet Explorer would call the server side OnClick method twice.