Maybe I'm searching with the wrong keywords, but I can't find a solution to this.
也许我正在搜索错误的关键字,但我无法找到解决方案。
I've got an ASPX page on which, within an <asp:Repeater>
I want to insert a button (per item) that will:
我有一个ASPX页面,在
- ask (JS "confirm") the user if they really want to proceed, then
- call a method on the page class passing in the ID (Guid) from that item.
请问(JS“确认”)用户是否真的要继续,然后
在页面类上调用一个方法,从该项中传入ID(Guid)。
I can do 1 or 2, but can't figure out how to call back into the page method from JavaScript.
我可以做1或2,但无法弄清楚如何从JavaScript调用页面方法。
I've tried turning on the EnablePageMethods in the ScriptManager, but that didn't seem to do what I expected -- calling PageMethods.Blah(...)
did not seem to have any effect.
我已经尝试在ScriptManager中打开EnablePageMethods,但这似乎没有达到我的预期 - 调用PageMethods.Blah(...)似乎没有任何效果。
Are there any ready examples? In fact, the confirmation string is the same for every entry.
有没有现成的例子?实际上,每个条目的确认字符串都是相同的。
5 个解决方案
#1
here's what you need to do:
这是你需要做的:
- make sure your button has runat="server"
- in that button, add onClientClick="javascript:return confirm('Are you sure?');"
-
call your SERVER SIDE code from onClick even of the button
从onClick甚至按钮调用您的SERVER SIDE代码
<asp:Button id="Button1" Text="Do Stuff" OnClick="YOUR SERVER SIDE METHOD" onClientClick="javascript: return confirm('Are you sure?');" runat="server"/>
确保你的按钮有runat =“server”
在该按钮中,添加onClientClick =“javascript:return confirm('你确定吗?');”
#2
You can perform the confirmation with the OnClientClick property of your button:
您可以使用按钮的OnClientClick属性执行确认:
<asp:Button runat="server" ID="DeleteButton" CommandName="Delete"
Text="Delete" OnClientClick="return confirm('Are you sure ?');" />
#3
<asp:Repeater ID="YourRepeater" runat="server">
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" OnClientClick="javascript:return confirm('Are you sure ?');" OnClick="btnDelete_OnClick" />
</ItemTemplate>
</asp:Repeater>
#4
Here's what you need to do:
这是你需要做的:
-
Enable script manager (which you did)
启用脚本管理器(您做过)
-
Create a web service, which will handle a callback into your code:
创建一个Web服务,它将处理代码的回调:
[WebService] [ScriptService] public class MyWebService : System.Web.Services.WebService { [WebMethod] public int SomeCallbackFunction() { .... } }
-
Put a link to a web service proxy into script manager (generated by [ScriptService]):
将指向Web服务代理的链接放入脚本管理器(由[ScriptService]生成):
<asp:ScriptManager runat="server" ID="scriptManagerId"> <Services> <asp:ServiceReference Path="WebService.asmx" /> </Services> </asp:ScriptManager>
-
In your JavaScript, you will be able to use the callback:
在JavaScript中,您将能够使用回调:
function Blablabla() { MyWebService.SomeCallbackFunction() }
This is only a general idea, of course, this code probably needs some polishing. You can look for more examples here.
这只是一个普遍的想法,当然,这段代码可能需要一些抛光。您可以在此处查找更多示例。
#5
Ultimately I decided to do it client-side. I omitted OnClick but set the onclientclick property to a function which confirms, then redirects to the page with a add parameter that is the ID of the table row in question.
最终我决定做客户端。我省略了OnClick,但将onclientclick属性设置为确认的函数,然后使用add参数重定向到页面,该参数是所讨论的表行的ID。
Why? Well, my data source is a read-only list created from a couple of tables, and because of that (IIUC) it is created before the button click was handled. Hence, the page refreshed without the data that the user had just added.
为什么?好吧,我的数据源是从几个表创建的只读列表,因此(IIUC)它是在处理按钮点击之前创建的。因此,页面刷新时没有用户刚刚添加的数据。
Instead, the **add=***ID* parameter causes the add, a success (or error) message and hilights the newly inserted row. The downside is if the user were to refresh the page, of course, but in this case it's within an IFRAME so I don't think that's likely.
相反,** add = *** ID *参数会导致添加,成功(或错误)消息以及新插入行的hilights。不利的一面是,如果用户要刷新页面,当然,但在这种情况下,它在IFRAME中,所以我认为不太可能。
So, thanks. You answered my question, I just didn't realize that my question wasn't aiming where I wanted to go.
那谢谢啦。你回答了我的问题,我只是没有意识到我的问题并不是针对我想去的地方。
Incidentally, I'm letting them add a row to the DB. The reason for confirmation is that it incurs up to an hour of processing time on the server!
顺便说一句,我让他们在数据库中添加一行。确认的原因是它会在服务器上产生长达一个小时的处理时间!
#1
here's what you need to do:
这是你需要做的:
- make sure your button has runat="server"
- in that button, add onClientClick="javascript:return confirm('Are you sure?');"
-
call your SERVER SIDE code from onClick even of the button
从onClick甚至按钮调用您的SERVER SIDE代码
<asp:Button id="Button1" Text="Do Stuff" OnClick="YOUR SERVER SIDE METHOD" onClientClick="javascript: return confirm('Are you sure?');" runat="server"/>
确保你的按钮有runat =“server”
在该按钮中,添加onClientClick =“javascript:return confirm('你确定吗?');”
#2
You can perform the confirmation with the OnClientClick property of your button:
您可以使用按钮的OnClientClick属性执行确认:
<asp:Button runat="server" ID="DeleteButton" CommandName="Delete"
Text="Delete" OnClientClick="return confirm('Are you sure ?');" />
#3
<asp:Repeater ID="YourRepeater" runat="server">
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" OnClientClick="javascript:return confirm('Are you sure ?');" OnClick="btnDelete_OnClick" />
</ItemTemplate>
</asp:Repeater>
#4
Here's what you need to do:
这是你需要做的:
-
Enable script manager (which you did)
启用脚本管理器(您做过)
-
Create a web service, which will handle a callback into your code:
创建一个Web服务,它将处理代码的回调:
[WebService] [ScriptService] public class MyWebService : System.Web.Services.WebService { [WebMethod] public int SomeCallbackFunction() { .... } }
-
Put a link to a web service proxy into script manager (generated by [ScriptService]):
将指向Web服务代理的链接放入脚本管理器(由[ScriptService]生成):
<asp:ScriptManager runat="server" ID="scriptManagerId"> <Services> <asp:ServiceReference Path="WebService.asmx" /> </Services> </asp:ScriptManager>
-
In your JavaScript, you will be able to use the callback:
在JavaScript中,您将能够使用回调:
function Blablabla() { MyWebService.SomeCallbackFunction() }
This is only a general idea, of course, this code probably needs some polishing. You can look for more examples here.
这只是一个普遍的想法,当然,这段代码可能需要一些抛光。您可以在此处查找更多示例。
#5
Ultimately I decided to do it client-side. I omitted OnClick but set the onclientclick property to a function which confirms, then redirects to the page with a add parameter that is the ID of the table row in question.
最终我决定做客户端。我省略了OnClick,但将onclientclick属性设置为确认的函数,然后使用add参数重定向到页面,该参数是所讨论的表行的ID。
Why? Well, my data source is a read-only list created from a couple of tables, and because of that (IIUC) it is created before the button click was handled. Hence, the page refreshed without the data that the user had just added.
为什么?好吧,我的数据源是从几个表创建的只读列表,因此(IIUC)它是在处理按钮点击之前创建的。因此,页面刷新时没有用户刚刚添加的数据。
Instead, the **add=***ID* parameter causes the add, a success (or error) message and hilights the newly inserted row. The downside is if the user were to refresh the page, of course, but in this case it's within an IFRAME so I don't think that's likely.
相反,** add = *** ID *参数会导致添加,成功(或错误)消息以及新插入行的hilights。不利的一面是,如果用户要刷新页面,当然,但在这种情况下,它在IFRAME中,所以我认为不太可能。
So, thanks. You answered my question, I just didn't realize that my question wasn't aiming where I wanted to go.
那谢谢啦。你回答了我的问题,我只是没有意识到我的问题并不是针对我想去的地方。
Incidentally, I'm letting them add a row to the DB. The reason for confirmation is that it incurs up to an hour of processing time on the server!
顺便说一句,我让他们在数据库中添加一行。确认的原因是它会在服务器上产生长达一个小时的处理时间!