C# jquery对话框和asp.net动态链接按钮
我有一个gridview,其中有一行包含动态添加的链接按钮。 单击这些链接按钮时,我需要显示一个确认对话框。 我试着按照这篇文章的建议工作: 但它不起作用,postBackReference不包含正确的ID(它忽略占位符) 这是我的代码:C# jquery对话框和asp.net动态链接按钮,c#,javascript,jquery,asp.net,C#,Javascript,Jquery,Asp.net,我有一个gridview,其中有一行包含动态添加的链接按钮。 单击这些链接按钮时,我需要显示一个确认对话框。 我试着按照这篇文章的建议工作: 但它不起作用,postBackReference不包含正确的ID(它忽略占位符) 这是我的代码: GridView1_RowCreated(Object sender, GridViewRowEventArgs e) { //some code here LinkButton lb = new LinkButton(); lb.Text
GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
//some code here
LinkButton lb = new LinkButton();
lb.Text = "something";
lb.ID = "someId";
string postBackReference = ClientScript.GetPostBackEventReference(lb, string.Empty);
lb.OnClientClick = "javascript: showConf(function(){"+ postBackReference +"});return false;";
TableCell cell = new TableCell();
cell.Controls.Add(lb);
e.Row.Cells.Add(cell);
}
有人有想法吗?我不知道JQuery在您的场景中扮演什么角色。我想你想展示一些奇特的确认框,如果你提供详细信息,我会提供一个更具体的答案,但现在,纯javascript就是这样做的:
GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
LinkButton lb = new LinkButton();
lb.Text = "something";
lb.ID = "someId";
lb.OnClientClick = "javascript: return confirm('Are you sure that you want to do this and that?'); ";
TableCell cell = new TableCell();
cell.Controls.Add(lb);
e.Row.Cells.Add(cell);
}
更新-在JQuery UI方法中尝试类似的方法
<div id="dialog-confirm" title="" ></div>
GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
LinkButton lb = new LinkButton();
lb.Text = "something";
lb.ID = "someId";
lb.OnClientClick = "return showConfirmation('Are you sure you want to do this and that?','"+lb.ID+"'); ";
TableCell cell = new TableCell();
cell.Controls.Add(lb);
e.Row.Cells.Add(cell);
}
注意:上面的代码还没有经过测试,但应该非常接近您的需要。所以这里有一个适合我的解决方案: 基本上,我是根据这篇文章中的解决方案工作的: 唯一的区别是,我必须使用RowCreated事件来添加动态链接按钮,使用RowDataBound事件来注册客户端函数(否则原始的_doPostBack将无法正确获取ID参数(好像它忽略了它位于占位符中的事实))。 现在我的代码如下所示:
GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
//some code here
LinkButton lb = new LinkButton();
lb.Text = "something";
lb.ID = "someId";
TableCell cell = new TableCell();
cell.Controls.Add(lb);
e.Row.Cells.Add(cell);
}
以及:
客户端函数-showConf和markup保持原样。抱歉,我忘了说,我想使用jquery ui对话框,只是用一个显示jquery ui对话框的函数替换返回确认。感谢lcarus和thiagoleite,但我认为它不会工作,因为它不会停止ASP.Net生成的原始u doPostBack的启动。因此,在用户能够选择按钮之前,回发将occur@A.B.Cade:是,它将停止它,因为当用户单击对话框上的取消按钮时,javascript函数返回false。注意OnClientClick如何
返回showConfirmation…
这与普通确认的工作方式完全相同。如果您在任何情况下都不想回发,只需在Yes
按钮上将return true
更改为return false
,或者将两个返回一起删除,让函数到达最后一行,该行始终返回false。再次感谢lcarus,这是我的第一个想法,但对我来说从未起作用。显然,jquery对话框不会挂起该过程。你的代码对你有用吗?找到了!应该改用RowDataBound事件(用于向asp.net注册我的JS函数),我明白你的意思了。无论您是在OnRowDataBound
还是OnRowCreated
上执行此操作,我都会使用您自己的id向showConfirmation
函数添加一个额外的参数,这样您就不必解析服务器端id来知道您在操作哪个记录。我把一些细节放在我答案的评论部分。欢迎来到Stack Overflow!一旦你有能力,确保并标记你的答案为已接受,这样其他人就会知道有解决方案。
GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
//some code here
LinkButton lb = new LinkButton();
lb.Text = "something";
lb.ID = "someId";
TableCell cell = new TableCell();
cell.Controls.Add(lb);
e.Row.Cells.Add(cell);
}
GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
//some code here
LinkButton lb = e.Row.FindControl("someId") as LinkButton;
string postBackReference = ClientScript.GetPostBackEventReference(lb, string.Empty);
lb.OnClientClick = "javascript: showConf(function(){"+ postBackReference +"});return false;";
}