Search code examples
c#.netajaxgridviewmodalpopupextender

Open an ajax modalpopup with details from gridview cells


I've got a gridview with a appointment information in. What I want to do is have a linkbutton in each cell (which will be created at runtime) and open a modal popup showing the details of the appointment. Any help would be greatly appreciated.

So far I've got, but it won't fire the 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>

code behind

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();


        }
    }

Solution

  • The bottom line is that you need to make sure that the dynamic LinkButton controls are recreated on the postback in order for the event to be properly wired. Since you create the LinkButtons within the Grd_RowDataBound handler, which only gets called when you call BindData() (and you're not calling it on postback), your events won't wire. So if you're code looks something like this:

    if (!IsPostBack)
    {
         Grd.BindData();
    }
    

    Try removing the if(!IsPostBack) check, and always call BindData() on Page_Load.