[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Ajax ModalPopupExtender

Posted on 2012-09-10
3
Medium Priority
?
608 Views
Last Modified: 2012-09-12
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

0
Comment
Question by:rcowen00
  • 2
3 Comments
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38386319
Upload a file, using an asp:FileUpload from within an ajax modal popup!
That's a good question, interested ...

Alan
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 38386422
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
 
LVL 26

Accepted Solution

by:
Alan Warren earned 2000 total points
ID: 38386568
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses how to implement server side field validation and display customized error messages to the client.
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question