Example 02 - Moderate - Submit HTML form, save data to database, and output merged PDF with data

Imports FDFApp
Partial Public Class example_02
    Inherits System.Web.UI.Page
    Private Debug_Me As Boolean = False
    Private _example_dataset_datasource As String = ""
    Protected Sub Load_Examples_GridView(ByVal grid As GridView, Optional ByVal IsDefault As Boolean = False)
        Dim ds As DataSet, selectedIndex As Integer = 0
        ds = Examples_Dataset
        grid.DataMember = ds.Tables(0).TableName
        grid.DataSource = ds.Tables(0).DefaultView
        grid.DataBind()
        grid.Visible = True
        grid.Focus()
    End Sub
    Protected Property Examples_Dataset() As DataSet
        Get
            Dim ds As DataSet
            If Session("Example_Dataset") Is Nothing Then
                ds = New DataSet("Examples")
                ds.ReadXml(_example_dataset_datasource, XmlReadMode.Auto)
            Else
                ds = New DataSet("Examples")
                ds = CType(Session("Example_Dataset"), DataSet)
            End If
            Return ds
        End Get
        Set(ByVal value As DataSet)
            Session("Example_Dataset") = CType(value, DataSet)
        End Set
    End Property
    Private Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click
        Page.Validate()
        If (Page.IsValid) Then
            Dim ds As DataSet = Examples_Dataset
            Dim dr As DataRow = ds.Tables(0).NewRow
            dr("FULLNAME") = StripCodeFromString(Me.FULLNAME.Text & "")
            dr("EMAIL") = StripCodeFromString(Me.EMAIL.Text & "")
            dr("COMBOBOX") = StripCodeFromString(Me.COMBOBOX.SelectedValue & "")
            dr("CHECKBOX") = StripCodeFromString(Me.CHECKBOX.Checked.ToString() & "")
            dr("RADIOBUTTON") = StripCodeFromString(Me.frmRadiobutton0.Checked.ToString() & "")
            dr("LISTBOX") = StripCodeFromString(Me.LISTBOX.SelectedValue & "")
            ds.Tables(0).Rows.Add(dr)
            Examples_Dataset = ds
            Load_Examples_GridView(Me.Examples_Selection, False)
            Me.SubmitButton.Enabled = True
            Me.SubmitButton.CssClass = "button button-250 bgRed white"
        Else
            Update_Message("Message - Sent Failed!")
        End If
    End Sub
    Public Sub Example(ByVal dr As DataRow)
        ' DECLARE FDFApp & FDFDoc Classes
        Dim cFDFApp As New FDFApp.FDFApp_Class
        Dim cFDFDoc As New FDFApp.FDFDoc_Class
        ' INITIALIZE FDFDOC Class
        cFDFDoc = cFDFApp.FDFCreate
        ' SET FILE
        cFDFDoc.FDFSetFile(Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("/") + 1) & "example.pdf")
        ' SET FIELD VALUES From Datarow Field Values
        cFDFDoc.FDFSetValue("FULLNAME", CStr(dr("FULLNAME")))
        cFDFDoc.FDFSetValue("EMAIL", CStr(dr("EMAIL")))
        cFDFDoc.FDFSetValue("COMBOBOX", CStr(dr("COMBOBOX")))
        cFDFDoc.FDFSetValue("CHECKBOX", CStr(dr("CHECKBOX")))
        cFDFDoc.FDFSetValue("LISTBOX", CStr(dr("LISTBOX")))
        cFDFDoc.FDFSetValue("RADIOBUTTON", CStr(dr("RADIOBUTTON")))


        Store_Cookie("Example_FULLNAME", StripCodeFromString(Me.FULLNAME.Text))
        Store_Cookie("Example_EMAIL", StripCodeFromString(Me.EMAIL.Text))
        'Store_Cookie("Example_COMBOBOX", StripCodeFromString(Me.COMBOBOX.SelectedValue))
        'Store_Cookie("Example_CHECKBOX", StripCodeFromString(Me.CHECKBOX.Checked.ToString()))
        'Store_Cookie("Example_LISTBOX", StripCodeFromString(Me.LISTBOX.SelectedValue))
        'Store_Cookie("Example_RADIOBUTTON", StripCodeFromString(Me.frmRadiobutton0.Checked.ToString()))

        ' SET FRAME
        cFDFDoc.FDFSetTargetFrame = "frame_examples"
        ' SET DOC JAVASCRIPT
        cFDFDoc.FDFSetOnImportJavaScript("var x = app.alert('Thank you for trying FDFToolkit.net!',3);", False)
        ' CLEAR THE OUTPUT STREAM
        Response.Clear()
        If Debug_Me Then
            Response.ContentType = cFDFApp.MimeTXT
        Else
            Response.ContentType = cFDFApp.MimePDF
        End If
        ' WRITE MERGED PDF FILE TO BUFFER
        Response.BinaryWrite(cFDFDoc.PDFMergeFDF2Buf(Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("/") + 1) & "example.pdf", Me.frmPDF_Flatten.Checked))
        ' END RESPONSE
        Response.End()
    End Sub

    Private Sub Examples_Selection_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Examples_Selection.RowCommand
        If e.CommandName.ToLower = "select" Then
            Examples_Selection.SelectedIndex = CInt(e.CommandArgument)
            Dim ds As DataSet
            ds = Examples_Dataset
            Dim dr As DataRow
            dr = ds.Tables(0).Rows(Examples_Selection.SelectedIndex)
            Example(dr)
        End If
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _example_dataset_datasource = Server.MapPath(ConfigurationManager.ConnectionStrings("fdftoolkit_example_dataset").ConnectionString & "")
        Me.SubmitButton.Text = "Submit"
        Me.SubmitButton.Enabled = True
        Me.SubmitButton.CssClass = "button button-250 bgRed white"
        Me.ResetButton.CssClass = "button button-250 bgRed white"
        Me.ResetButton.Text = "Reset"
        If Not IsPostBack Then

            Me.FULLNAME.Text = StripCodeFromString(Get_Cookie("Example_FULLNAME") & "")
            Me.EMAIL.Text = StripCodeFromString(Get_Cookie("Example_EMAIL") & "")
            'Me.COMBOBOX.SelectedValue = StripCodeFromString(Get_Cookie("Example_COMBOBOX") & "")
            'Me.CHECKBOX.Checked = StripCodeFromString(Get_Cookie("Example_CHECKBOX") & "")
            'Me.LISTBOX.SelectedValue = StripCodeFromString(Get_Cookie("Example_LISTBOX") & "")
            Me.SubmitButton.Enabled = True
            Me.SubmitButton.CssClass = "button button-250 bgRed white"
            FULLNAME.Focus()
        End If
        Update_Message("Complete this form, and then press submit.")
    End Sub
    Private Sub Update_Message(ByVal strMsg As String)
        Me.lblMessage_Top.Text = "Status: " & strMsg
        Me.lblMessage_Bottom.Text = "Status: " & strMsg
    End Sub


    Private Sub ResetButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ResetButton.Click
        Me.SubmitButton.Enabled = True
        Me.SubmitButton.CssClass = "button button-250 bgRed white"
    End Sub

    Private Sub Examples_Selection_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Examples_Selection.SelectedIndexChanged

    End Sub
End Class