简体   繁体   English

使用RenderControl ASP.NET VB HTML时将空白输入显示为方括号

[英]Empty input shown as brackets when using RenderControl ASP.NET VB HTML

As a pseudo follow up from my previous question . 作为我先前问题的伪跟进。

My current code looks similar to the following: 我当前的代码类似于以下内容:

<%@ 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>

The method that matters... 重要的方法...

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

All of this stuff works! 所有这些东西都有效! I takes the form, renders the html to the result variable, and sends it off in an email! 我采用表单,将html呈现为result变量,然后通过电子邮件发送出去! Awesome! 真棒!

My current issues are: 我当前的问题是:

A) When any user input is empty, the empty input renders as [ ] exactly. A)当任何用户输入为空时,空输入将准确呈现为[ ] [ , 10 spaces, then ] . [ ,然后是10个空格] Unfortunately, this messes up the formatting of the form. 不幸的是,这弄乱了表格的格式。 It's written out to look like an ASCII-like table. 它写出来看起来像一个类似ASCII的表。 Is there a way for no user input to be displayed as solely the empty space allowed by the input element? 有没有一种方法可以将没有用户输入的内容仅显示为输入元素所允许的空白? (Eg <asp:Textbox ... Columns="8" ...></asp:Textbox> only showing as (例如<asp:Textbox ... Columns="8" ...></asp:Textbox>仅显示为 (eight spaces).) (八个空格)。

B) On submit, I get the email, but the webpage shows an "A page can have only one server-side Form tag." B)在提交时,我收到了电子邮件,但是网页上显示“一个页面只能有一个服务器端Form标记”。 error message. 错误信息。 There is only one Form tag. 只有一个Form标签。 The markup you see in the example code above is accurate even though it isn't detailed. 即使没有详细说明,您在上面的示例代码中看到的标记也是准确的。

C) The user needs to be able to print this form out. C)用户需要能够打印此表格。 There are specific page-breaks setup in the CSS, but once this is emailed, those page breaks don't exist. CSS中有特定的分页符设置,但是通过电子邮件发送后,这些分页符就不存在了。 How can I get this form to print out the way the user wants? 如何获得此表格以打印出用户想要的方式?

EDIT : One fix I can think of would be to send the HTML form printed somehow. 编辑 :我可以想到的一种解决方法是以某种方式发送HTML表单。 Do something like printing to PDF and send the PDF in the email instead of the Rendered HTML Form. 执行类似打印到PDF的操作,然后通过电子邮件而不是“渲染的HTML表单”发送PDF。 Any help would be awesome! 任何帮助都是极好的!

EDIT: I changed the SubmitLinkButton_Click code to look at each table specifically rather than the entire form. 编辑:我更改了SubmitLinkButton_Click代码以专门查看每个表,而不是整个表单。

You should be able to retrieve and set the text value of the control from code-behind with something like (from https://stackoverflow.com/a/4674181/2953322 ): 您应该能够通过类似下面的代码(从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

Your code: 您的代码:

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

By the way, I had put 8 spaces in my comment, it obviously got formatted into " ". 顺便说一句,我在评论中加了8个空格,显然它被格式化为“”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM