[英]Click a LinkButton with javascript in IE9
Ive got several LinkButtons inside a Repeater. 我在中继器中有几个LinkButtons。 When I manually click on them, they do a postback nicely.
当我手动单击它们时,它们会很好地执行回发。 All my tries to click on a LinkButton through javascript have failed, and Im now running out on ideas (and so are Google, cant seem to find any other options).
我所有尝试通过javascript单击LinkButton的尝试均以失败告终,我现在的想法已经用尽(Google也是如此,似乎找不到其他选择)。 I have tried to alternate between OnClick and OnCommand.
我试图在OnClick和OnCommand之间切换。
LinkButtons: 链接按钮:
<asp:Repeater ID="repItems" runat="server" OnItemDataBound="repItems_OnItemDataBound">
<ItemTemplate>
<asp:Panel ID="pnlItem" runat="server">
<asp:LinkButton ID="lnkItem" runat="server" OnClick="lnkItem_OnClick" OnCommand="lnkItem_OnCommand"></asp:LinkButton>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
Javascript: Javascript:
eval(LinkButton.attr('href'); //Causes Sys.ParameterCountException
LinkButton.click(); //Nothing happens (no click event on LinkButton)
LinkButton.trigger('click'); //Same as LinkButton.click()
If i set OnClientClick to do the postback, the click() functions starts working, but I get Sys.ParameterCountException error again: 如果我将OnClientClick设置为执行回发,则click()函数开始工作,但再次出现Sys.ParameterCountException错误:
lnkItem.OnClientClick = String.Format("__doPostBack('{0}', '');", lnkItem.UniqueID);
Some have suggested to change ScriptManager to release mode, but that didnt trigger the postback, it just removed the error message. 有些人建议将ScriptManager更改为发布模式,但这并没有触发回发,只是删除了错误消息。
eval(LinkButton.attr('href');
does work in Google Chrome. eval(LinkButton.attr('href');
确实可以在Google Chrome浏览器中使用。
I'm not sure what issue you're running into, but the following works for me. 我不确定您遇到什么问题,但是以下方法对我有用。
ASCX: ASCX:
<asp:Repeater ID="repItems" runat="server">
<ItemTemplate>
<asp:Panel ID="pnlItem" runat="server">
<asp:LinkButton ID="lnkItem" runat="server" Text='<%# Eval("Text") %>' OnClick="lnkItem_OnClick" OnCommand="lnkItem_OnCommand"></asp:LinkButton>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
<button onclick="document.getElementById('MainContent_repItems_lnkItem_0').click(); return false;">Huzzah!</button>
Codebehind: 代码背后:
protected void Page_Load(object sender, EventArgs e)
{
List<Item> test = new List<Item>();
test.Add(new Item { Text = "Item 1" } );
test.Add(new Item { Text = "Item 2" } );
repItems.DataSource = test;
repItems.DataBind();
}
protected void lnkItem_OnClick(object sender, EventArgs e)
{
this.Controls.Add(new Literal() { Text = ((LinkButton)sender).Text });
}
protected void lnkItem_OnCommand(object sender, EventArgs e)
{
this.Controls.Add(new Literal() { Text = ((LinkButton)sender).Text });
}
Both OnClick and OnCommand fire in this example. 在此示例中,均会触发OnClick和OnCommand。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.