1

我在表单上有以下代码,以便用户提交表单并发送和发送电子邮件。我有两个 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>
4

3 回答 3

7

要设置Label控件的文本,您只需在其.Text属性中设置一个值。所以代替这个:

Response.Write("some string");

你会有这个:

lblSomeLabel.Text = "some string";

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

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

在旁注中,对您的代码的几句话建议......

查看C# 中using语句。它以更优雅的方式处理try/finally任何实现IDisposable(您需要调用的任何东西)的构造。.Dispose()从本质上讲,它节省了您自己编写处理代码的需要,并且通常会产生更清洁和更安全的代码。

在您的catch声明中,您目前完全忽略了您实际捕获的异常。这是故意的吗?或者正在使用此代码修复您计划的这一部分?作为一般规则,永远不要丢弃异常数据。它包含实际发生的错误消息而不是“发生了不好的事情”)以及发生的位置。

于 2012-04-05T00:54:17.357 回答
0

Better to use labelname.text="yourtext";

Or the text property value of the label as mentioned above.

于 2012-04-05T02:34:06.760 回答
0

如果你有一个 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!";
于 2012-04-05T00:59:15.563 回答