Sunday, May 16, 2010

Silverlight DataBinding with Visual Studio 2010 IDE

Silverlight 4 in ActionI discussed Silverlight Databinding in the blog Silverlight 3 / WPF Databinding. There I tweaked the raw XAML for enabling DataBinding.

The Binding class is used to define a connection between a CLR Object and an User Interface Component. The three elements of binding are Source, Binding Mode and the Target. Source is the CLR Object and Target is the Dependency Property.

Visual Studio 2010 have support for Silverlight DataBinding in the IDE.

As in the previous example here too I am placing a Slider Control with Maximum 100 and a TextBox.

In the properties window there is an Advanced Option icon next the Text in the Text Property.


















Clicking the icon another list will appear. Select Apply Data Binding.


















First you can select the ElementName ie. slider1.











Then select the Path. In our case it is the Value.













This action will automtically generate the Binding Syntax.

















Related Blogs
 

Silverlight Out of Browser Application

Pro Silverlight 4 in C#Starting with Silverlight 3 it is possible to run a Silverlight Application as Out of Browser Application.
Means you can work with yours silverlight Application as if working with a Windows Application.

For enabling this feature Select the Project Properties page by going to Project menu and Select <projectname> Properties.

Check the "Enable running application out of browser" option.

























Now by right clicking the Silverlight application in the Browser you will receive an additional option called "Install HelloSilverlight Application on this computer..".







Clicking the option enables you select the location(s) for the shortcuts.





Friday, April 30, 2010

C# 4 Optional Parameters and COM no ref

Pro .NET 4 Parallel Programming in C#COM had the concept of Optional Parameters. For example the COM TreeView control's Nodes.Add method expects six parameters. The last two parameters and Image and Selected Image.

Working with such controls from C# requires embedding a lot of Type.Missing statements which makes the code bulky as described in the latter part of my blog Using COM Controls in .Net

In that blog I have demonstrated working with COM controls from both VB .Net and C#.

With the introduction of Optional Parameters in C# 4 you can call COM Components by reducing unwanted tweaking.

COM no ref

COM Components also expect certain parameters to be of pass by ref and you had to explicitly mention the ref keyword in the previous version of C#. C# 4 relaxes the code by allowing you to omit the ref modified.

Here is the C# 4 code

private void Form1_Load(object sender, EventArgs e)
{
mscomctl.Node Nodx;
Nodx = axTreeView1.Nodes.Add(Type.Missing, Type.Missing, "India", "India");
Nodx = axTreeView1.Nodes.Add("India", mscomctl.TreeRelationshipConstants.tvwChild, "Kerala", "Kerala");
Nodx.EnsureVisible();

axTreeView1.Nodes.Add(Nodx);
}



Asp .Net MVC 2 with Visual Studio 2010 or Visual Web Developer 2010

Pro ASP.NET MVC 3 Framework, Third EditionAsp .net Mvc is the Web Development framework from Microsoft based on
the MVC architecture.

Asp .Net MVC is getting installed as a part of Visual Studio 2010 and
Visual Web Developer 2010.

Asp .Net 2 MVC Empty Web Application Project

Asp .Net MVC 2 Web Application comes with predefined
Membership and other frills which makes it easy
to get a head start, but abstracts away the inner working of MVC.

But I am taking another route. There is another
Asp .Net MVC 2 Empty Web Application Project.

Asp .Net MVC






















Lets start with Controllers. Controllers are responsible for handling
incoming request. It's a class that inherits from System.Web.Mvc.Controller.























I am naming the controller HomeController.

















I am replacing the existing code of HomeController which expects a View (which will be explained late) with a function that returns a string.
























If you run the application you will see a web page with the value of function.





















Views


Now Let's turn our attention to Views. A view is typically what you see in an Asp .Net MVC Application.

Preferably start a new new Asp .Net MVC 2 Empty Web Application so that we can start afresh.


The following diagram shows the default code within the HomeController class.































If you run the application you will receive a detailed error message stating that the view is missing.

































So right click the Index method and select Add View.



















Make sure you check off the Select MasterPage option because we haven't introduced Master Pages.
A view template called Index.aspx will created for you under ~/Views/Home/ directory.

Which comprises of plain HTML.





































Now if you run the application you will receive a blank form.


Lets replace the existing code with the code below. The code will return a
ViewData structure



public ViewResult Index()
{
ViewData["Blog"] = "ShalvinPD.blogspot.com";
return View();
}



Now lets show the ViewData in the View.
















Asp .Net MVC Views


Easiest way to develop Asp .Net MVC views are by using Html Helpers.


Name <%: Html.TextBox("Name") %>

<br />
<%: Html.CheckBox(".Net", true) %>.Net

<br />
<%: Html.RadioButton("Male", true) %>Male
<br />
<%: Html.RadioButton("Female", false) %>Female
<div>
Password <%: Html.Password("myPassword")%>
</div>
<div>
Address <%: Html.TextArea("Address", "Shalvin P D", 5, 20, null)%>
</div>

Kmug<%: Html.DropDownList("Kmug", new SelectList(new[] { "Shalvin", "Mathew" }), "Shalvin") %>

<br />
Technologies<%: Html.ListBox("myList", new SelectList(new[] { "Asp .Net", "Asp .Net Mvc" }), "Shalvin") %>

Tuesday, April 6, 2010

Asp .Net Membership -III aspnet_regsql command tool, configuring the Site to using Instance Database and Working with Membership and Roles Api

Membership and Role Api Methods


In the blog Asp .Net Membership - II aspnet_regsql to port Membership API database to Sql Server we saw aspnet_regsql tool.


The tool can also be invoked from command prompt, which provides fine grained support on which tables you want to dump to sql server Instance database.

If you want to dump just Membership and Role tables to Sql Server you issue the command as follows:

>aspnet_regsql -S . -E -d Shalvin -A mr


Here -S denoted the Sql Serve instance. Since I am using Sql server 2000 I am specifying a dot. -E denoted integrated security. Optionally you can specify user name and password if you are using Sql Server Security. -d denoted the database name. -A is used for adding the tables. Here I am adding Membership and Role tables.
Like wise you can remove tables using -R.

Next order of business it the instruct your Asp .Net project to use Instance database instead of File System Database.

For that you have edit the web.config as follows:









<configuration>
<connectionStrings>
<add name="ShalvinConnectionString" connectionString="Data Source=.\shalvin;Initial Catalog=Shalvin;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>

<roleManager enabled="true">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ShalvinConnectionString"/>
</providers>
</roleManager>

<authentication mode="Forms"/>

<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ShalvinConnectionString"/>
</providers>
</membership>
</system.web>
</configuration>



Programmatic working with Membership and Roles Api

using System.Web.Security;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlUsers.DataSource = Membership.GetAllUsers();
ddlRole.DataSource = Roles.GetAllRoles();

DataBind();
}
}
protected void btnAddUserToRole_Click(object sender, EventArgs e)
{
try
{
Roles.AddUserToRole(ddlUsers.Text, ddlRole.Text);
Response.Write("User Added to role");
}
catch (Exception ex)
{
Response.Write(String.Format("{0} is already an {1}",
ddlUsers.Text, ddlRole.Text));
}
}






Related Blog
Asp .Net 2.0 Membership I - LoginStatus, Login and LoginView Controls
Asp .Net Membership - II aspnet_regsql to port Membership API database to Sql Server

Monday, March 22, 2010

Silverlight Dynamically Creating Controls and Attaching Event Handlers

Pro Silverlight 4 in C#Dynamically creating controls in Silverlight is a breeze.
I am using Visual Studio 2010 Release Candidate.
I am starting a Silverlight Application Project.














I am selecting Asp .Net Web Project as new Web Project Typer.
















As mentioned in the previous blog VS 2010 comes with excellent tool support for Silverlight.

I am adding a Stack Panel (stp1)to hold the dynamically created controls and a TextBlock to display the message based on the control selected.















Going to the load event of Main Page I am dynamically creating Buttons.


List<string> lstr = new List<string> { "Shalvin", "Praseed Pai" };

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Button btn;
foreach (string str in lstr)
{
btn = new Button();
btn.Content = str;
btn.Tag = str;
btn.Click += new RoutedEventHandler(btn_Click);
stp1.Children.Add(btn);
}
}

Notice I have only one event Handler for the whole set of controls.
I am going to have different functionlity for different buttons by identifying the tag property of Button.

private void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
string txtTag = btn.Tag.ToString();
tblMessage.Text = txtTag;
}









Monday, February 22, 2010

Asp .Net 4 Empty Web Site and Clean Web.Config

One thing I hate about previous Web.Config in previous versions of Asp .Net is its sheer size.

Asp .Net 4 has done a wonderful job in reducing the size of Web.Config by offloading the job to Machine.Config.

Inorder to take advantage of this you should start a Empty Web Site.














Tuesday, February 2, 2010

Asp .Net Membership - II aspnet_regsql to port Membership API database to Sql Server

In the blog Asp .Net Membership I - LoginStatus, Login and LoginView Controls
we saw how to work with Asp .Net Membership service.

By default the Membership Provider is creating a File System Database called AspNetDb.mdf under App_Data folder. This will suffice for small projects, where you can create rest of the tables in AspNetDb.mdf.
This by no means is an elegant solution, since usually the database will reside on Sql Server as Instance database and not in File System database. In effect there will be two database one for membership, role, profile, personalization, etc. and another for the application.

It is possible to merge the membeership database with out application database.

For that you can use aspnet_regql tool found in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 folder incase. Incase you are using Asp .net 4 the folder will be C:\WINDOWS\Microsoft.NET\Framework\v4.0.30128.

The wizard will prompt for the database to which you want to dump the membership, role, profile and personalization tables and associated database objects.

Related Blog

Asp .Net 2.0 Membership I - LoginStatus, Login and LoginView Controls