Jan 31 2006

Convert UserControls to Custom Controls

Category: ASP.NET 2.0 - GeneralBil@l @ 21:40

I found a nice article today on MSDN titled as:
Turning an .ascx User Control into a Redistributable Custom Control

I will summarize as much as possible of that article, since it is really a valuable one.

It talks about creating your own UserControls, then converting them into CustomControls.

Create a New Web Site in Visual Studio 2005 (I guess it should work in VWD too, haven't tried it). Name it WebUserControl (Namespace Name!!!)

  • Add a new Web User Control
  • Design the control the way you want. For the same of this post, I created this simple user control shown below, you can use either Code-Beside or Code-Inline:
<%@ Control Language="C#" ClassName="WebUserControl.HelloWorld" %>

<script runat="server">

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        this.lblMsg.Text = Server.HtmlEncode(this.txtMsg.Text);
    }
</script>

<strong>Hello World Custom-User Control<br />
</strong>
<br />
Enter Your Name:
<asp:TextBox ID="txtMsg" runat="server"></asp:TextBox>
&nbsp;
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /><br />
<br />
<br />
<asp:Label ID="lblMsg" runat="server"></asp:Label>
  • Compile the control, and then add it to the default.aspx page that came with the website upon its creation. Make sure it works, and then delete that default.aspx page.
  • Now we need to publish the usercontrol, why? In ASP.NET 2.0, every aspx page or ascx file, will be converted to its own DLL file either through precompiling or at run time. In this example, we will publish the website, which requires precompiling the website, and this generating the DLL that resembles the user control under test.
    • Go to Build --> Publish Web Site
    • Type in the Target Location, where the precompiled files shall be placed.
    • Deselect the *Allow this precompiled to be updatable*, why? In the updatable mode, only the code-beside will be precompiled and not the ascx file itself. In our case we need both to be in one DLL.
    • Select *Use fixed naming and single page assemblies*. "This will gurantee that your user control will be compiled into a single assembly that will have a name based on the ascx file itself ..."
  • Now, the user control is precompiled. Create a new website, right click the application, Add Reference, choose the DLL just created.
  • On an aspx page, ad the following below the page directive:
<%@Register TagPrefix="bhaidar" Namespace="WebUserControl" Assembly="App_Web_helloworld.ascx.cdcab7d2"%>

The Namespace is the same as the Namespace in which the UserControl was created.
The Assembly, is the name of the DLL file generated

  • Inside the page body, add the following control:
<bhaidar:helloworld id="MyUC" runat="server"/>

Although this method made your user controls become custom controls yet, the custom controls won't have any desing time view and this is really a loss!!!

Hope you enjoyed this post and found it beneficial.

Regards

 

 

Tags:

Jan 31 2006

Download Visual Studio 2005 Code Snippets

Category: ASP.NET 2.0 - GeneralBil@l @ 21:34

You can now download a set of Code Snippets to use in your C# development.

Please check this link for more information: Visual Studio 2005 Code Snippets

 

Regards

 

Tags:

Jan 22 2006

Install Application Services Database on Microsoft Sql Server 2000

Category: ASP.NET 2.0 - GeneralBil@l @ 20:01

ASP.NET 2.0 ships with a set of providers for different features. We have the profile, membership, personalization and other new features that require a set of database tables and stored procedures, which are known as Application Services Database.

Usually when you create a new web application in ASP.NET 2.0 using either Visual Studio 2005 or Visual Web Developer, those tools would automatically build the database to use in either the Microsoft Sql Server 2005 or in the Microsoft Sql Express 2005.

However, if you still want to use the new features in ASP.NET 2.0 that need the database, with Microsoft Sql Server 2000, that is possible too. There is a tool called aspnet_regsql.exe that you can use in order to install the database on Microsoft Sql Server 2000.

In this post, we will go through a step by step tutorial on how to do so.

First of all, open a new comand prompt windows, go to:

{DriveLetter}:\Windows\Microsoft.Net\Framework\{Version Number}\aspnet_regsql.exe

Once you run this, the following screen is shown in front of you:

Click Next.

You have the choice to either install a new Application Services Database or remove an existing one from one of the databases you installed before. In this case we want to install new one, so choose the first option and press Next.

You will need to put your machine name instead of *Development*, it is the name of my VPC that I develop on. You can use either Windows or Sql authentication. In case you want to use Sql Authentication, then should have previously created the username and password on the database you want to install the services databased. Finally choose the database where you to install the services related database. Click Next. 

Click Next.

Click Finish.

 

Hope this tutorial helps,

 

Regards

 

Tags:

Jan 22 2006

ListControl property : AppendDataBoundItems

Category: ASP.NET 2.0 - GeneralBil@l @ 12:30

In ASP.NET 1.1, we used to bind a ListControl or any other control inheriting from it to some source of data, after that we used to add a new item at location 0 to tell the user to choose a value, like :  -- Choose a Value --

We always had to do so AFTER binding the ListControl to its data source, else the binding will remove all previous items in the ListControl.

Today, in ASP.NET 2.0, we have the ListControl's property, called AppendDataBoundItems, once this property is set to true, this tells the ListControl NOT to clear the ListControl items before binding to data, so now we can add an item on the top of the ListControl, then simply bind the ListControl to the data source we have. an example is given beloe:

this.ddlItems.Items.Add("- Choose an Item -");
this.ddlItems.AppendDataBoundItems = true;
this.ddlItems.DataSource = CreateDataSource();
this.ddlItems.DataBind();
this.ddlItems.DataTextField = "Name";
this.ddlItems.DataValueField = "ID";

Hope that helps you

Regards

Tags:

Jan 14 2006

Unable to Start Debugging on the Server. You do not have permission to debug

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 20:46

Have you ever faced that error before?

I have started development lately on a Windows 2003 Standard Server, having both VS.NET 2003 and VS.NET 2005 installed.

I was trying to open a DotNetNuke project yesterday and got the error by VS.NET 2003:

Error while trying to run the project  : Unable to Start debugging on the web server. You do not have permissions to debug the server
Verify that you are a member of the 'Debugger Users' group on the server

I have Integrated Security on, Network Service, ASPNET, Administrator (Local Account), IUSR_MachineName accounts all added in the Debugger Users group.

Lately, I figured out after reading this document, How To Solve Debugger Problems

If you created the web project with a full machine name (like “machinename.domainname.something”), the web site is recognized as “Internet” site.
So the default setting of IE will impact on the behavior of log on. In this case, you need to enable logging on with your current user account in “Internet” area with IE setting.

But it is not the default setting of IE, so you’d be better off if you create project with only the machine name.

 

So, open the IE browser, go to --> Tools --> Internet Options --> Security --> Custom Level --> User Authentication --> Select Automatic Logon with current username/password

 

This will hopefully, fix your problem.

 

Regards

Tags: ,

Jan 12 2006

Bit Column in Microsoft SQL Server

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 07:57

In my work today, I faced a very weird thingy, here it is:

I was retreiving a bit column from the table whose default value is set to 0, and then using an SqlDataReader, and the weird thing is that I used:

1- (bool)reader[col]
2- Convert.ToBoolean(reader[col]])
3- reader.GetInt32(col)

Nothing worked but:

Convert.ToBoolean( Convert.ToByte( reader[col] ) )

Can anyone explain why that happened? I beleive, option #1 should work pretty simple.

Regards

Tags: ,

Jan 12 2006

Add NameSpaces in Web.Config

Category: ASP.NET 2.0 - GeneralBil@l @ 05:38

A new and cool feature in ASP.NET 2.0, that allows you to import default namespaces once in the Web.Config file, and then every ASPX page (Not Code-Behind) can see those imported namspaces. Here is a small code:

<?xml version="1.0"?>
<configuration>
 <system.web>
    <pages buffer="true" maintainScrollPositionOnPostBack="true">
      <namespaces>
        <add namespace ="System.Web" />
        <add namespace="System.Text"/>
      </namespaces>
    </pages>  
 </system.web>
</configuration>

Hope you liked that.

Regards

Tags:

Jan 12 2006

ASP.NET 2.0 Security New Wiki In Town !!

Category: ASP.NET 2.0 - GeneralBil@l @ 05:25

There is a new ASP.NET 2.0 Security Wiki in Town, go and check it:

http://channel9.msdn.com/wiki/default.aspx/SecurityWiki.ASPNET2SecurityFAQs

 

Regards

Tags:

Jan 12 2006

Visual Studio .NET and Class Libraries

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 04:31

When I start a new project, I usually create a Blank Solution in VS.NET, then add to it my Class Libraries and Web Application. The problem I used to have before is that, every time I did some changes in one of the Class Libraries I had to update the references manually to all other projects referencing that updated Class Libraries.

Thank to Peter Johnson for hinting for me that:

Correct way:
1. Right-click web project file, choose "Add Reference..."
2. Click "Projects" tab.
3. Select your class library project and hit OK.

Wrong way:
1. Right-click web project file, choose "Add Reference..."
2. On ".NET" or "Browse" or "Recent" tabs, choose your class library output DLL.

Hope that helps you out.

This way, whenever you change anything in your Class Libraries, then you do a Build, all the references will be updated.

Regards

 

Tags: ,

Jan 4 2006

Code Snippets in Visual Studio 2005

Category: ASP.NET 2.0 - GeneralBil@l @ 17:09

I kindly invite you to check my latest article on the www.aspalliance.com, with the title of:

Code Snippets in Visual Studio 2005

Hope you enjoy it.

Regards

Tags: