4 posts tagged “asp.net”
Overview
Silverlight 2 Beta brings the wonders of Silverlight and removes the annoyance of Javascript. The question remains: How do I get Silverlight deployed on my webserver even though it isn't out yet? This article will walk through creating a simple "hello world" silverlight application and deploying it on your webserver. This article assumes you have Silverlight 2 installed with the Visual Studio 2008 templates.
Create the Silverlight project
- Open Visual Studio 2008
- Click File->New Project
- Under Visual C# click Silverlight
- Select Silverlight Application, name it what you want, and click ok
- When it asks you if you would like to create a web project, click add a new and click ok
Now you should have two projects. Under the library, click and open up your Page.xaml file. You are going to add xaml to the Grid control so that it looks like this:
<UserControl x:Class="SilverlightDeploy.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Button x:Name="BtnHelloWorld" Content="Hello World"
Width="100" Height="50" Click="BtnHelloWorld_Click" />
</Grid>
</UserControl>
Then in the Page.xaml.cs file, you need to add the code for the button's Click event:
Now you can build the application, but it is not quite publish ready.
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void BtnHelloWorld_Click(object sender,
RoutedEventArgs e)
{
this.BtnHelloWorld.Content = "You Clicked Me!";
}
}
Publishing the application
First thing we need to do is create a publish version of the application.
- Right click on the web application and click "Publish Website"
- Specify your publish location (I used the desktop), and click ok
Now the issue with publishing Silverlight 2 is that webservers don't recognize the xap file as a proper mime type. The good thing is xap files are essentially just zip files. With this in mind, we can use some wizard trickery to make it all work happily.
- Go to the folder where you published your web application
- There should be a .xap file in your ClientBin directory. Change the file extension file to .zip instead of .xap
- Go to any pages that reference this file (Visual Studio will have created a [ProjectName]TestPage.aspx and .html file.
- Open the SilverlightDeployTestPage.html and find the object section.
- Rename the .xap reference to .zip as done in the ClientBin file
<object data="data:application/x-silverlight,"
type="application/x-silverlight-2-b1"
width="100%" height="100%">
<param name="source" value="ClientBin/SilverlightDeploy.zip"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<a href="http://go.microsoft.com/fwlink/?LinkID=108182"
style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181"
alt="Get Microsoft Silverlight"
style="border-style: none"/>
</a>
</object>
You should be good to go. You can now ftp the site wherever your heart desires and it will work. For now, you have to do this for all of the references to your xap files. I'll keep you posted (No pun intended).
Overview
This is the first of a couple articles on managing web parts. I would like to keep these articles as simple and concise as possible. This tutorial simply covers how to place WebParts onto the screen. WebParts are a portion of Asp.Net that doesn’t receive very much love. From my experience they feel complex, and where to apply them in your application very ambiguous. Let’s just start with creating one.
Creating your WebPart
- Open Visual Studio (I am using 2008), and add a new website
- This will create the Default.aspx page. Open the Default.aspx page
- Create three Divs
- Drag a WebPartManager and a DropDownList in the first
- Drag a WebPartZone in the second
- Drag a WebPartZone in the third
<div>
<asp:WebPartManager ID="WebPartManager1" runat="server">
</asp:WebPartManager>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</div>
<div>
<asp:WebPartZone ID="WebPartZone1" runat="server">
</asp:WebPartZone>
</div>
<div>
<asp:WebPartZone ID="WebPartZone2" runat="server">
</asp:WebPartZone>
</div>
- In the design view, place something in each webpartzone (like a calendar, button, or wizard control)
<div>
<asp:WebPartZone ID="WebPartZone1" runat="server">
<ZoneTemplate>
<asp:Calendar ID="Calendar1" runat="server" />
</ZoneTemplate>
</asp:WebPartZone>
</div>
<div>
<asp:WebPartZone ID="WebPartZone2" runat="server">
<ZoneTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" />
</ZoneTemplate>
</asp:WebPartZone>
</div>
- Set your dropdown list to AutoPostBack and then double click it to create an event handler
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged1">
</asp:DropDownList>
- Switch to your code-behind
- Change your code to this
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Set up the list of options
foreach (WebPartDisplayMode mode in this.WebPartManager1.SupportedDisplayModes)
// Add an item for each mode
this.DropDownList1.Items.Add(
new ListItem(mode.Name, mode.Name));
}
this.WebPartManager1.DisplayMode =
this.WebPartManager1.SupportedDisplayModes[
this.DropDownList1.SelectedValue];
}
protected void DropDownList1_SelectedIndexChanged1(
object sender, EventArgs e)
{
this.WebPartManager1.DisplayMode =
this.WebPartManager1.SupportedDisplayModes[
this.DropDownList1.SelectedValue];
}
Explaination
The first time the page loads, the dropdown will populate with the different display modes of the WebPartManager. The WebPartManager handles all of the cool drag and drop functionality. With every page load, the WebPartManager is set to the display mode specified by the dropdown. The eventhandler just changes the display mode of the WebPartManager.
If you run this code in IE, you will see your dropdown with options. If you select the Design options, you can move the controls inside the WebPartZone to another WebPartZone. In Firefox, this functionality isn’t there, but we will enable it in the next article.
Conclusion
This is the simple hello world application with WebParts. In the near future, we will cover more advanced topics in WebParts as well as how and where to integrate them into your application.
I noticed that when I used the CollapsiblePanelExtender, it had a tendancy to flicker once when the page loads. Here is my quick fix for that. All you have to do is add these properties to the panel's definition:
style="overflow:hidden" Height="0"
I hope this helps.
Overview
I was originally going to just post the information needed for my
reference, but I decided that I had to reference many things all the
time, so I will just post a tutorial. For those people using ASP.NET
often on a large amount of projects have no doubt-ably come across
ASP.NET 2.0's custom roles and membership tools. These tools allow you
to use a lot of prefabricated tools written by the Microsoft developers
in order to perform menial tasks like authentication and role
assignment. The problem is that this system is so flexible, it is hard
to get a lot of documentation about using all of the features, and
searchers are usually pointed to specific information. This tutorial is
going to be short and sweet and demonstrate what I feel would be the
most common and useful usage for the membership and role system.
Our task: A custom authentication system implementing users and roles.
Our technologies: ASP.NET 2.0 written in C# (Visual Studio) using a SqlServer 2005 database.
Setting up the database
Origonally, I was under the assumtion that unless I wrote my own custom
membership and role classes, I would be required to use the ASPNETDB on
SqlExpress (I later found out that a lot of people were under the same
assumption). Well I have no intention of writing unnessissary code, and
I found out that there is a tool that will set up a remote database
according to the specifications of Microsoft's default membership and
role providers. so...
- Run the Visual Studio Command Prompt
- Type aspnet_regsql and press enter
- Go through the wizard provided to set up your database
This will set up your database with all of the stored procedures and tables required. Very fast eh? Onward...
Setting up the website
- Create a new ASP.NET website or open up the existing site.
- Create a new or open up your web.config files
Use the following in your web.config under the <configuration> section
<connectionStrings>
<add name="TestConnection"
connectionString="your connection string here"/>
</connectionStrings>
This section is the connection string for your SqlServer2005 server that you previously configured. It will be used by our membership and roles declarations below. Add the following too your web.config file under the <system.web> section
<roleManager enabled="true" defaultProvider="MyTestRoleProvider">
<providers>
<clear />
<add connectionStringName="TestConnection"
applicationName="/ApplicationName"
name="MyTestRoleProvider"
type="System.Web.Security.SqlRoleProvider" />
</providers>
</roleManager>
<membership defaultProvider="MyTestMembershipProvider"
userIsOnlineTimeWindow="20"
hashAlgorithmType="MD5">
<providers>
<clear />
<add name="MyTestMembershipProvider"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
applicationName="/ApplicationName"
requiresUniqueEmail="true"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
connectionStringName="TestConnection"
type="System.Web.Security.SqlMembershipProvider" />
</providers>
</membership>
<authentication mode="Forms" />
A little explaination of the above. This section configures your asp.net to handle your new Sql connection with custom membership and roles. The <roleManager> section creates a custom role provider that will be used by asp.net to handle... well roles. The <membership> section defines a custom membership provider that asp.net will use to manage your users. The settings within the TestMembership declaration should be fairly straight forward to control your application as much as possible. That last little bit, just lets ASP.net know that you are using custom authentication.
Now you can use all of the authentication controls within asp.net using SqlServer2005 as well as the nifty ASP.net Web Site Administration Tool.
Happy Coding.