我有一个带有约会信息的gridview。我想要做的是在每个单元格中有一个链接按钮(将在运行时创建)并打开一个模式弹出窗口,显示约会的详细信息。任何帮助将不胜感激。
到目前为止,我已经有了,但它不会触发linkbutton
<asp:GridView ID="Grd" runat="server" AutoGenerateColumns="true" onrowdatabound="Grd_RowDataBound"></asp:GridView><asp:Button ID="btnShowPopup" style="display:none" runat="server" />
<ajaxToolkit:ModalPopupExtender
ID="ModalPopupExtender1" runat="server" TargetControlID="btnShowPopup" PopupControlID="pnlpopup" CancelControlID="ImgCancel" ></ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="pnlpopup" runat="server" Width="400px" ><!--Show Details--!>
<asp:ImageButton ID="imgCancel" AlternateText="cancel" Height="25" Width="25" runat="server" ImageAlign="Right" />
</asp:Panel>
背后的代码
protected void Grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Split out the visit details & format
for (int i = 0; i <= e.Row.Cells.Count - 1; i++)
{
LinkButton lnk = new LinkButton();
lnk.Text = "Details for:" + "<br />" + e.Row.Cells[i].Text;
lnk.CommandName = "ShowDetails";
lnk.Command += LinkButton_Command;
e.Row.Cells[i].Controls.Add(lnk);
}
}
}
protected void LinkButton_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "ShowDetails")
{
LinkButton btndetails = sender as LinkButton;
GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
this.ModalPopupExtender1.Show();
}
}
答案 0 :(得分:0)
最重要的是,您需要确保在回发上重新创建动态LinkButton
控件,以便正确连接事件。由于您在LinkButtons
处理程序中创建了Grd_RowDataBound
,只有在您调用BindData()
时才会调用(并且您没有在回发时调用它),因此您的事件将无法连接。所以如果你的代码看起来像这样:
if (!IsPostBack)
{
Grd.BindData();
}
尝试删除if(!IsPostBack)
检查,并始终在Page_Load上调用BindData()。
答案 1 :(得分:0)
在你的模态弹出窗口中,唯一的内容应该是空的iframe。然后编写一个javascript函数如下:
function showPopup(target, link, width, height)
{
var frame = document.getElementById('popupIframe');
frame.src = link;
frame.width = width;
frame.height = height;
var location = Sys.UI.DomElement.getLocation(target);
var popup = $find('Popup');
popup.set_X(location.x);
popup.set_Y(location.y);
popup.show();
}
然后在你的链接按钮中添加:
OnClientClick="showPopup(this, your_details_page_with_querystring_params, width, height)