Question

i used ModalPopupExtender today only ,so i have not much idea how it works

i am stuck in problem here

i have Gridview showing all students data,now when i click on Linkbutton of particular student in Gridview than that student data should shown in Detailsview on popup

i am getting a perticular student data in Detailsview but its not showing in popup,should i have to add anything else here?

here is my code for modelpopupextender

aspx code-

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                    DataKeyNames="Stu_id" onselectedindexchanged="GridView1_SelectedIndexChanged">
                <Columns>
                    <asp:TemplateField HeaderText="ID">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Stu_id") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Stu_id") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                            <asp:BoundField DataField="Fullname" HeaderText="name" />
                            <asp:BoundField DataField="Username" HeaderText="Username" />
                            <asp:BoundField DataField="Email" HeaderText="Email" />
                            <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="btnviewdetails" runat="server" CausesValidation="false" 
                                CommandName="Select" Text="select" CommandArgument='<%# Eval("Stu_id") %>' 
                                onclick="LinkButton1_Click"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                 </Columns>
            </asp:GridView>
        </ContentTemplate>
  </asp:UpdatePanel>
<asp:Button ID="btnshowpopup" runat="server"/>
        <asp:ModalPopupExtender ID="mdlpopup" runat="server" TargetControlID="btnshowpopup" PopupControlID="pnlpopup" CancelControlID="btncancel" BackgroundCssClass="pop">
        </asp:ModalPopupExtender>

    <asp:Panel ID="pnlpopup" runat="server" Width="500px" style="display:none">
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <ContentTemplate>



                <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Stu_id" Height="50px" Width="125px">
                        <Fields>
                                <asp:BoundField DataField="Stu_id" HeaderText="ID" />
                                <asp:BoundField DataField="Fullname" HeaderText="Fullname" />
                                <asp:BoundField DataField="Username" HeaderText="Username" />
                                <asp:BoundField DataField="Email" HeaderText="Email" />
                        </Fields>
                </asp:DetailsView>
                  <div id="div1" style="display:none">                     
                       <asp:Button ID="btncancel" runat="server" Text="cancel" />
                 </div> 
            </ContentTemplate>
        </asp:UpdatePanel>             
    </asp:Panel>

cs code-

public partial class Admin_Default : System.Web.UI.Page


 {
    portalDal dal = new portalDal();
    adminBal bal = new adminBal();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = bal.student_bind();
            GridView1.DataBind();
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

    }
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

    this.DetailsView1.Visible = true;
    LinkButton l1 = (LinkButton)sender;
    dal.stu_id = Convert.ToInt16(l1.CommandArgument);
    DetailsView1.DataSource = bal.select_student(dal);
    DetailsView1.DataBind();
    this.UpdatePanel2.Update();
    this.mdlpopup.Show();

}

bal file-

public DataTable student_bind()
    {
        con.Open();
        cmd = new SqlCommand("select * from Stu_registration", con);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        con.Close();
        return dt;
    }

    public DataTable aselect_student(portalDal dal)
    {
        con.Open();
        cmd = new SqlCommand("select_student", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Stu_id", dal.stu_id);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        con.Close();
        return dt;
    }
Was it helpful?

Solution

I few observations:

  • You need to move the ModalPopupExtender control outside the Panel that you are using for the modal popup (pnlpopup, in your case).
  • Once you do that, you'llneed to change your CancelControlID to UpdatePanel2$btncancel (otherwise your ModalPopupExtender won't be able to find the "btncancel" control inside that UpdatePanel).
  • You also need to move the control you're using as the "TargetControlID" (btnshowpopup, in your case) outside of the modal popup control.

So your markup should look like this:

<asp:Panel ID="pnlpopup" runat="server" Width="500px" style="display:none">
    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
                DataKeyNames="Stu_id" Height="50px" Width="125px">
                <Fields>
                    <asp:BoundField DataField="Stu_id" HeaderText="ID" />
                    <asp:BoundField DataField="Fullname" HeaderText="Fullname" />
                    <asp:BoundField DataField="Username" HeaderText="Username" />
                    <asp:BoundField DataField="Email" HeaderText="Email" />
                </Fields>
            </asp:DetailsView>
            <div id="div1" style="display:none">                     
                <asp:Button ID="btncancel" runat="server" Text="cancel" />
            </div> 
        </ContentTemplate>
    </asp:UpdatePanel>             
</asp:Panel>

<asp:ModalPopupExtender ID="mdlpopup" runat="server" 
    TargetControlID="btnshowpopup" PopupControlID="pnlpopup" 
    CancelControlID="UpdatePanel2$btncancel" BackgroundCssClass="pop">
</asp:ModalPopupExtender>
<asp:Button ID="btnshowpopup" runat="server"/>

I would also change the order of the "Update" and "Show" commands in your codebaheind file. I don't think it will make any difference, but it is definitely more logical:

this.mdlpopup.Show();
this.UpdatePanel2.Update();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top