我有一个自定义Web控件* .ascx文件。我试图模拟用户使用java脚本单击gridview中的更新按钮。我在.ascx页面的顶部有这个脚本:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript">
$(document).keypress(function (e) {
if (e.which == 13) {
document.getElementById("LinkButton1").click();
}
});
</script>
“LinkButton1”位于此模板字段中:
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" onKeyPress="" UseSubmitBehavior="True" CausesValidation="True"
CommandName="Update" TabIndex="13" Text="Update" ></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
我错过了什么或做错了什么?
答案 0 :(得分:0)
您可以尝试使用
document.getElementById('<%# ((GridViewRow)Container).FindControl("LinkButton2").ClientID %>').click();
答案 1 :(得分:0)
这不起作用,因为点击仅适用于附加事件,而不适用于链接按钮呈现为的锚点。
你可以使用它..
$(document).keypress(function (e) {
if (e.which == 13) {
simulate(document.getElementById("LinkButton1"), "click");
}
});
function simulate(element,eventName){ var options = extend(defaultOptions,arguments [2] || {}); var oEvent,eventType = null;
for (var name in eventMatchers) {
if (eventMatchers[name].test(eventName)) { eventType = name; break; }
}
if (!eventType)
throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
if (document.createEvent) {
oEvent = document.createEvent(eventType);
if (eventType == 'HTMLEvents') {
oEvent.initEvent(eventName, options.bubbles, options.cancelable);
}
else {
oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
}
element.dispatchEvent(oEvent);
}
else {
options.clientX = options.pointerX;
options.clientY = options.pointerY;
var evt = document.createEventObject();
oEvent = extend(evt, options);
element.fireEvent('on' + eventName, oEvent);
}
return element;
}
function extend(destination,source){ for(源代码中的var属性) destination [property] = source [property]; 返回目的地; }
var eventMatchers = { 'HTMLEvents':/ ^(?:load | unload | abort | error | select | change | submit | reset | focus | blur | resize | scroll)$ /, 'MouseEvents':/ ^(?:click | dblclick | mouse(?:down | up | over | move | out))$ / };
var defaultOptions = { pointerX:0, pointerY:0, 按钮:0, ctrlKey:false, altKey:false, shiftKey:false, metaKey:假的, 气泡:真的, 可取消:是的 };