Response.Write to label

Go To StackoverFlow.com

1

I have the code below on form so that the user submits the form and it sends and email. I have two response.write statements and I am wanting to change these so that a it writes these to a label (lblSubmit). Any help would be appreciated.

<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>
2012-04-05 00:52
by user1300580


7

To set the text of a Label control you'd just set a value in its .Text property. So instead of this:

Response.Write("some string");

you'd have this:

lblSomeLabel.Text = "some string";

This is vastly preferred over Response.Write because the label control allows you to specify a location on the page as a placeholder. Response.Write just blindly writes the string to the response, probably either appending it after the rest of the HTML or in some undefined place in the middle.

Note that a Label control wraps the text in a span element. If you want to output raw data over which you have complete markup control, consider using a Literal control.

On a side note, a few words of advice for your code...

Look into the using statement in C#. It handles the try/finally construct of anything that implements IDisposable (anything on which you'd need to call .Dispose()) in a more elegant way. Essentially it saves you the need to write the disposal code yourself and results in cleaner and safer code in general.

In your catch statement you're currently completely ignoring the exception that you actually catch. Is this intentional? Or is fixing this part of your plan with this code? As a general rule, never throw away the exception data. It contains the actual error message of what happened (as opposed to "something bad happened") as well as where it happened.

2012-04-05 00:54
by David
thank you for this : - user1300580 2012-04-05 01:00
Hi, the catch statement is something I am looking into and any help on how to actually show the error message rather than the comment "something bad happened" would be appreciated. Thankyo - user1300580 2012-04-05 01:06
@user1300580: Take a look at the ex object that you're catching: http://msdn.microsoft.com/en-us/library/system.exception.aspx It contains lots of useful information, most specifically .Message (the error message) and .StackTrace (the location where the error occurred). One thing to note is that you can create and throw custom exceptions when values violate the logic of the application. For example, if you have a function with an int argument, and the int should be > 0 then you can say throw new ArgumentException("Value must be a positive integer."); in the function - David 2012-04-05 01:10
@user1300580: As for displaying the errors to the user, there are many possibilities there. It all comes down to what you want the user to see (system errors or more user-friendly errors), when you want them to see it, etc. Some errors should be shown to the user, some should be silently logged, and so on. It's a matter of the logic of the application in that particular context. (For logging, you can look into a logging framework such as NLog or log4net to write errors to files, databases, email, etc. - David 2012-04-05 01:12
I would obviously want the users to see that they have encountered an error but would rather this be user friendly. Any errors that do occur would probably best be logged somehow so that they can be reviewed appropriately - user1300580 2012-04-05 01:23


0

If you have a ASP.NET label control, you can set the Text property value of that.

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

and in the code,

lblSomeLabel.Text = "Email Sent";

You can even use a Div control with a runat="server" property and set the inner html of that from your code

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

and in the code,

 divMessage.innerHTML="Email Sent!";
2012-04-05 00:59
by Shyju


0

Better to use labelname.text="yourtext";

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

2012-04-05 02:34
by user1270384
Ads