Reputation: 12970
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)
{
}
}
}
Upvotes: 0
Views: 4351
Reputation: 5689
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"});
Upvotes: 3
Reputation: 3925
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
Upvotes: 0
Reputation: 3469
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;
Upvotes: 0