-->

Monday, October 7, 2013

Mode property of Literal control in ASP.NET

Introduction 

A literal control contains three enumeration value such as PassThrough , Encode and Transform. Using these enumeration we can obtains or sets an enumeration value that specifies how the content in the Literal control is rendered (According to msdn).

Enumeration values of the Mode properties are:

PassThrough :  If you specify PassThrough in mode property, all contents of the Text property are passed to the browser without making any modifications. For example.

<asp:Literal ID="Literal1" runat="server" Mode ="PassThrough" Text ="<h1>Hello world</h1>" ></asp:Literal>

Output

ASP.NET :PassThrough enumeration in mode property




Encode : If you specify Encode in mode property, the contents for the Text property are converted into an HTML-encoded string before rendering. For example, if the Text property of a Literal control contains an <H1> tag, it is converted to &lt;H1&gt; and sent to the  browser. (according to msdn article)

<asp:Literal Mode ="Encode" ID="Literal1" runat="server"  Text ="<h1>Hello world</h1>" ></asp:Literal>

ASP.NET: Encode enumeration in mode property of literal control




Transform : If you specify Transform, the rendering behavior of the Text property depends on the type of markup being rendered. When the Literal control is rendered for a device or browser that supports HTML or XHTML, specifying Transform produces the same behavior as specifying PassThrough.



  <asp:Literal Mode="Transform" ID="Literal1" runat="server"  Text ="<h1>Hello world</h1>" ></asp:Literal>


ASP.NET :Transform enumeration in mode property

How to enable tracing in MVC

Introduction

Tracing is way to show diagnostic information about a single request for an ASP.NET Page. According to msdn article , ASP.NET tracing enables you to follow a page's execution path, display diagnostic information at run time, and debug your application. (http://msdn.microsoft.com/en-us/library/bb386420.ASPX )

Steps to follow how to enable tracing in MVC

Step-1: Open Web.config file
Step-2:  Enable tracing so add <trace> tag  which is inside in  <System.Web>

<system.web>
    <trace enabled ="true" pageOutput ="false"/>

</ system.web>

Step-3:  IgnoreRoute("{resource}.axd/{*pathInfo}") method create a trace file with .axd extension(This method is available in RouteConfig.cs file)

Step-4: Run your application
Step-5: Open trace.axd file in url

How to enable tracing in MVC




Sunday, October 6, 2013

How to use QueryString in MVC

Introduction


If you want to pass control or variable value from one form to another form then we can use QueryString. ASP.NET MVC will automatically pass any query string or form post parameter name “name” to Index action method when its invoked. You have to read about HomeController from my previous post i.e.  how to create HomeController class in MVC Because in MVC  id is an optional parameter in RegisterRoutes method.



public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }


This code is available in RouteConfig.cs file which resides in App_Start folder. If you want to pass queryString in URL then you should pass that in Index method.

public String Index(string id, string name)

Lets take a simple example to use QueryString parameter.

Step-1: Create a string parameter in Index method in homeController class

  public class HomeController : Controller
    {
        //
        // GET: /Home/


        public String Index(string id)
        {
            return "id=" + id;
        }
     
    }
Run your application and pass parameter in url such as
Now your output will come

How to use QueryString in MVC


Now again create another parameter to Index method

   public String Index(string id,string name)
        {
            return "id=" + id + "and name=" + name;
        }

Run your application with some parameter such as
Now your output will come.

How to use QueryString in MVC

Eample of QueryString in ASP.NET

Introduction

If we want to pass control or variable value from one form to another form then we can use QueryString . There are many options available to pass queryString , these are.

In Hyperlink:

    <a href=”~/Default.aspx?id=10&name=Jacob”>QueryString Example</a>

In Response.Redirect method :

 Response.Redirect(“~/Default.aspx?id=10&name=Jacob”);

If we want to get queryString Parameter valuefrom url then we should use Request.QueryString  Object.
Label1.Text=Request.QueryString[“querystring parameter”].ToString();

Lets take an simple example to pass textbox value from one form to another form using QueryString.




<%@ Page Language="C#" %>

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

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default2.aspx?name=" + TextBox1.Text);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.Request.QueryString["name"] != null)
        {
            Label1.Text = "name of the querystring parameter is " + Page.Request.QueryString["name"];
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Enter Name :
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="QueryString" OnClick="Button1_Click" /><br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>

</html>
Output
Eample of QueryString in ASP.NET

Saturday, October 5, 2013

How to set Default action method in MVC.

Introduction

In MVC index method is the default method in application route file. Means you can say Index is the first running action method. Here this example show how to change default method means
Index to another method.

Follow some steps for changing Default method

Step-1: Create a new action method in the Home controller class
Like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication3.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        public string detail()
        {
            return "hello";
        }

    }
}
This example take two method first one is Index ( Default method) and another one is detail method . If you want to set default method is detail then you would go RouteConfig.cs file.
Step-2:  Open RouteConfig.cs file which is inside in App_Start folder.
Step-3:  Change action value in RouteConfig.cs file
public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "detail", id = UrlParameter.Optional }
            );
        }
Step-4: Assign second action method name (detail) to action value in RegisterRoutes method (which is mentioned above).

Step-5 : Save your Application and run.
How to set Default action method in MVC.

Friday, October 4, 2013

Difference between Webform application and MVC application.

MVC Application
WebForm Application
In MVC URL’s are mapped to controller Action methods.

Example:

MVC Output mapped with code file



In Above snap you can see a url’s mapped to  controller function(Index,another) , it’s not related to physical files .



In a WebForm url’s are mapped to physical files

Example :




In Above snap you can see url mapped to Home.aspx files (physical file). Its not related to function.

How to Edit Columns in SQL Server Binding: Windows Froms

When we bind the data grid view with SQL Server database, then it will show all the columns created in the table of database. According to our previous post we have displayed all the records of table, without writing a single line of code.

Now in previous example all the columns are shown even the primary key of table. In this article we will remove those columns by some mouse clicks.

A datagridview tasks pop-up menu appears automatically, as shown in below image, click on Edit Columns link button.

How to Edit Columns in SQL Server Binding: Windows Froms


By clicking on this link button, some column names are shown in left side and on the right side there are some properties of that particular selected column.

How to Edit Columns in SQL Server Binding: Windows Froms


To remove a column, just select that and click on remove button (just below the selected columns panel), and it will be removed. We can easily change the header text of the particular column. Select a column and on the right side edit the value of Header Text.

How to Edit Columns in SQL Server Binding: Windows Froms


Perform the editing you want and at the last, click on ok button. All the settings you changed has been saved and when you run the project only three columns Code, Description and Type will be shown like:

How to Edit Columns in SQL Server Binding: Windows Froms

© Copyright 2013 Computer Programming | All Right Reserved