简体   繁体   English

使用Javascript将其他参数传递到LinkBut​​ton的OnClick事件处理程序中

[英]Passing additional arguments into the OnClick event handler of a LinkButton using Javascript

I have a ASP.NET Website, where, in a GridView item template, automatically populated by a LinqDataSource, there is a LinkButton defined as follows: 我有一个ASP.NET网站,在GridView项目模板中,由LinqDataSource自动填充,有一个LinkBut​​ton定义如下:

<asp:LinkButton ID="RemoveLinkButton" runat="server" CommandName="Remove"
                CommandArgument='<%# DataBinder.GetPropertyValue(GetDataItem(), "Id")%>'                                 
                OnCommand="removeVeto_OnClick"
                OnClientClick='return confirm("Are you sure?");'
                Text="Remove Entry" />

This works fine. 这很好用。 Whenever the Button is Clicked, a confirmation dialog is displayed. 只要单击按钮,就会显示确认对话框。

What I am trying to do now, is to allow the user to enter a reason for the removal, and pass this on the the OnClick event handler. 我现在要做的是允许用户输入删除的原因,并在OnClick事件处理程序上传递它。 How would I do this? 我该怎么做? I tried OnClientClick='return prompt("Enter your reason!");' 我试过OnClientClick='return prompt("Enter your reason!");' , but, of course, that did not work =) ,但是,当然,这不起作用=)

Personally, I would stash the reason into a hidden field. 就个人而言,我会将原因隐藏在一个隐藏的领域。

It would work something along these lines: your OnClientClick method would take the return value of a JS method, which does the prompt, and then places the result of the prompt into the hidden field. 它可以在这些方面起作用:OnClientClick方法将获取JS方法的返回值,该方法执行提示,然后将提示的结果放入隐藏字段。

You can also look into calling __doPostBack from your client-side code instead of using the OnClick postback. 您还可以考虑从客户端代码调用__doPostBack,而不是使用OnClick回发。 Then you can capture the reason and pass it server-side. 然后你可以捕获原因并将其传递给服务器端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM