.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Post New Web Links

interesting problem about @ and extension methods.

Posted By:      Posted Date: October 22, 2010    Points: 0   Category :ASP.Net

Note , that i'm new to c# so it might not be a problem.

first , what is the exact meaning of @ in Razor ?

does it mean WebPage.Response.Write ?

next, i designed an extension method to WebPage ( by the way that is the coolest feature ever in c# )

in my Razor code , to use it in a webpage , i must write @this.MyExtensionMethod() instead of @MyExtensionMethod().

So why is it?  because @this.MyExtensionMethod() and @MyExtensionMethod() should be the same ?

here is my extension method :

using System;
using System.Web;
using System.Web.WebPages;

namespace MyExtenstionMethods{

	public static class Utility{

	    // make the page to render optional
		public static dynamic RenderOptionalPage(this WebPage Webpage ,string page="",params object[] data){

				return Webpage.RenderPage(page,data);
            }catch(HttpException ex){
                return new HtmlString("<!-- page not found -->");


View Complete Post

More Related Resource Links

The Decorator Pattern & Extension Methods

The decorator pattern provides a formal way to add new functionality to an existing type, without sub-classing. First question: What is wrong with classical inheritance? In and of itself, nothing is wrong with the good old Is-A relationship. It is very common to derive a new class from an existing base class in order to override a few virtual members, and add a pinch of new functionality.

The Decorator Pattern & Extension Methods

Numerous design patterns exist in the software world today, and ultimately nothing is preventing you from making your own (if people buy into it or not is another question). Furthermore, there is no 'master list' of patterns that all developers agree upon. While this is true, if you pick up any book on the subject, you will find a set of very common patterns most programmers agree are very useful. Many of these patterns were first formalized in the seminal book on the subject, Design Patterns: Elements of Reusable Object-Oriented Software (aka, the Gang of Four [GOF] book; not to be confused with the UK punk band of the same name).

Basic Instincts: Extension Methods


This column discusses one of the new features of Visual Basic 2008-Extension Methods. This powerful new feature was introduced to support LINQ. See what extension methods are, how to apply them yourself, and get some great usage tips.

Adrian Spotty Bowles

MSDN Magazine November 2007

Interesting problem with getting OUTPUT parameters from SQL Server using C#

 I have the following SP: Create procedure dbo.example(@test INT OUTPUT)ASBEGINSET @test = 5SELECT * from UsersENDGO  and when I try calling it from C# using an SqlCommand with adding an output parameter and getting it's value, I get that the value is NULL. (I should mention that I use ExecuteReader)However,  if I alter the SP and remove the select line, the c# code works and returns the value 5. 

Problem with the XML returned by web methods


Hi, I have a weird problem with the XML returned by web methods.

I have a class that is auto-generated by a tool. The tool reads a table from the database and generates the class automatically. The "id" field is not supposed to be modified as it is just a representation of the "id" field of the database and is auto incremental.
Let's see the whole problem through a example:
 Table in database

Interesting Problem WPF Visual to Image Convert.


I am converting the Canvas to Image By using

renderBitmap = new RenderTargetBitmap(desiredWidth, desiredHeight, 96d, 96d, PixelFormats.Pbgra32);// PixelFormats.Pbgra32);
                    //RenderOptions.SetEdgeMode(drawingVisual, EdgeMode.Aliased);
                    RenderOptions.SetBitmapScalingMode(drawingVisual, BitmapScalingMode.HighQuality );
                    drawingVisual.SnapsToDevicePixels = true;
                    var dv = new DrawingVisual();
                    using (DrawingContext ctx = dv.RenderOpen())

                        var vb = new VisualBrush(drawingVisual);

Extension methods and viewstate


Hi all,

I am creating 2 extension methods for a textbox, which need to store a value and get the value, same like property, unfortunately dotnet 3.5 doesnt support extension property, so created two extension method for set and get, I am so confusing where should I store the value, like session, cache or anything else, but my recommendation to store in the viewstate object. how can I store value to viewstate. 

example my code look like

public static class clsExt
    public static bool getValue(this TextBox tb)

        return (bool)Viewstate[tb.ID];

    public static void setValue(this TextBox tb, bool value)

        Viewstate[tb.ID] = value;


Its not good idea to store session or cache, bcoz its should store only in the pagelevel, how can do this? How other textbox property storing? Could any one help on this issue please?

Thanks in advance.

Interesting problem with getting OUTPUT parameters from SQL Server using ASP


I had an sqldatasource. I had set its parameter as follows

<asp:SqlDataSource ID="FvDs" runat="server"
InsertCommand="INSERT INTO [Type] ([ShortName], [LongName], [Version]) VALUES (@ShortName, @LongName, @Version);

Then i had retrived the value in OnInserted event of the datasource

protected void FvDs_OnInserted(object sender, SqlDataSourceStatusEventArgs e)
        FvDs.SelectParameters["ID"].DefaultValue = e.Command.Parameters["@ID"].Value.ToString(); 

For the above code it is working fine.

But now i changed the code as follows:

<asp:SqlDataSource ID="FvDs" runat="server"

and here is the code for my stored procedure

ALTER PROCEDURE dbo.usp_InsertData
	@ShortName varchar(20),
	@LongName varchar(50),
	@Version varchar(20),
			INSERT INTO [Type] ([ShortName], [LongName], [Version]) VALUES (@ShortName, @LongName, @Version)

Creating HTML Helpers with Extension Methods


namespace MvcLogin_Validation.Helpers
    public static class LabelExtensions
        public static string Label(this HtmlHelper helper, string target, string text)
            return String.Format("<label for='{0}'>{1}</label>", target, text);



Above is my html helper class.

<%= Html.Label("first name", "First name") %>

This is how I use it.

Now while using it I am getting an error which says, "  No overload for method 'Label' takes '2' arguments ", where as this link ( http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs) says to do it exactly as i did.

Can anyone please guide where I am going wrong.



file extension problem


I try to upload  .mdb file to wss 3.0 and get an error message. When I go to Central  Administration - Operations - Blocked files type, I can delete the extension from the list, but I cannot click ok - the change doesn't stay.  Please, help



what is Extension Methods and when, where we will we use it


what is Extension Methods and when, where we will we use it

DataBinding + Extension Method Problem


Inside my repeater I'm calling an extension method like so:


<asp:label id="fileSize" runat="server" text='<%# ((UploadedFile)DataBinder.GetDataItem(Container.DataItem)).Size() %>' />


he above is giving me an "Object not set to an instance of a object" exception inside the extension method. If I comment the above expression out, it works fine, and the items are binded wtih no problems. Any ideas?

Inexplicable problem with behavior extension in a WCF REST service


Not sure what happened, but behavior extensions that are necessary to support a WCF REST capability suddenly seem to not be registering. The problem is with the webAuthentication extension. In the Visual Studio 2010 IDE, is is blue underlined and the message states:

"The element 'behavior' has invalid child element "webAuthentication'."

I tried running it in Fiddler, and get the following HTTP 500 response:

"An error occurred creating the configuration section handler for system.serviceModel/behaviors: Extension element 'webAuthentication' cannot be added to this element. Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions"

The config file looks fine and has been working up until now. I haven't got a clue as to why this extension is not being recognized in the behavior.

Does anyone have any ideas on how to debug this?

My config code is:  

     <add name = "webAuthentication" type = "WcfRestContrib.ServiceModel.Configuration.WebAuthentication.ConfigurationBehaviorElement, WcfRestContrib, Version=, Culture = neutral, PublicKeyToken=89183999a8dc93b5"/>

VS 2005 .net 2.0 WSE Extension problem (part II)


I refer to http://forums.asp.net/t/1393488.aspx because it most closely resembles my problem, but does not provide an adequate solution.


The problem is this: we have 2 PC's. Both have .NET 2.0, .NET 3.0, WSE 3.0, and VS 2005  8.0.50727.762

The problem occurs when we add a web reference.


One PC generates a xxxWSE proxy class, and the other does not.

In other words, one PC generates 2 partial classes: one of which contains a "WSE" and is derived from Microsoft.Web.Services3.WebServicesClientProtocol, and another which contains a nearly identically named class without the "WSE" and is derived from System.Web.Services.Protocols.SoapHttpClientProtocol.

For example, lets say I add a web reference to my TestSoap web services. After adding a web reference, I have these two classes:

public partial class TestSoapWse : Microsoft.Web.Services3.WebServicesClientProtocol

public partial class TestSoap : System.Web.Services.Protocols.SoapHttpClientProtocol

...And the second PC generates only one class which does not contain the "WSE" extension.

For example, lets say I add a web reference to my TestSoap web services. After adding a web reference, I have but one cl

Extension Methods

Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type. (Source MSDN)

Using jQuery to directly call ASP.NET AJAX page methods

Here I am looking to explain how to call page methods using jQuery. Using jQuery to directly call ASP.NET AJAX page methods

Problem to group radio button across gridview rows

In general no one can easily group or make a single selection from radio button list inside gridview rows. There are a lot of way to make a single or unique selection from list. Here i want to share how i can address this problem in the most easiest way. Googling the problem most of the cases i saw the grouping in horizontal way. Thats why i tried to group the radio buttons in vertical manner. Let i have a requirement like below:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend