Dynamic is magic : A solution to use the ASP.NET profile in web applications.

Profiles are not auto generated with Web Applications, so you can’t just add them in the web.config and use them directly. (unless …)

The WebSite project do a magic thing : when you add properties to the profile in the web.config file, a class is autogenerated and allow the developer to access these properties. The web application project don’t do that.

You can find some workarounds over the web, here is some

The “dynamic” keyword help us here.

So, no more chit chat, here is the code snippets for a profile with 3 parameters : LastName, FirstName and GSM

web.config:


<profile enabled="true">
 <providers>
 <clear/>
 <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="MyApp"/>
 </providers>
 <properties>
 <add name="FirstName" type="string"/>
 <add name="LastName" type="string"/>
 <add name="GSM" type="string"/>
 </properties>
 </profile>

A “business” class, I called it “ProfilePresenter”, it returns the profile of the logged user and allow modifications:


public class ProfilePresenter
{
 public dynamic GetProfile()
 {
 return HttpContext.Current.Profile;
 }
 public dynamic UpdateProfile(string FirstName, string LastName, string GSM)
 {
 HttpContext.Current.Profile.SetPropertyValue("FirstName", FirstName);
 HttpContext.Current.Profile.SetPropertyValue("LastName", LastName);
 HttpContext.Current.Profile.SetPropertyValue("GSM", GSM);
 HttpContext.Current.Profile.Save();
 return HttpContext.Current.Profile;
 }
}

A page that uses the ProfilePresenter class : displaying the profile properties, and edit them

ObjectDataSource:


<asp:ObjectDataSource runat="server" ID="ProfileODS" TypeName="Board.Presenters.ProfilePresenter"
 SelectMethod="GetProfile" UpdateMethod="UpdateProfile"/>
<pre>

FormView: Display part

 <asp:FormView runat="server" ID="ProfileFV" DataSourceID="ProfileODS" RenderOuterTable="false">
 <EmptyDataTemplate>
 <p>
 No profil datas
 </p>
 </EmptyDataTemplate>
 <ItemTemplate>
 <h2>
 Profil informations</h2>
 <div>
 <asp:Button runat="server" ID="Edit" CommandName="Edit" Text="Edit" /></div>
 <table>
 <thead>
 <tr>
 <th colspan="2">
 Infos
 </th>
 </tr>
 </thead>
 <tbody>
 <tr>
 <th>
 LastName
 </th>
 <td>
 <%# Eval("LastName")%>
 </td>
 </tr>
 <tr>
 <th>
 FirstName
 </th>
 <td>
 <%# Eval("FirstName")%>
 </td>
 </tr>
 <tr>
 <th>
 GSM
 </th>
 <td>
 <%# Eval("GSM")%>
 </td>
 </tr>
 </tbody>
 </table>
 </ItemTemplate>

FormView : the Edit part

 <EditItemTemplate>
 <h2>
 Profil Update</h2>
 <div>
 <asp:Button runat="server" ID="Edit" CommandName="Cancel" Text="Annuler" />
 <asp:Button runat="server" ID="Button1" CommandName="Update" Text="Sauver" /></div>
 <table>
 <thead>
 <tr>
 <th colspan="2">
Infos
 </th>
 </tr>
 </thead>
 <tbody>
 <tr>
 <th>
 LastName
 </th>
 <td>
 <asp:TextBox runat="server" ID="LastName" Text='<%# Bind("LastName")%>' />
 </td>
 </tr>
 <tr>
 <th>
 FirstName
 </th>
 <td>
 <asp:TextBox runat="server" ID="FirstName" Text='<%# Bind("FirstName")%>' />
 </td>
 </tr>
 <tr>
 <th>
 GSM
 </th>
 <td>
 <asp:TextBox runat="server" ID="GSM" Text='<%# Bind("GSM")%>' />
 </td>
 </tr>
 </tbody>
 </table>
 </EditItemTemplate>
 </asp:FormView>

A simple use :


dynamic profil = HttpContext.Current.Profile;
if (profil != null)
{
 Console.Writeline(profil.GSM);
}

Another example : retrieve the gsm numbers from  members of a security role. It shows how to retrieve the profile of an other user


string[] users = Roles.GetUsersInRole(role);

List<string> gsm = new List<string>();
foreach (string user in users)
{
 dynamic profil = HttpContext.Current.Profile;
 dynamic profi = profil.GetProfile(user);
 if (profi != null)
 {
 if (!string.IsNullOrEmpty(profi.GSM))
 {
 gsm.Add(profi.GSM);
 }
 }
}

Let me know what you think !

ASP.NET WebForms Datacontrols : Dropdownlist and detailview connected.

Links for this article :

How to link a dropdownlist and a detailview in both ways ?

Ie: I want my dropdownlist to be populated with a list of vendors, and if I select one of them I want to see the details of the vendor in the detailview.
Plus, if I add/delete/update a vendor in the detailview, I want the dropdownlist to be updated.

This kind of questions is often asked on different forums, here is my solution:

Here are the steps to follow.

Step 1 : getting the datas

First we need a datasource, so for the demo we will create a simple vendor class in our DAL.

The vendor class has three properties : ID, Name and WebSite and one static method : “All” who allows us to retrieve a list of vendors.

Step 2 : managing the datas

We also need a manager class for these vendors, which will be in our BLL.

VendorsManager will contain the simple crud methods (Get, Insert, Update, Delete) and a “GetAll” method which simply call the DAL.Vendors’s “All” Method

Step 3 : displaying the data

Now that we have our datasource methods ready, we can create a webform named “DropDownDetailView.aspx”

The solution has to be like that:

In the aspx markup we’ll add an objectdatasource which will be used by the dropdownlist.
This objectdatasource will use the GetAll method from our BLL.


<asp:ObjectDataSource runat="server" ID="VendorsDataSource"
TypeName="DataControls.BLL.VendorsManager"
SelectMethod="GetAll"/>

Then we add the dropdownlist and configure it to use the objectdatasource.


<asp:DropDownList runat="server" ID="VendorsList"
DataSourceID="VendorsDataSource"
DataValueField="ID" DataTextField="Name" />

If you run the application you will have a dropdownlist populated with our vendors

Now we add a second objectdatasource with the same TypeName, and we set the SelectMethod, UpdateMethod, DeleteMethod and InsertMethod to use the methods of the VendorsManager’s methods.


<asp:ObjectDataSource runat="server" ID="VendorDetailDataSource"
TypeName="DataControls.BLL.VendorsManager"
SelectMethod="Get"
UpdateMethod="Update"
DeleteMethod="Delete"
InsertMethod="Insert"/>

And because the GetMethod takes a parameter, we will say to the objectdatasource to take this parameter from the Selected value of the dropdownlist.


<SelectParameters>
<asp:ControlParameter Name="id" ControlID="VendorsList" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>

So the complete objectdatasource will be


<asp:ObjectDataSource runat="server" ID="VendorDetailDataSource"
TypeName="DataControls.BLL.VendorsManager"
SelectMethod="Get"
UpdateMethod="Update"
DeleteMethod="Delete"
InsertMethod="Insert">
<SelectParameters>
<asp:ControlParameter Name="id" ControlID="VendorsList" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

Now that we have our datasource, we can link it to a detailview.
So we create the detailview and tell him to use the datasource


<asp:DetailsView runat="server" ID="VendorDetail"
DataSourceID="VendorDetailDataSource"  />

If we run the application, we see that the detailview is filled with the details of the first vendor

Try to select another vendor in the list -> nothing happens!
It’s because we don’t cause a postback to the server to let him change the data.

To achieve that, we simply set the AutoPostBack property of the dropdownlist to true, so the dropdownlist is


<asp:DropDownList runat="server" ID="VendorsList"
DataSourceID="VendorsDataSource"
DataValueField="ID" DataTextField="Name"
AutoPostBack="true"/>

Re-run the application and change the selected value of the dropdownlist.


Now the data in the detailview matches the selected vendor.

The first part of our need is achieved!

Step 4 : playing with the data

Now we want to be able to add, edit or delete vendors, so we set the detailview’s propertie AutoGenerateDeleteButton,  AutoGenerateEditButton and  AutoGenerateInsertButton to true.

And we don’t forget to set the DataKeyNames property to “ID”, because ID is the key for our vendors.

So now, the detailview looks like this:


<asp:DetailsView runat="server" ID="VendorDetail"
DataSourceID="VendorDetailDataSource"
DataKeyNames="ID"
AutoGenerateDeleteButton="true"
AutoGenerateEditButton="true"
AutoGenerateInsertButton="true"/>

Run the application and try to add a new item

Now that the new vendor is inserted, look at the values in the dropdownlist

Arghh, the new vendor is not in the list !

It’s normal! We don’t refresh the dropdownlist yet!
To do that, we’ll add handlers for deleted, inserted and updated events.

The aspx markup of the detailview becomes


<asp:DetailsView runat="server" ID="VendorDetail"
DataSourceID="VendorDetailDataSource"
DataKeyNames="ID"
AutoGenerateDeleteButton="true"
AutoGenerateEditButton="true"
AutoGenerateInsertButton="true"
onitemdeleted="VendorDetail_ItemDeleted"
oniteminserted="VendorDetail_ItemInserted"
onitemupdated="VendorDetail_ItemUpdated"/>

and in the code-behind of the page :


protected void VendorDetail_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
{
Refresh();
}

protected void VendorDetail_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
Refresh();
}

protected void VendorDetail_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
Refresh();
}

In each handler, I call  the Refresh() method , and in this method we will rebind the dropdownlist:


private void Refresh()
{
this.VendorsList.DataBind();
}

Now, run the application, and redo the same test:


Now look at the dropdownlist:

And that’s it! The new vendor is in the list

Now, all the steps are processed!

Here is the complete final aspx markup


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DropDownDetailView.aspx.cs" Inherits="DataControls.DropDownDetailView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>Dropdownlist and detailview demo</title>
</head>
<body>
 <form id="form1" runat="server">
 <asp:ObjectDataSource runat="server" ID="VendorsDataSource"
 TypeName="DataControls.BLL.VendorsManager"
 SelectMethod="GetAll"/>
 <asp:DropDownList runat="server" ID="VendorsList"
 DataSourceID="VendorsDataSource"
 DataValueField="ID" DataTextField="Name"
 AutoPostBack="true"/>

 <asp:ObjectDataSource runat="server" ID="VendorDetailDataSource"
 TypeName="DataControls.BLL.VendorsManager"
 SelectMethod="Get"
 UpdateMethod="Update"
 DeleteMethod="Delete"
 InsertMethod="Insert">
 <SelectParameters>
 <asp:ControlParameter Name="id" ControlID="VendorsList" PropertyName="SelectedValue" Type="Int32" />
 </SelectParameters>
 </asp:ObjectDataSource>
 <asp:DetailsView runat="server" ID="VendorDetail"
 DataSourceID="VendorDetailDataSource"
 DataKeyNames="ID"
 AutoGenerateDeleteButton="true"
 AutoGenerateEditButton="true"
 AutoGenerateInsertButton="true"
 onitemdeleted="VendorDetail_ItemDeleted"
 oniteminserted="VendorDetail_ItemInserted"
 onitemupdated="VendorDetail_ItemUpdated"/>
 </form>
</body>
</html>

And the matching code behind


using System.Web.UI.WebControls;

namespace DataControls
{
 public partial class DropDownDetailView : System.Web.UI.Page
 {
 protected void VendorDetail_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
 {
 Refresh();
 }

 protected void VendorDetail_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
 {
 Refresh();
 }

 protected void VendorDetail_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
 {
 Refresh();
 }

 private void Refresh()
 {
 this.VendorsList.DataBind();
 }
 }
}

Links for this article :

Incoming search terms:

  • link dropdownlist with detailsView
  • asp datacontrols
  • asp net search drop down list parameter
  • asp:detailsview change selected data
  • datacontrols asp
  • sharepoint 2010 typename objectdatasource selectparameters

Free beta ebook and samples : ASP.NET Web Pages with Razor Syntax

ASP.NET Web Pages with Razor Syntax is a web framework and part of WebMatrix which gives you everything you need to build Web sites using Windows.

A free  ebook (it’s a draft) and the samples are availables on the Microsoft Download Center.

Here is the table of contents :

Chapter 1 – Getting Started with WebMatrix Beta and ASPNET Web Pages 1

What is WebMatrix Beta? 1
Installing WebMatrix Beta 1
Getting Started with WebMatrix Beta 2
Creating a Web Page 4
Installing Helpers with Package Manager 7
Using ASPNET Web Pages Code 9
Programming ASPNET Razor Pages in Visual Studio 12
Creating and Testing ASPNET Pages Using Your Own Text Editor 13

Chapter 2 – Introduction to ASPNET Web Programming Using the Razor Syntax 15

The Top 8 Programming Tips 15
HTML Encoding 16
HTTP GET and POST Methods and the IsPost Property 21
A Simple Code Example 22
Basic Programming Concepts 24
Classes and Instances 25
Language and Syntax 26
Additional Resources 47

Chapter 3 – Creating a Consistent Look 48

Creating Reusable Blocks of Content 48
Creating a Consistent Look Using Layout Pages 51
Designing Layout Pages That Have Multiple Content Sections 55
Making Content Sections Optional 58
Passing Data to Layout Pages 59
Creating and Using a Basic Helper 65

Chapter 4 – Working with Forms 67

Creating a Simple HTML Form 67
Reading User Input From the Form 68
HTML Encoding for Appearance and Security 70
Validating User Input 71
Restoring Form Values After Postbacks 73
Additional Resources 74

Chapter 5 – Working with Data 75

Introduction to Databases 75
Relational Databases 76
Creating a Database 76
Adding Data to the Database 78
Displaying Data from a Database 79
Structured Query Language (SQL) 81
Inserting Data in a Database 82
Updating Data in a Database 86
Deleting Data in a Database 91
Connecting to a Database 95
Additional Resources 96

Chapter 6: Displaying Data in a Grid 97

The WebGrid Helper 97
Displaying Data Using the WebGrid Helper 97
Specifying and Formatting Columns to Display 99
Styling the Grid as a Whole 102
Paging Through Data 103

Chapter 7 – Displaying Data in a Chart 106

The Chart Helper 106
Chart Elements 107
Creating a Chart from Data 108
“Using” Statements and Fully Qualified Names 114
Displaying Charts Inside a Web Page 115
Styling a Chart 116
Saving a Chart 117
Additional Resources 123

Chapter 8 – Working with Files 124

Creating a Text File and Writing Data to It 124
Appending Data to an Existing File 127
Reading and Displaying Data from a File 128
Displaying Data from a Microsoft Excel Comma-Delimited File 131
Deleting Files 131
Letting Users Upload a File 133
Letting Users Upload Multiple Files 136
Additional Resources 138

Chapter 9 – Working with Images 139

Adding an Image to a Web Page Dynamically 139
Uploading an Image 142
About GUIDs 144
Resizing an Image 144
Rotating and Flipping an Image 147
Adding a Watermark to an Image 148
Using an Image As a Watermark 149

Chapter 10 – Working with Video 152

Choosing a Video Player 152
MIME Types 153
Playing Flash (swf) Videos 153
Playing MediaPlayer (wmv) Videos 156
Playing Silverlight Videos 158
Additional Resources 159

Chapter 11 – Adding Email to Your Website 160

Sending Email Messages from Your Website 160
Sending a File Using Email 164
Additional Resources 166

Chapter 12 – Adding Search to Your Website 167

Searching from Your Website 167
Additional Resources 170

Chapter 13 – Adding Social Networking to Your Web Site 171

Linking Your Website on Social Networking Sites 171
Adding a Twitter Feed 172
Rendering a Gravatar Image 174
Displaying an Xbox Gamer Card 175
Displaying a Facebook “Like” Button 176

Chapter 14 – Analyzing Traffic 179

Tracking Visitor Information (Analytics) 179

Chapter 15 – Caching to Improve the Performance of Your Website 183

Caching to Improve Website Responsiveness 183

Chapter 16 – Adding Security and Membership 186

Introduction to Website Membership 186
Creating a Website That Has Registration and Login Pages 187
Creating a Members-Only Page 191
Creating Security for Groups of Users (Roles) 192
Creating a Password-Change Page 194
Letting Users Generate a New Password 196
Preventing Automated Programs from Joining Your Website 200

Chapter 17 – Introduction to Debugging 203

Using the ServerInfo Helper to Display Server Information 203
Embedding Output Expressions to Display Page Values 205
Using the ObjectInfo Helper to Display Object Values 209
Using Debugging Tools 210
Additional Resources 213

Chapter 18 – Customizing Site-Wide Behavior 214

Adding Website Startup Code 214
Running Code Before and After Files in a Folder 218
Creating More Readable and Searchable URLs 224

Appendix – ASPNET Quick API Reference 227

Classes 227
Data 233
Helpers 234

Appendix – ASPNET Web Pages Visual Basic 241

The Top 8 Programming Tips 241
HTML Encoding 242
A Simple Code Example 248
Visual Basic Language and Syntax 250
Additional Resources 271

Appendix – Programming ASPNET Web Pages in Visual Studio 272

Why Use Visual Studio? 272
Installing the ASPNET Razor Tools 272
Using the ASPNET Razor Tools for Visual Studio 273

Disclaimer 278

Previous versions of the draft are also available.

For the latest informations and more about webmatrix and the Razor syntax, visit http://www.asp.net/webmatrix/

Incoming search terms:

  • ebook razor database
  • razor syntax ebook