C# ASP.NET 2.0 - 需要以编程方式单击链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/706659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET 2.0 - Need to programmatically click a link
提问by n2009
Is there a way to click a link programatically, so that it has the same effects as if the user clicked on it?
有没有办法以编程方式单击链接,使其具有与用户单击相同的效果?
Example:
例子:
I have an ASP.NET LinkButton:
我有一个 ASP.NET LinkButton:
<asp:LinkButton id="lnkExport" runat="server" CssClass="navclass">Export</asp:LinkButton>
I have a link on a sidebar directing to the .aspx page that has this linkbutton on it. For various reasons I can't have the code for the LinkButton executed until the page has refreshed -- so I am looking for a way to force-click this LinkButton in my code once the page is completely loaded. Is there a simple/doable way to accomplish this? If it involves triggering an event, please provide a code sample if you can. Thanks.
我在侧边栏上有一个链接,指向带有此链接按钮的 .aspx 页面。由于各种原因,在页面刷新之前我无法执行 LinkButton 的代码——所以我正在寻找一种方法,一旦页面完全加载,就可以在我的代码中强制单击此 LinkButton。有没有一种简单/可行的方法来实现这一点?如果涉及触发事件,请提供代码示例。谢谢。
采纳答案by Scott Nichols
Rashack's post show's how to do it. You can just do it in javascript.
Rashack 的帖子展示了如何做到这一点。你可以用javascript来做。
function ClickLink() {
document.getElementById('').click();
}
If you want this to fire after some other event, you can add code in c# to add a call to that function on the client side when the page loads.
如果您希望在某个其他事件之后触发它,您可以在 c# 中添加代码,以便在页面加载时在客户端添加对该函数的调用。
Page.ClientScript.RegisterStartupScript(
this.getType(),
"clickLink",
"ClickLink();",
true);
回答by Matthew Timbs
I'm not sure why you'd need your page to load if you're just wanting to programmatically click that link. I'd recommend using Response.Redirect() on the server side to redirect them to that page. Not sure if there are other extenuating reasons this simple approach won't work...
如果您只想以编程方式单击该链接,我不确定为什么需要加载您的页面。我建议在服务器端使用 Response.Redirect() 将它们重定向到该页面。不确定是否还有其他情有可原的原因,这种简单的方法不起作用......
--Matt
--马特
回答by DLS
Triggering a click event programatically on a link will trigger the “onclick” event, but not the default action(href).
在链接上以编程方式触发点击事件将触发“onclick”事件,但不会触发默认操作(href)。
And since linkbuttons come out as hrefs, so you could try doing this with Javascript.
由于链接按钮是作为 href 出现的,因此您可以尝试使用 Javascript 执行此操作。
var lnkExport = document.getElementById('<%= lnkExport.ClientID %>');
if(lnkExport){
window.location = lnkExport.href;
}
回答by Rashack
<button onclick="document.getElementById('<%=this.lnkExport.ClienID%>').click()">
click me</button>
回答by Doozer Blake
If i understand what you're saying:
如果我明白你在说什么:
<asp:LinkButton id="lnkExport" runat="server" CssClass="navclass" onclick="lnkExport_Click">Export</asp:LinkButton>
then in your codebehind or whenever call the following when you need to...
然后在您的代码隐藏中或在您需要时调用以下...
lnkExport_Click( null, null );
and make sure you've got lnkExport_Click wired up.
并确保您已连接 lnkExport_Click。
protected void lnkExport_Click( object sender, EventArgs e )
{
//DO Whatever here
}
回答by 123Developer
I certainly think that, there is a design and implementation flaw which forces you to conclude as you described.
我当然认为,存在一个设计和实现缺陷,迫使您按照您的描述得出结论。
Well, invoking the click event means nothing but executing the event registration method.
好吧,调用click事件无非就是执行事件注册方法而已。
So, the worst suggestion I can think of is, just call the function at what point you want to happen the click event like,
所以,我能想到的最糟糕的建议是,只需在您想要发生点击事件的时间点调用该函数,例如,
lnkExport_Click(lnkExport, new EventArgs());
lnkExport_Click(lnkExport, new EventArgs());