0

作为我上一个问题的伪跟进。

我当前的代码类似于以下内容:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="MyPage.aspx.vb" Inherits="Project.MyPage" EnableEventValidation="false" %>
<head>...</head>
<body id="Body" class="Window" runat="server">
<form id="MyForm" runat="server" defaultbutton="SubmitLinkButton">
    <!-- Markup for a the SubmitLinkButton and DropDownList -->
    <!--    to pick which Table is shown                    -->
    <asp:Table ID="Table1" runat="server">
        <asp:TableRow class="row" runat="server">
            <asp:TableCell runat="server">
                <pre>    Some Input1    </pre>
                <pre>___________________</pre>
                <pre>|___<asp:Textbox ID="Textbox1" runat="server"></asp:Textbox>____|</pre>
                <pre>|_________________|</pre>
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
    <asp:Table ID="Table2" runat="server">
        <asp:TableRow class="row" runat="server">
            <asp:TableCell runat="server">
                <pre>    Some Input2    </pre>
                <pre>___________________</pre>
                <pre>|___<asp:Textbox ID="Textbox2" runat="server"></asp:Textbox>____|</pre>
                <pre>|_________________|</pre>
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
</form>
</body>

方法很重要...

Public Sub SubmitLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitLinkButton.Click
    Dim result As String = vbNull

    Dim sw As New StringWriter
    Dim htmlWriter As New HtmlTextWriter(sw)

    If (DropDownList.SelectedValue IsNot "") Then
        Try
            Select Case DropDownList.SelectedValue
                Case "Table1"
                    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Table)
                    Table1.RenderControl(htmlWriter)
                Case "Table2"
                    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Table)
                    Table2.RenderControl(htmlWriter)
            End Select

            htmlWriter.RenderEndTag()
            htmlWriter.Flush()

            result = sw.ToString()
        Finally
            htmlWriter.Close()
            sw.Close()
        End Try

        With New [Reference To A Class]
            .SendMyEmail("Email Header", result) 'Email(header, body)
        End With
    Else
        'Do something else that isn't important right now
    End If
End Sub

Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    'No code necessary
End Sub

所有这些东西都有效!我采用表单,将 html 呈现给result变量,然后通过电子邮件发送出去!惊人的!

我目前的问题是:

A)当任何用户输入为空时,空输入将[ ]完全呈现。[, 10 个空格, 然后]. 不幸的是,这弄乱了表单的格式。它被写出来看起来像一个类似 ASCII 的表。有没有办法让没有用户输入仅显示为输入元素允许的空白空间?(例如<asp:Textbox ... Columns="8" ...></asp:Textbox>仅显示为(八个空格)。)

B) 在提交时,我收到了电子邮件,但网页显示“一个页面只能有一个服务器端表单标签”。错误信息。只有一个表单标签。您在上面的示例代码中看到的标记是准确的,即使它并不详细。

C) 用户需要能够打印此表格。CSS 中有特定的分页符设置,但是一旦通过电子邮件发送,这些分页符就不存在了。我怎样才能让这个表格以用户想要的方式打印出来?

编辑:我能想到的一种解决方法是以某种方式发送打印的 HTML 表单。执行打印到 PDF 之类的操作,并通过电子邮件而不是呈现的 HTML 表单发送 PDF。任何帮助都是极好的!

编辑:我更改了SubmitLinkButton_Click代码以专门查看每个表格而不是整个表格。

4

1 回答 1

1

您应该能够从代码隐藏中检索和设置控件的文本值,例如(来自https://stackoverflow.com/a/4674181/2953322):

' Function to find controls
Public Shared Function FindControlRecursive(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
    If parent Is Nothing Then 
        Return list
    If parent.GetType Is ctrlType Then
        list.Add(parent)
    End If
    For Each child As Control In parent.Controls
        FindControlRecursive(list, child, ctrlType)
    Next
    Return list
End Function

你的代码:

Case "Table1"

    Dim allInputs As New List(Of Control)
    For Each txtBox As TextBox In FindControlRecursive(allInputs, Me, GetType(TextBox))
        If String.IsNullOrEmpty(txtBox.Text) Then
            txtBox.Text = "        "
        End If
    Next

    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Table)
    Table1.RenderControl(htmlWriter)

Case "Table2"

'Rest of your code

顺便说一句,我在评论中放了 8 个空格,它显然被格式化为“”。

于 2017-10-23T11:56:45.207 回答