0

I'm trying to figure out a way where I can write html code to a SharePoint Web Part with C#. I don't even know if this is possible but I'm assuming it is. In JavaScript it would just be something along the lines of document.write("<br>" + variable + "</b>") so I'm just trying to figure out how this will work in C#. There is a much bigger picture than this but this is the basic portion I'm stuck on.

I would like the HTML Code to be inside of the for loop below.

Here's the code:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Web;

namespace CSharpAttempt.VisualWebPart1
{

public class getTheSPItems
{
    public void Page_Load(object sender, EventArgs e)
    {
        int i;

        for (i = 0; i <= 10; i++)
        {
            string theHtmlString = "<b>" + i + "</b>";
            Response.Write(theHtmlString);

        }
    }
}



public partial class VisualWebPart1UserControl : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
}

3 Answers 3

3

A SharePoint WebPart is just an ASP.NET control.

You can add any other SharePoint controls, WebControls, or html to it by adding to the controls collection.

this.Controls.Add(new HtmlGenericControl("div") { InnerText ="My HTML"});
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. Can you give me a better example, please?
in your VisualWebPart1UserControl class, in the Page_Load() method, you would add the snippet above. I am not sure what your getTheSPItems class is for - that wouldn't be called at all based on your code above.
Error 1 The type or namespace name 'HtmlGenericControl' could not be found (are you missing a using directive or an assembly reference?) c:\users\administrator\documents\visual studio 2010\Projects\CSharpAttempt\CSharpAttempt\VisualWebPart1\VisualWebPart1UserControl.ascx.cs 32 35 CSharpAttempt
add using System.Web.UI.HtmlControls; to your cs file.
this.Response.Write("<div>my html</div>")
|
0

Easy answer,

Response.Write (theHtmlString);

Source

Or

If you want to add it to a specific part of your page then drag on a literal control to your webform.

Use

LiteralControlName.Text = theHtmlString;

2 Comments

Yea I cannot figure this Response.Write out. Everytime I try and use it I get an error: The name 'Response' does not exist in the current context
@mwilson add system.web, I only see system.web.ui
0

Yes, you can. If you put an ASP.Net control on the page and give it an ID you can refer to it in code behind very trivially.

See this stack overflow answer: How do I set an ASP.NET Label text from code behind on page load?

EDIT: see this new SO answer for basic ASP.Net form manipulation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.