Home Contact RSS

Web Image Maker control by Tom Crane

Web Image Maker is an open source and pretty useful Image Maker control by Tom Crane. Feel free to give it a try as I can confirm that it works pretty fine.

Read more and download the code at http://www.theguildnetwork.com/tgn/articles/imageControl_page1.aspx

Strip all HTML tags from string

This is something we need very often. Either for security reasons or just because of some business rules. I will share a small code snippet with you as an extension method to string type so you can take advantages of using Regular Expressions to remove HTML tags from any user input.

public static string StripHtml(this string value)
{
    if (string.IsNullOrEmpty(value))
    {
        return string.Empty;
    }

    value = Regex.Replace(value, @"<(.|\n)*?>", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);

    return value;
}

And this is how you need to use it:

string var = txUserInput.Text;
var = var.StringHtml();

BuildManager.CreateInstanceFromVirtualPath

BuildManager is a class located in System.Web.Compilation namespace and contains a method named CreateInstanceFromVirtualPath. Using this static method, you may create instances of your UserControl, Page, Generic Handler or those kind of stuff. This might be useful in case you need to create some controls on the fly, etc.

The following piece of code shows how to use it.

UserControl instance = (UserControl)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath("~/Controls/Somefile.ascx", typeof(UserControl));

ASP.NET MVC Beta Has Been Released

For those who does not know, ASP.NET MVC Beta has been released this week. Click here to download.

jQuery and ASP.NET

A great news, Microsoft will be shipping jQuery with Visual Studio soon. The first shipment will be to support jQuery intellisense in Visual Studio as a free download.

For details please click here.

ASP.NET AJAX 4.0 CodePlex Preview 1

ASP.NET AJAX 4.0 Preview 1 was released on July 21.

This release contains a preview version of the following features (that are also described in our Roadmap document: http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14924):

* Client-side template rendering
* Declarative instantiation of behaviors and controls
* DataView control
* Markup extensions
* Bindings

One good news for JSON lovers, this is from release notes:

In the next version of ASP.NET AJAX, we plan on enabling new client-side data scenarios for both page and component developers. You will be able to get JSON data from the server and present it as HTML on the client in a highly manageable and performant way.

You may see the CodePlex project at http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=15511

Using DataPager with Code-Behind Data Source

Some of you might have already realized that DataPager does its job only if you use a DataSource control. For instance, if you want to set a ListView’s DataSource property in page’s Load event and expect DataPager control to successfully page the ListView control, it means that you will need to spend your hours to find why it does not work.

Even though I still can’t figure out why it does not work, here is the workaround. Just create a DataSource control (e.g.: LinqDataSource) control just with “ID” and “runat” properties and then hook the “Selecting” event. Also set the ListView’s “DataSourceID” property to the ID of the DataSource control. What you will do there is to set the “Result” property of the SelectEventArgs argument.

<asp:LinqDataSource ID="LinqDataSource1" runat="server" OnSelecting="LinqDataSource1_Selecting">
</asp:LinqDataSource>  

 

protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
e.Result = GetDataSource(); // Assuming that GetDataSource method returns the datasource.
}  

Now you can save your hours.

"Invalid postback or callback argument" Problem

In case you get the following exception while trying to run an ASP.NET application, the solution is not as simple as setting the "enableEventValidation" configuration to "false" all the time.

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

I had such a problem last week. One of my colleagues asked me to check a problem because he could not see it and everything used to work perfectly before. I must confess that I spent hours to find the problem. Even if I set the "enableEventValidation" value to "false", nothing worked and everything started to lose its ViewState data.

Finally, I’ve found that the problem was a forgotten "form" tag within a user control. So, if you have such a problem about losing ViewState unexpectedly and exceptions about Event Validation, double check your code not to have such <form> tags.

ConfigSource Property: Dividing configuration files into pieces

If you need to divide the web.config or app.config file into pieces for any reason – imagine you just don’t like a lot of settings of components that you will never need to change – it is even possible in .NET. The only thing you have to find out is how to use “ConfigSource” property of System.Configuration.SectionInformation (MSDN: Contains metadata about an individual section within the configuration hierarchy.) class.

Each configuration section that derives from this class has the ability of getting its configuration settings from a separate “config” file.

In a case that you would like to store your connections strings in a different “config” file, the way you have to follow is shown in the example below.

web.config file:

<?xml version="1.0"?>
<configuration>
  <connectionStrings configSource="ConnectionStrings.config" />
  <system.web>
    <!-- settings... -->
  </system.web>
</configuration>

ConnectionStrings.config file:

<connectionStrings>
  <clear />
  <add name="SqlServer" connectionString="..." />
</connectionStrings>

.NET Framework source code is now open to the public

I know that we all have been waiting for this great news for a long time. Now you can download symbols for .NET Framework source code and you can see or even debug the framework’s itself.

ScottGu has a long post describing how you can access this feature in Visual Studio 2008.

This is what he says:

Last October I blogged about our plan to release the source code to the .NET Framework libraries, and enable debugging support of them with Visual Studio 2008. Today I’m happy to announce that this is now available for everyone to use. Specifically, you can now browse and debug the source code for the following .NET Framework libraries:

.NET Base Class Libraries (including System, System.CodeDom, System.Collections, System.ComponentModel, System.Diagnostics, System.Drawing, System.Globalization, System.IO, System.Net, System.Reflection, System.Runtime, System.Security, System.Text, System.Threading, etc).
ASP.NET (System.Web, System.Web.Extensions)
Windows Forms (System.Windows.Forms)
Windows Presentation Foundation (System.Windows)
ADO.NET and XML (System.Data and System.Xml)
We are in the process of adding additional framework libraries (including LINQ, WCF and Workflow) to the above list. I’ll blog details on them as they become available in the weeks and months ahead.

To read the rest of the post, please follow this page.

« Previous entries · Next entries »