【问题标题】:Not Showing Detailsview in ModalPopupExtender在 ModalPopupExtender 中不显示详细信息视图
【发布时间】:2014-02-12 15:57:57
【问题描述】:

我今天只使用了 ModalPopupExtender,所以我不太清楚它是如何工作的

我遇到了问题

我有显示所有学生数据的 Gridview,现在当我点击 Gridview 中特定学生的链接按钮时,该学生数据应该显示在弹出窗口的 Detailsview 中

我在 Detailsview 中获得了一个特定的学生数据,但它没有显示在弹出窗口中,我是否需要在此处添加其他任何内容?

这是我的 modelpopupextender 代码

aspx代码-

 <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代码-

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文件-

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;
    }

【问题讨论】:

    标签: asp.net c#-4.0 gridview asp.net-ajax


    【解决方案1】:

    我的一些观察:

    • 您需要将 ModalPopupExtender 控件移到用于模式弹出窗口(在您的情况下为 pnlpopup)的 Panel 之外。
    • 完成此操作后,您需要将 CancelControlID 更改为 UpdatePanel2$btncancel(否则您的 ModalPopupExtender 将无法在该 UpdatePanel 中找到“btncancel”控件)。
    • 您还需要将用作“TargetControlID”的控件(在您的情况下为 btnshowpopup)移到模态弹出控件之外。

    所以你的标记应该是这样的:

    <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"/>
    

    我还将更改您的 codebaheind 文件中“更新”和“显示”命令的顺序。我认为这不会有什么不同,但肯定更合乎逻辑:

    this.mdlpopup.Show();
    this.UpdatePanel2.Update();
    

    【讨论】:

    • 感谢您回答我的问题,但同样的问题,当我点击 gridview 链接按钮时,即使弹出窗口也没有出现,只有在点击 btnshowpopup 时才会出现
    • 哦不!对不起,@vikas。您是否收到任何错误消息?例如,JavaScript 错误?此外,这是一个远景,但您应该尝试将第一个 UpdatePanel 中的 GridView 定义为包含 DetailsView 的 UpdatePanel 的触发器。
    • 感谢@jadarnel,但我今天早上刚刚解决了这个问题,实际上我在 updatepanel2 中触发了触发器作为 gridview 的控件 ID,并在 LinkBut​​ton1_Click 中编写了所有 GridView1_SelectedIndexChanged 代码,并按照你所说的将模型弹出窗口移到面板外并添加了一些标签和它的工作,再次感谢您提供解决方案
    • 不客气,@vikas!我很高兴能帮上忙。随时发布解释您最终解决方案的答案,或者您可以将我的答案标记为“已接受的答案” - 或两者兼而有之!或者两者都没有 =) 没有压力。
    猜你喜欢
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    相关资源
    最近更新 更多