Emil’s Blog

Programming Windows, .Net, EPiServer and whatnot…

[Powered by WordPress.]

January 26, 2007

Retrieve the Html code for a Web User Control

by @ 14:37. Filed under .Net programming, EPiServer

Here's how to extract the Html code for a web control:

public static string GetHtmlForControl(Control control)
{
    StringWriter textwriter = new StringWriter();
    HtmlTextWriter writer = new HtmlTextWriter(textwriter);
           
    control.DataBind();
    control.RenderControl(writer);
    string html = textwriter.GetStringBuilder().ToString();

    return html;
}

Tip: If you're having problems with the generated Html being incomplete, then maybe you're calling the function above in the wrong time? I've been having some problems with databound EPiServer controls until we discovered that we were doing this too early. When we started doing it in the PreRenderComplete event of the page, then it started working:

protected void Page_PreRenderComplete(object sender, EventArgs e)
{
    string html = FormHelper.GetHtmlForControl(Butikshuvud);
}

More useful regular expression examples

by @ 11:34. Filed under .Net programming

Here are some more Regex examples that may come in handy:

// Remove Html comments
//html = Regex.Replace(html, @"\<!--[^>]*-->", "");
html = Regex.Replace(html, @"\<!--.*?-->", ""); // new version!

// Validate an email address (with named groups)
Regex regex = new Regex(@"^(?<user>[^@]+)@(?<host>.+)\..+$", RegexOptions.None);
Match m = regex.Match(epost);
/* Check m.Success to decide if valid or not */

// Validate a Swedish zip code ("postnummer")
regex = new Regex(@"^(\d\d\d \d\d|\d\d\d\d\d)$", RegexOptions.None);
m = regex.Match(postnummer);

In the first example, note .*? sequence. The question mark makes the match lazy, as opposed to the default greedy matching strategy. This makes a difference when there's more than one comment in the html string. In my first try above I used [^>] to avoid matching the end comment marker, with the unfortunate side effect of missing comments with tags as content.

I have also found a useful online Regex testing service here:

http://www.regexlib.com/RETester.aspx

(Update 2009-04-28: Changed service, the previous one is now unavailable...)

January 24, 2007

Open an EPiServer page

by @ 11:07. Filed under EPiServer

Here are a few ways to retrieve data for a given page (expressed in a PageReference instance) in EPiServer:

PageReference pageref = ...;
PageData pagedata;

// In a page (which is a sub-class of PageBase, e.g. TemplatePage, EditPage, SimplePage)
pagedata = this.Getpage(pageref);

// In a user control (which is a sub-class of UserControlBase)
pagedata = this.Getpage(pageref);

// Anywhere
pagedata = Global.EPDataFactory.GetPage(pageref);

Global.EPDataFactory also has some other useful functions:

// Open a page from a given FriendlyUrl rather than a PageReference
// (which requires knowing the Page ID)
pagedata = Global.EPDataFactory.GetPageByURL("/Bokningsbara-kurser/");

BTW, it's also possible to initialize a PageReference with a Url (although NOT a Friendly Url):

pageref = PageReference.ParseUrl("http://localhost:8889/shop/Puff.aspx?id=130");

UPDATE (2007-06-04):

If you have a so called "simple address" such as "/foo", then none of the above will work. In that case you have to use code such as this:

PropertyCriteriaCollection critcol = new PropertyCriteriaCollection();

PropertyCriteria crit = new PropertyCriteria();
crit.Condition = EPiServer.Filters.CompareCondition.Equal;
crit.Name = "PageExternalURL";
crit.Type = PropertyDataType.String;
crit.Value = simple_address.Trim(new char[] { '/' }); // Remove initial "/"
critcol.Add(crit);

PageDataCollection pagecol = Global.EPDataFactory.FindPagesWithCriteria(Global.EPConfig.RootPage, critcol);
if (pagecol.Count> 0)
{
   return pagecol[0].LinkURL;
}

January 18, 2007

Get URL from an EPiServer PageReference

by @ 15:38. Filed under EPiServer

Retrieving the URL to an EPiServer page seems like a basic thing to do, but it's not quite obvious...

Referring to a page is normally represented by an instance of the PageReference class, so obviously that contains a public property called Url or something, right?

Wrong!

Instead, this is how to do it (as a bonus I also show how to retrieve the PageReference from a page property):

string url = "";
PropertyData pd = CurrentPage.Property["Shop_CartPage"];
if (pd != null && pd.IsNull == false)
{
    PageReference pageref = (PageReference)pd.Value;
    url = GetPage(pageref).LinkURL;
}

If anyone knows better or more efficient ways to do this, please add a comment to this post :-)

January 17, 2007

Pages without page types in EPiServer

by @ 12:46. Filed under EPiServer

When creating "normal" ASPX pages (i.e. pages that are not EPiServer page types) to use in EPiServer sites it can be very useful to have them inherit from TemplatePage or SimplePage (both are EPiServer types) so that dynamic properties can be read, page methods called, etc. Unfortunately this will give rise to an error message similar to this:

The current template "/shop/Varukorg.aspx" does not match the specified page type file "/default.aspx"

This is because TemplatePage and SimplePage assumes that they are part of an EPiServer page type. This problem can be fixed by overriding ValidatePageTemplate() with an empty implementation:

public override void ValidatePageTemplate()
{
}

Voilá! No more error message.

BTW, try finding this information in the EPiServer docs. Not so easy...

January 15, 2007

User Controls in EPiServer

by @ 16:19. Filed under EPiServer

This is my first post about EPiServer, which basically is a framework for building web sites. It's very capable and extensible and is in wide use in Scandinavia. For more information, look here: http://www.episerver.com.

Web User Controls in EPiServer solutions should always inherit from EPiServer.UserControlBase rather than the usual System.Web.UI.UserControl. This gives access to useful functions and properties such as

Note that those functions work even if the current page is not a "proper" page type in EPiServer so it's missing from the page hierarchy. Very useful!

January 8, 2007

Character encodings when writing strings to a stream

by @ 19:14. Filed under .Net programming

Here's how to write a string into a stream using a given character encoding:

UTF8Encoding encoding = new UTF8Encoding();
//Encoding encoding = Encoding.GetEncoding("iso-8859-1");
byte[] bytes = encoding.GetBytes(postData);
stream.Write(bytes, 0, bytes.Length);

January 5, 2007

Regular expression examples

by @ 13:27. Filed under .Net programming

Some useful Regex code for easy reuse. More will come...

For documentation about regular expressions in the .Net Framework, look up Regular Expression Language Elements in the online help, or look here:

http://msdn2.microsoft.com/en-us/library/az24scfc.aspx

// Remove namespace prefixes from Xml element names
string str = Regex.Replace(xml, "(</?)([a-zA-Z_]+:)([a-zA-Z_]+)", "$1$3");

January 3, 2007

Refresh Visual Studio Add-in settings

by @ 13:47. Filed under Visual Studio tips

Here are some steps to try if you're having problems with addins in Visual Studio (for example if toolbars or menus have vanished, or have been duplicated).

First, close all Visual Studio instances.

Then open a command line console with Visual Studio settings (e.g. "Visual Studio 2005 Command Prompt" from the start menu) and type:

devenv /resetaddin *

That line will clear the settings for all addins and cause them to refresh menus and toolbars the next time Visual Studio is started. To do that with an individual add-in, which requires knowing its class name, including namespace, type

devenv /resetaddin CodeAssist.Connect

If the above does not help, then try this (NOTE: This will delete all customization done to Visual Studio, such as keyboard mappings, etc!):

devenv /resetsettings
devenv /resetaddin *

When starting Visual Studio, you should now face a fresh and clean environment.

[powered by WordPress.]

jour·nal n. A personal record of occurrences, experiences, and reflections kept on a regular basis; a diary.

Internal links:

Categories:

Search blog:

Archives:

January 2007
M T W T F S S
« Dec   Feb »
1234567
891011121314
15161718192021
22232425262728
293031  


View Emil Åström's profile on LinkedIn

General links:

I read:

Visitors

Recent Comments

Spam caught

Other:

Clicky Web Analytics

35 queries. 0.494 seconds