2011-09-14 72 views
0

当用户点击某个LinkBut​​ton时,我需要用OK/Cancel打开一个确认对话框。如果用户点击确定,那么我需要从ASP.NET运行一个函数(更新数据库)。在Javascript中执行ASP.NET 1.1代码

像下面这样...

的Javascript

function openConfirm() { 
    return window.confirm("Are you sure you want to do this?"); 
} 

ASP

<asp:LinkButton runat="server" CommandName="viewPart" onClick="if(openConfirm() === true) <% SomeASPFunctionCall() %>;"> 
    Delete 
</asp:LinkButton> 

美中不足的是,我的应用程序正在运行ASP.NET 1.1,所以任何引用将OnClientClick添加到控件是无关紧要的(因为已为ASP 2.0添加了)。我已经通过__doPostBack和__eventObject和__eventArguments尝试回发,但这些都不起作用,或者我无法弄清楚。

我应该如何管理客户端/服务器端交互的这种组合?

谢谢

回答

1

你的linkBut​​ton没有ID,你确定吗?

把一个ID给它,让我们想象这就是所谓的myLinkButton(不要使用这个ID,请:d)在Page_PreRender把这段代码

myLinkButton.Attributes.Add("onClick", "return window.confirm('Are you sure you want to do this?'"); 

这是伪代码,但应该建立根据框架和网格versio您的网格内,在的ItemDataBound或RowDataBound事件:与工作也在.NET 1.1,我认为,我没有使用它,因为年龄...

编辑ñ把这样的事情:

private void myDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e) 
{ 
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     var myControl = (LinkButton)e.Item.FindControl("myLinkButton"); 

     if(myControl != null) 
     { 
      myControl.Attributes.Add("onClick", "return window.confirm('Are you sure you want to do this?'"); 
     } 
    } 
} 
+0

嗯,LinkBut​​ton没有ID的原因是因为它是在一个DataGrid中的中继器控制(更具体地说是一个TemplateColumn)。有没有办法解决所有动态创建的链接按钮,如CSS选择器? – danyim

+0

是的,你仍然可以在TemplateItem里面还有一个id,编辑我的答案... –

+0

谢谢,我明白了! – danyim