响应.写入标签

本文关键字:标签 响应 | 更新日期: 2023-09-27 18:30:07

我在表单上有下面的代码,这样用户就可以提交表单并发送电子邮件。我有两个response.write语句,我想更改它们,以便它将它们写入标签(lblSubmit)。如有任何帮助,我们将不胜感激。

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {

        if (IsPostBack)
        {

            SmtpClient sc = new SmtpClient("smtp.talktalk.net");
            StringBuilder sb = new StringBuilder();
            MailMessage msg = null;
            sb.Append("Email from: " + txtEmail.Text + "'n");
            sb.Append("Message   : " + txtQuestion.Text + "'n");
            try
            {
                msg = new MailMessage(txtEmail.Text,
                    "myemail@talktalk.net", "Message from Web Site",
                    sb.ToString());
                sc.Send(msg);


                Response.Write("Message sent!");
            }
            catch (Exception ex)
            {
                // something bad happened
                Response.Write("Something bad happened!");
            }
            finally
            {
                if (msg != null)
                {
                    msg.Dispose();
                }
            }
        }
    }



</script>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<div id="title"><p>Contact Us...</p></div>

<div id="titletext"><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p></div>
<div id="contactform">

    <asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
    &nbsp;
    <asp:TextBox ID="txtName" runat="server" Width="189px"></asp:TextBox>
    <br />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
        ControlToValidate="txtName" ErrorMessage="Please enter your name" 
        ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator>
    <br /><br />
    <asp:Label ID="lblEmail" runat="server" Text="Email:"></asp:Label>
    &nbsp;
    <asp:TextBox ID="txtEmail" runat="server" Width="189px"></asp:TextBox>
    <br />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
        ControlToValidate="txtEmail" Display="Dynamic" ErrorMessage="Please enter an email address" 
        ForeColor="Red"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
        ControlToValidate="txtEmail" ErrorMessage="Please enter a valid Email address" 
        ForeColor="Red" 
        ValidationExpression="'w+([-+.']'w+)*@'w+([-.]'w+)*'.'w+([-.]'w+)*" 
        Display="Dynamic"></asp:RegularExpressionValidator>
    <br />
    <br />
    <asp:Label ID="lblPhone" runat="server" Text="Phone:"></asp:Label>
    &nbsp;
    <asp:TextBox ID="txtPhone" runat="server" Width="189px"></asp:TextBox>
    <br />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
        ControlToValidate="txtPhone" 
        ErrorMessage="Please enter a contact telephone number" ForeColor="Red" 
        Display="Dynamic"></asp:RequiredFieldValidator>
    <br />
    <asp:Label ID="lblQuestion" runat="server" Text="Question:"></asp:Label>
    <asp:TextBox ID="txtQuestion" runat="server" Height="103px" TextMode="MultiLine" 
        Width="189px"></asp:TextBox>
    <br />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
        ControlToValidate="txtQuestion" ErrorMessage="Please enter a message" 
        ForeColor="Red"></asp:RequiredFieldValidator>

    <asp:Label ID="lblSubmit" runat="server"></asp:Label>

<asp:Button ID="cmdSubmit" runat="server" Text="Submit" CssClass="cmdSubmit" 
        onclick="cmdSubmit_Click" PostBackUrl="~/Contact Us.aspx" />

</div>
</asp:Content>

响应.写入标签

要设置Label控件的文本,只需在其.Text属性中设置一个值。因此,取而代之的是:
Response.Write("some string");

你会有这个:

lblSomeLabel.Text = "some string";

这是Response.Write更受欢迎的,因为标签控件允许您在页面上指定一个位置作为占位符。Response.Write只是盲目地将字符串写入响应,可能会将其附加在HTML的其余部分之后,或者在在中间某个未定义的位置。

请注意,Label控件将文本包装在span元素中。如果要输出具有完整标记控制的原始数据,请考虑使用Literal控件。

顺便说一下,对您的代码有几句建议。。。

查看C#中的using语句。它以一种更优雅的方式处理任何实现IDisposable的东西(任何需要调用.Dispose()的东西)的try/finally构造。从本质上讲,它省去了您自己编写处置代码的需要,并产生了更干净、更安全的代码。

catch语句中,您当前完全忽略了实际捕获的异常。这是故意的吗?还是用这个代码来修复你计划的这一部分?作为一般规则,从不丢弃异常数据。它包含发生了什么(与"发生了不好的事情"相反)以及在哪里发生的实际错误消息。

如果您有ASP.NET标签控件,则可以设置该控件的Text属性值。

<asp:Label ID="lblSubmit" runat="server" Text="Label"></asp:Label>

在代码中,

lblSomeLabel.Text = "Email Sent";

您甚至可以使用带有runat="server"属性的Div控件,并从代码中设置其内部html

<div id="divMessage" runat="server" />

在代码中,

 divMessage.innerHTML="Email Sent!";

最好使用labelname.text="yourtext";

或者如上所述的标签的文本属性值。