Ajax ModalPopupExtender

I have added a ModalPopupExtender to my page.  It works except how do I run my code to upload the document when the user clicks the link?

<asp:Panel ID="pnlPhotoUploadC1" style="display:none;" runat="server">
    <asp:Label ID="C1UploadDetails" runat="server" CssClass="validError"></asp:Label>
                    <asp:FileUpload ID="C1Upload" runat="server" TabIndex="28" Height="29px" 
              Width="218px" /><br />
              <asp:LinkButton ID="btnComp1" runat="server">Upload Selected Document</asp:LinkButton>
              
        &nbsp;&nbsp;&nbsp;
        <asp:LinkButton ID="btnComp1Cancel" runat="server">Cancel</asp:LinkButton>
              
    </asp:Panel>
    <asp:ModalPopupExtender ID="pnlPhotoUploadC1_ModalPopupExtender" runat="server" 
        DynamicServicePath="" Enabled="True" TargetControlID="btnUploadC1" 
        PopupControlID="pnlPhotoUploadC1" OkControlID="btnComp1" 
        CancelControlID="btnComp1Cancel" BackgroundCssClass="modalPopup">
    </asp:ModalPopupExtender>

Open in new window


Protected Sub btnComp1_Click(sender As Object, e As EventArgs) Handles btnComp1.Click
        If C1Upload.HasFile = False Then
            ' No file uploaded!
            C1UploadDetails.Text = "Please first select a file to upload..."
        Else
            If C1Upload.FileBytes.Length > 1099999 Then
                C1UploadDetails.Text = "File size is limited to 1MB"
            Else
                ' Display the uploaded file's details
                C1UploadDetails.Text = String.Format( _
                        "Uploaded file: {0}<br />" & _
                        "File size (in bytes): {1:N0}<br />" & _
                        "Content-type: {2}", _
                        C1Upload.FileName, C1Upload.FileBytes.Length, C1Upload.PostedFile.ContentType)
                Dim DocId As String = System.Guid.NewGuid().ToString()
                C1Upload.SaveAs(Server.MapPath("~/Uploads/") & DocId.ToString() & "_" & Session("OrdersProductKey").ToString & System.IO.Path.GetExtension(C1Upload.PostedFile.FileName))
                'Insert file name into table
                Dim cnn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("myprovalConnectionString").ConnectionString)
                Try

                    Dim cmd As New System.Data.SqlClient.SqlCommand
                    cmd.Connection = cnn
                    cmd.CommandType = Data.CommandType.StoredProcedure
                    cmd.CommandText = "dbo.PhotoUpdate"
                    Dim Location As String = DocId.ToString() & "_" & Session("OrdersProductKey").ToString & System.IO.Path.GetExtension(C1Upload.PostedFile.FileName)
                    cmd.Parameters.Add("@OrdersProductKey", Data.SqlDbType.Int).Value = Session("OrdersProductKey").ToString
                    cmd.Parameters.Add("@filePath", Data.SqlDbType.VarChar).Value = Location
                    cmd.Parameters.Add("@PhotoType", Data.SqlDbType.Int).Value = "7"
                    Dim DocName As String = System.IO.Path.GetFileName(C1Upload.PostedFile.FileName.Replace("&", "")).ToString
                    cmd.Parameters.Add("@DocName", Data.SqlDbType.VarChar).Value = DocName
                    Dim iUserId As New Guid(Membership.GetUser(My.User.Name).ProviderUserKey.ToString())
                    cmd.Parameters.Add("@UserId", Data.SqlDbType.VarChar).Value = iUserId.ToString
                    cnn.Open()
                    cmd.ExecuteNonQuery()
                Catch ex As Exception
                    C1UploadDetails.Text = "Upload failed"

                Finally
                    cnn.Close()
                    UpdateSubjectSection()
                    UpdateC1Section()
                    UpdateC2Section()
                    UpdateC3Section()
                    Dim sRedirect As String = "CompEvalListingComps.aspx?OPK=" + Session("OrdersProductKey")
                    Response.Redirect(sRedirect)
                End Try
            End If
        End If
    End Sub

Open in new window

rcowen00Asked:
Who is Participating?
 
Alan WarrenApplications DeveloperCommented:
Hi rcowen00,
This works, no problem (asp-fileupload-inside-ajax-modal-popup.aspx)
<%@ Page Language="VB" Title="asp-fileupload-inside-ajax-modal-popup" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  Protected Sub UploadModalPopup_Click(ByVal sender As Object, ByVal e As EventArgs)
    Me.UploadModalPopup.Show()
  End Sub

  Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim strSaveAsPath As String = ""
    strSaveAsPath = Server.MapPath("~/upload/") & FileUpload1.PostedFile.FileName
    FileUpload1.PostedFile.SaveAs(strSaveAsPath)
    Label2.Text = FileUpload1.PostedFile.FileName
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:ScriptManager ID="ScriptManager1" runat="server" />
      <asp:LinkButton runat="server" ID="showUploadModalPopupLinkButton" Text="Upload" OnClick="UploadModalPopup_Click" />
 
      <!-- Upload Modal Popup -->
      <asp:Button runat="server" ID="hiddenTargetControlForUploadModalPopup" style="display:none"/>
      <ajaxToolkit:ModalPopupExtender runat="server" ID="UploadModalPopup" 
        BehaviorID="UploadModalPopupBehavior"
        TargetControlID="hiddenTargetControlForUploadModalPopup"
        PopupControlID="UploadPopup" 
        BackgroundCssClass="modalBackground"
        DropShadow="True"
        PopupDragHandleControlID="UploadPopupDragHandle"
        RepositionMode="None"
        CancelControlID="UploadPopupCloseImageButton" />
      <asp:Panel runat="server" CssClass="modalPopup" ID="UploadPopup" style="display:none;">
        <asp:Panel runat="server" ID="UploadPopupDragHandle" CssClass="doctitle">
          <asp:Image ID="UploadPopupImage" runat="server" ImageUrl="~/favicon.ico" ImageAlign="Left" />
          &nbsp;Upload Document
          <asp:ImageButton ID="UploadPopupCloseImageButton" runat="server" ImageUrl="~/img/icon_close.png" ImageAlign="Right" ToolTip="Close this thing" />
          </asp:Panel>
        <div id="UploadPopupDiv" runat="server"> 
        <asp:Label ID="Label1" runat="server" Text="Label" Visible="false" />
        <br />
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <asp:Button ID="Button2" runat="server" Height="25px" onclick="Button2_Click" Text="Upload" Width="128px" />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        <br />
      </div>
      </asp:Panel>
    </div>
  </form>
</body>
</html>

Open in new window

Alan
0
 
Alan WarrenApplications DeveloperCommented:
Upload a file, using an asp:FileUpload from within an ajax modal popup!
That's a good question, interested ...

Alan
0
 
sarabhaiCommented:
in modal popup, u should upload the file and save the file path in session because afterwards it required to stored the file path in database. if not to save the should delete this file using this file path.
if u have issue tell ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.