2011-10-13 107 views
0

我無法讓ModalPopupExtender通過代碼運行。如果我設置ModalPopupExtender的屬性TargetControlID =「btn」,它就可以工作。當我將TargetControlID =「HiddenField1」設置爲在代碼中使用mpe.show()方法時,它不起作用(dgvResults_RowCommand事件觸發正常,我測試了它)。我經歷了很多查看代碼的網站,但無法弄清楚什麼是錯的。在此先感謝您的幫助無法使ModalPopupExtender通過代碼運行

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="Michlala._Default" %> 

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %> 

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
    <style type="text/css"> 
     .modalPopup 
     {    
      background-color: Black; 
      filter: alpha(opacity=80); 
      opacity: 0.8; 
      z-index: 10000;    
     } 
     .style1 
     { 
      width: 100%; 
     } 
     .style5 
     { 
      text-align: center; 
     } 
    </style> 
</asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    <h1> Student Feedback</h1> 
     <asp:ScriptManager ID="ScriptManager1" runat="server"> 
     </asp:ScriptManager>  
    <br /> 
    <asp:TextBox ID="txtSID" runat="server" Width="61px" Height="25px"></asp:TextBox> 
    &nbsp;&nbsp;&nbsp;&nbsp; 
    <asp:Button ID="btnSID" runat="server" onclick="Button1_Click" 
     Text="Submit Student ID" /> 

    <asp:GridView ID="dgvResults" runat="server" Visible="False" 
     onrowcommand="dgvResults_RowCommand"> 
     <Columns> 
      <asp:TemplateField> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>       
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:Button ID="btnFeedback" runat="server" onclick="Button1_Click1" 
         Text="Add FeedBack" CommandName="InsertFeedback" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" /> 
         <asp:HiddenField ID="HiddenField1" runat="server" /> 
        <asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" 
         TargetControlID="HiddenField1" 
         PopupControlID="pnlModalPanel" 
         CancelControlID="btnCancel" 
         BackgroundCssClass="modalPopup" > 
        </asp:ModalPopupExtender> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 
    <asp:Panel ID="pnlModalPanel" runat="server" style="display:none" Width="481px" > 
     <table class="style1"> 
      <tr> 
       <td> 
        Course 
       </td> 
       <td> 
        Semester 
       </td> 
       <td class="style5"> 
        Teacher Assitant 
       </td> 
       <td class="style5"> 
        Lecturer 
       </td> 
      </tr> 
      <tr> 
       <td> 
        &nbsp 
        <asp:TextBox ID="txtCourse" runat="server"></asp:TextBox> 
       </td> 
       <td> 
        &nbsp 
       </td> 
       <td> 
        &nbsp 
       </td> 
       <td> 
        &nbsp 
       </td> 
      </tr> 
      <tr> 
       <td colspan="2" style="text-align: center"> 
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" /> 
        &nbsp; 
       </td> 
       <td colspan="2"> 
        &nbsp 
        <asp:Button ID="btnCancel" runat="server" Text="Cancel" /> 
       </td> 
      </tr> 
     </table>  
    </asp:Panel> 
    <asp:DropDownList ID="ddlSemester" runat="server"> 
    </asp:DropDownList> 
    <asp:Label ID="lblRes" runat="server" Text="Label"></asp:Label>   
    <br />   
</asp:Content> 

在服務器端代碼

protected void dgvResults_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    ModalPopupExtender mpe = new ModalPopupExtender(); 
    mpe.Show(); 
} 

回答

1

您需要鰭ModalPopupExtender選擇行,然後用名字稱呼它:

if (e.CommandName.Equals("InsertFeedback")) 
{ 
    int index = Convert.ToInt32(e.CommandArgument); 

    // Retrieve the row that contains the button clicked 
    // by the user from the Rows collection. 
    GridViewRow row = dgvResults.Rows[index]; 

    ModalPopupExtender mpe = (ModalPopupExtender)row.FindControl("ModalPopupExtender1"); 
    mpe.Show(); 
} 
+0

測試有用 –