我有一个网格视图,其中有一行包含动态添加的LinkButtons。当单击这些LinkButtons时,我需要显示一个确认对话框。我试着按照这篇文章中的建议工作:JQuery DIalog and ASP.NET Repeater,但它不工作,postBackReference没有包含正确的ID (它忽略了占位符)这是我的代码:
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);
}有谁有主意吗?
发布于 2011-12-07 14:04:56
所以这里有一个对我有效的解决方案:基本上我是根据这篇文章中的解决方案工作的:JQuery DIalog and ASP.NET Repeater,唯一的区别是我必须使用RowCreated事件来添加我的动态LinkButtons和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);
}和:
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;";
}客户端功能- showConf和标记保持不变。
发布于 2011-12-06 23:37:48
我不知道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);
}UPDATE -在JQuery UI方法中尝试如下所示
function showConfirmation(confirmationMessage) { $( "#dialog-confirm“).dialog( "destroy");$(”#dialog-confirm“).dialog({ resizable: false,height:140,title: confirmationMessage,modal: true,buttons:{ "Yes":function() { $( this ).dialog( "close”) );__doPostBack(linkItemID,‘’);//将导致回发},取消: function() { $( this ).dialog( "close“);});return false;//永远不会回发}
GridView1_RowCreated(对象发送者,GridViewRowEventArgs e) { LinkButton lb =新单元格();lb.Text = "something";lb.ID = "someId";lb.OnClientClick = "return showConfirmation(‘是否确定要做这个做那个?’,'"+lb.ID+"');";TableCell单元格=新TableCell();cell.Controls.Add(lb);e.Row.Cells.Add(单元格);}
注意:上面的代码还没有经过测试,但它应该非常非常接近你所需要的。
https://stackoverflow.com/questions/8401795
复制相似问题