将 linkbutton 设置为 asp.net 中 asp:panel 的默认按钮
如何将链接按钮设置为 asp.net 中 asp:panel 的默认按钮?我知道可以将按钮设置为默认按钮,但我的应用程序对所有表单都使用链接按钮。任何建议如何完成。
编辑:
现在我尝试了这个, 它也适用于 Firefox,但我的 javascript 验证(即)客户端单击我的链接按钮不起作用,为什么?
var __defaultFired = false;
function WebForm_FireDefaultButton(event, target) {
var element = event.target || event.srcElement;
if (!__defaultFired && event.keyCode == 13 && !(element && (element.tagName.toLowerCase() == "textarea"))) {
var defaultButton;
if (__nonMSDOMBrowser)
defaultButton = document.getElementById(target);
else
defaultButton = document.all[target];
if (defaultButton) {
if (typeof (defaultButton.click) != "undefined")
defaultButton.click();
else
eval(unescape(defaultButton.href.replace("javascript:", "")));
event.cancelBubble = true;
if (event.stopPropagation) event.stopPropagation();
return false;
}
}
return true;
}
第二次编辑:
我能够使我的自定义链接按钮控件正常工作,但无法将 OnClientClick 挂钩到它。来源 using-panel- defaultbutton-property-with-linkbutton-control-in-asp-net。
我这样做了,
<%@ Register Namespace="App_Code" TagPrefix="ac" %>
<asp:Label runat="server" ID="lblHello" />
<asp:Panel runat="server" DefaultButton="lbHello">
First name: <asp:TextBox runat="server" ID="txtFirstName" />
<ac:LinkButtonDefault ID="lbHello" runat="server" Text="Click me"
OnClientClick="javascript:alert('hai');" OnClick="lbHello_Click" />
</asp:Panel>
我的客户端功能不起作用,为什么?任何建议。
Possible Duplicate:
Link Button on the page and set it as default button, work fine in IE but not in Mozila
How to set linkbutton as default button for asp:panel in asp.net? I know a button can be set as default but my application uses linkbuttons for all forms. Any suggestion how it can be done.
EDIT:
Now i tried this,
It works in firefox as well but my javascript validation (ie) onclient click of my linkbutton doesn't work why?
var __defaultFired = false;
function WebForm_FireDefaultButton(event, target) {
var element = event.target || event.srcElement;
if (!__defaultFired && event.keyCode == 13 && !(element && (element.tagName.toLowerCase() == "textarea"))) {
var defaultButton;
if (__nonMSDOMBrowser)
defaultButton = document.getElementById(target);
else
defaultButton = document.all[target];
if (defaultButton) {
if (typeof (defaultButton.click) != "undefined")
defaultButton.click();
else
eval(unescape(defaultButton.href.replace("javascript:", "")));
event.cancelBubble = true;
if (event.stopPropagation) event.stopPropagation();
return false;
}
}
return true;
}
Second EDIT:
I was able make my custom linkbutton control work but couldn't able to hook OnClientClick to it. Source using-panel-defaultbutton-property-with-linkbutton-control-in-asp-net.
and i did this,
<%@ Register Namespace="App_Code" TagPrefix="ac" %>
<asp:Label runat="server" ID="lblHello" />
<asp:Panel runat="server" DefaultButton="lbHello">
First name: <asp:TextBox runat="server" ID="txtFirstName" />
<ac:LinkButtonDefault ID="lbHello" runat="server" Text="Click me"
OnClientClick="javascript:alert('hai');" OnClick="lbHello_Click" />
</asp:Panel>
My Clientside function doesn't work why? Any suggestion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以采用添加属性来处理文本框的
onKeyPress
事件的简单方法,而不是使用自定义控件。这可以正确处理从文本框中按 Enter 并触发LinkButton
的事件。这种方法的缺点是,任何LinkButton
OnClientClick
事件都不会在 Firefox 中触发,这与您链接到的博客文章中描述的问题有关。仅当用户实际用鼠标单击链接时才会触发它。但是,在 IE 中,它将从文本框和直接单击触发。解决方案 #1 - 添加属性的代码如下:
尝试一下,看看它是否符合您的需求。请记住我之前描述的限制。
现在,如果您希望消除上述限制,该博客文章中的一条评论显示了一种似乎可以正常工作的方法。我对其进行了修改,删除了
StringBuilder
并将其转换为 C#。解决方案 #2 - 添加该功能并注册的代码如下:
页面标记 -
对于上述两种解决方案,页面标记是相同的:
在这两种情况下都不需要自定义控件来实现此类功能。保持简单。
Rather than use a custom control you could take the simple approach of adding an attribute to handle the textbox's
onKeyPress
event. This correctly handles pressing Enter from the textbox and triggering theLinkButton
's event. The downside to this approach is that anyLinkButton
OnClientClick
event will not be triggered in Firefox, which is related to the issue described in that blog post you linked to. It will only be triggered when the user actually clicks on the link with their mouse. However, in IE, it will trigger from both the textbox and from being clicked on directly.Solution #1 - The code to add the attribute is as follows:
Try that and see if it fits your needs. Just bear in mind the limitation I described earlier.
Now, if you want the above limitation to go away, one of the comments from that blog post showed an approach that appears to work correctly. I've modified it to get rid of the
StringBuilder
and converted it to C#.Solution #2 - The code to add the function and register it is as follows:
Page Markup -
The page mark-up is the same for both of the aforementioned solutions:
In both cases a custom control is not needed to achieve this type of functionality. Keep it simple.
看看这个
其余的答案可以在这里找到。它展示了如何解决 FireFox 出现的问题
http://kpumuk .info/asp-net/using-panel-defaultbutton-property-with-linkbutton-control-in-asp-net/
Take a look at this
The rest of the answer can be found here. It shows how to fix the problem that arises with FireFox
http://kpumuk.info/asp-net/using-panel-defaultbutton-property-with-linkbutton-control-in-asp-net/
另一种方法可能是像这样伪造 onclick 事件。
缺点是您需要在所有需要创建默认按钮的地方使用它。
Another approach could be to fake the onclick event like this.
The downside is that you need to use this for all places you need to make default buttons.
请参阅此
页面上的链接按钮并将其设置为默认按钮,在 IE 中工作正常,但在 Mozila 中不行 在
我看来,虚拟按钮答案是最干净的解决方案。
See this
Link Button on the page and set it as default button, work fine in IE but not in Mozila
The dummy button answer is the cleanest solution IMO.