Posts Tagged ‘asp.net’

ASP.NET GridView with an ObjectDataSource

Monday, October 26th, 2009

Just a quick tip:

I got the following exception today:

If a data source does not return ICollection and cannot return the total row count, it cannot be used by the GridView to implement server-side paging.

If you’re trying to return an IEnumerable from your select method, try changing it to a List or an array. That should clear the problem right up.

Configuring webHttpBinding When Using WCF with JQuery

Thursday, June 25th, 2009

Firstly, a disclaimer: I don’t have a lot of experience working with AJAX enabled WCF services, and from reading some of Rick Strahl’s posts on using JQuery with ASP.NET, I’m doing things in a really hackish and terrible manner. Hopefully my mistakes won’t impact the usefulness of this short tip.

When working with JQuery and AJAX enabled WCF services, I recently encountered the following error, through Firebug and Fiddler:

The maximum string content length quota (8192) has been exceeded while reading XML data.
This quota may be increased by changing the
MaxStringContentLength property on the XmlDictionaryReaderQuotas
object used when creating the XML reader.

This is caused by WCF webHttpBinding default having fairly draconion limitations on XML message length and depth. This thread on MSDN helped diagnose the problem, however it didn’t do much to help me solve my particular problem. Ultimately, I stumbled upon the solution quite accidentally.

Firstly, as per the MSDN thread, you need to add the custom binding configuration underneath the configuration section:

<system.serviceModel>
  <bindings>
    <webHttpBinding>
      <binding name="bindingConfiguration">
        <readerQuotas maxDepth="32" maxStringContentLength="2048000" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      </binding>
    </webHttpBinding>
  </bindings>
  ..
</system.serviceModel>

At first I thought this was a custom binding, and tried to replace the service endpoint binding to the custom binding defined above, however this was causing an “System.ServiceModel.ServiceActivationException” exception.

The above binding declaration is actually a configuration for the webHttpBinding. As such, you need to add the BindingConfiguration property onto your service endpoint, along side your binding declaration. This is as follows:

<service behaviorConfiguration="Your.Service.Behavior"
      name="Your.Service.Name">
    <endpoint address="" behaviorConfiguration="Your.Behavior.Configuration"
     binding="webHttpBinding" bindingConfiguration="bindingConfiguration" contract="Your.Service.Contract" />
</service>

This will allow you to send larger messages to your WCF services via AJAX, and get on with creating applications.

ASP.NET Ajax, FCKEditor and Firebug

Friday, February 13th, 2009

Firebugs

Today at work I had the pleasure of dealing with a functionality bug in our new CMS that is written in ASP.NET. Our CMS uses FCKEditor for the rich text editor, and we’re using a whole gang of ASP.NET Ajax stuff for the interface. Specifically, we’re using a TabContainer with user controls inside each tab.

The CMS is designed in the way that most CMS’s are – we have 2 major components. Page objects contain the content data of a page, so text, HTML meta data, etc. Template objects contain templates – that is, HTML files with areas where you insert the page content, similar to how Dreamweaver does it’s templates for you.

When you publish a page/site, the CMS compiles the content into the template, then saves that as a file. When you’re editing or creating a page in the admin interface, the application reads the content areas available in the current template you have selected for that page, and creates an FCKEditor dialog for it.

Because you might not know how the content you’re writing fits into the template you’re going to use, we provide a preview function. This preview function will read in the content of the FCKEditor dialogs, create a temporary page object, set the src of an iframe to our preview page, which takes the page objects and feed it to a preview function which will compile the page and return the string, which we dump to the output stream of the preview page. We then fire up the iframe inside an ASP.NET popup extender, for a nice modal preview.

This all worked fine, until we got the tab container involved.

In order to streamline the work flow and reduce the page refreshes on postbacks, we decided to put the content in a tab container, with different parts of the page in different tabs. When we did this, we noticed that when the preview button did a partial postback, it reloaded the FCKEditor dialogs from the database, obliterating the changes we made to the page (if any), or just displaying nothing for the content if it was a new page.

This was just on my computer.

On a co-workers computer, it worked perfectly as intended, despite the fact we were both working from the same source, in the same IDE, running it in the same browser. What gives?

After poking around on both computers and trying the application in different browsers, we concluded that it doesn’t work only in my Firefox – IE was fine on my computer, and all browsers were fine on his. Acting on a hunch, I disabled Firebug for my Firefox, because I noticed my co-worker didn’t have Firebug installed.

The application started working again.

I don’t know why this happens, and why Firebug should be interfering with the Javascript in an obtrusive way, or even which Javascript it’s interfering with. I don’t know if Firebug was screwing with FCKEditor or ASP.NET Ajax. So, this post is partially to put this info out there, in the hopes that someone else can answer for me.

It’s also partially to say, if you’re seeing odd behavior that seems to be cropping up inconsistently across machines running Firefox, try disabling Firebug. Since being done by Firebug, I’ve decided to leave it off during all development, and thinking back, I wonder how many other Javascript bugs I’ve fought with that were caused by Firebug, and not the code.

Compile a User Control as a DLL

Monday, December 8th, 2008

At work, I’m redeveloping the old, ASP 3.0 CMS application and replacing it with a shiny new .NET 3.5 implementation. The software is designed to be run as a virtual directory from all websites, and thus shares the same from installation to installation.

The old CMS is a mutant. It started as an ASP 3.0 application, and then had extra bits made of .NET 1.0 & .NET 2.0 tacked on to it, much in the same manner that Frankenstein’s monster was created. The core ASP 3.0 application handled the page/news creation and publishing, and served as the interface.

The additions were mostly in the form of user controls (.ascx files) that were placed in the content for a page that needed one. These user controls took care of things like random image rotators, contact forms, bread crumb menus, etc. Various sites used a various sub-section of a pool of controls.

The modus operandi with the current system is that each installation has it’s own collection of controls which are inserted into pages by us on behalf of the client, because it involves putting ASP.NET markup into the content areas.

I’ve decided this isn’t such a great idea, and as such, I’m centralising the user controls as a library, seperate from the CMS, so that each site accesses the single pool of controls, and if a client leaves, it will be relatively simple to recompile a new library with the controls they use.

Anyway, that’s the back story. Essentially what I need to do is compile a whole load of .ascx files into a dll, and load them into pages. I’ve spent all morning looking on Google for tips on how to do this. And frankly the results were less than helpful, however I have a history of not reading documentation properly.

What I found out you need to do, is to add a class library project to your solution, and add the .ascx file as per normal. However, you need to do several steps to get this working properly:

  • Go to the properties of your .ascx file and change the build action to Embedded Resource.
  • From your control declaration on the .ascx file, delete AutoEventWireup. We need to wire these things up ourselves. Personally I cleared it of everything except the language attribute.
  • In the old code behind (now unattached class file) for the .ascx, you need to override the OnLoad event handler, and put the following code:
    string content = String.Empty;
    Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(GetType(), "example.ascx");
    using (StreamReader reader = new StreamReader(stream))
    {
    content = reader.ReadToEnd();
    }
    Control userControl = Page.ParseControl(content);
    this.Controls.Add(control);
    

    This source will use reflection to open the currently executing dll (in this case, our .ascx library) and find the content for the .ascx, parse it as a new control and add it to the currently executing context.

  • For every control on the .ascx, in our now orphaned .ascx.cs you need to declare a private/protected variable of the same time, with the same variable name as the ID on the control (for sanity’s sake). You need to find this control in our newly loaded .ascx and assign a reference:
    labelInAscx = (Label)control.FindControl("labelInAscx");
    
  • The above two steps is exactly what the AutoWireUp attribute on the control declaration does for you. We do it manually because we have to use reflection to extract the content of the ascx file, which is compiled into our dll.

After taking these steps, compile your project, and in another project, on a regular aspx page, register the assembly and use as you would any normal .ascx and hopefully the content will render out fine.

Compile .ascx as DLL solution for Visual Studio 2008