ASP.NET – How to write some html in the page? With Response.Write?

I need that some html in the area in the asp.net page that i am coding, is changed according to a string variable.
I was thinking about creating a label, and then change the text on it.

But the string variable contains something like:

<h2><p>Notify:</p> alert</h2>

So, I don’t feel that give this to a label text is a good idea

How i can do?
Using response.write?
If I use response.write, my added code will be at the beginning of the html source, how i can tell him to add it in a specific ?

Thank you

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you really don’t want to use any server controls, you should put the Response.Write in the place you want the string to be written:

<body>
<% Response.Write(stringVariable); %>
</body>

A shorthand for this syntax is:

<body>
<%= stringVariable %>
</body>

Method 2

why don’t you give LiteralControl a try?

 myLitCtrl.Text="<h2><p>Notify:</p> Alert</h2>";

Method 3

If you want something lighter than a Label or other ASP.NET-specific server control you can just use a standard HTML DIV or SPAN and with runat=”server”, e.g.:

Markup:

<span runat="server" id="FooSpan"></span>

Code:

FooSpan.Text = "Foo";

Method 4

ASPX file:

<h2><p>Notify:</p> <asp:Literal runat="server" ID="ltNotify" /></h2>

ASPX.CS file:

ltNotify.Text = "Alert!";

Method 5

Use a literal control and write your html like this:

literal1.text = "<h2><p>Notify:</p> alert</h2>";

Method 6

You should really use the Literal ASP.NET control for that.

Method 7

You can go with the literal control of ASP.net or you can use panels or the purpose.

Method 8

You can also use pageMethods in asp.net. So that you can call javascript functions from asp.net functions. E.g.

 [WebMethod]
    public static string showTxtbox(string name)
    {
         return showResult(name);
    }
      
    public static string showResult(string name)
    {
        Database databaseObj = new Database();
        DataTable dtObj = databaseObj.getMatches(name);

        string result = "<table  border='1' cellspacing='2' cellpadding='2' >" +
                                            "<tr>" +
                                                "<td><b>Name</b></td>" +
                                                "<td><b>Company Name</b></td>" +
                                                "<td><b>Phone</b></td>"+
                                             "</tr>";

        for (int i = 0; i < dtObj.Rows.Count; i++)
        {
            result += "<tr> <td><a href="javascript:link('" + dtObj.Rows[i][0].ToString().Trim() + "','" +
             dtObj.Rows[i][1].ToString().Trim() +"','"+dtObj.Rows[i][2]+ "');">" + Convert.ToString(dtObj.Rows[i]["name"]) + "</td>" +
                "<td>" + Convert.ToString(dtObj.Rows[i]["customerCompany"]) + "</td>" +
                "<td>"+Convert.ToString(dtObj.Rows[i]["Phone"])+"</td>"+
             "</tr>";
        }

        result += "</table>";
        return result;
    }

Here above code is written in .aspx.cs page. Database is another class. In showResult() function I’ve called javascript’s link() function.
Result is displayed in the form of table.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x