-->

Tuesday, October 8, 2013

How to bind DropDownlist using Xml file in ASP.NET

XML File Introduction

XML file is a text file. basically XML file is used for carry data.
Features of XML file
1. Its a Case sensitive language.
2. Use User defined tags
3. Used for communication purpose
4. Its not a presentation language .
5. Same as HTML language.
6. Use Tree based architecture

The DropDownList control displays the list of data as a drop-down list from which you can make a single selection. The DropDownList control exists within the System.Web.UI.WebControls namespace. You cannot select multiple items in this control because when you make a selection from the list, the list closes automatically.
The DropDownList control has no non-inherited methods or events. This class inherited the ListControl class.

Public Properties of DropDownList Class

SelectedIndex : Obtains or sets the index of the selected item in the control.

Application of DropDownList Control


  • In Registration page where you can select your country in given DropDownList.
  • In management project where you can select single option in given options.

Lets take an simple example to bind DropdownList 

Step-1. Create a XML file with <Countries> tag.

<?xml version="1.0" encoding="utf-8" ?>
<Countries>
  <country>
    <countryId>101</countryId>
    <countryName>USA</countryName>
   
  </country>
  <country>
    <countryId>102</countryId>
    <countryName>India</countryName>

  </country>

  <country>
    <countryId>103</countryId>
    <countryName>UK</countryName>

  </country
 
 
</Countries>


Step-2 : Drag one DropdownList from ToolBox and Drop to design window.
Step-3 : Create a DataSet instance 
Step-4 : Read XML file by ReadXML() method
Step-5 :Bind DropdownList with DataSet Instance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
      
        ds.ReadXml(Server.MapPath("countries.xml"));
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "countryName";
        DropDownList1.DataValueField = "countryId";
        DropDownList1.DataBind();
       

    }

}
Output
How to bind DropDownlist using Xml file in ASP.NET

Monday, October 7, 2013

Designing Contact us page using ASP.NET and C#

Introduction

A contact us page contains some information about visitor who visit the site and he/she want to contact site- administrator. It contains some fields such as Message subject , Body, Reply name and Reply email.

A simple snap of the contact us page.

Designing Contact us
For designing this type of contact us page first we should take a webform and after that drop some controls onit.
Designing methodology 
Step-1 : Take a webform named as "contact.aspx"
Step-2 :  For writing Contact Us , we should take division tag  <div> </div>
for example 
<div>
    <h2>Contact us</h2>
    </div>

Step-3 : Drop one label control in second division tag with blank text for display result message.

<div style="text-align :center ; background-color :Green" >
        <asp:Label ID="ResultLabel" runat="server" Text=""></asp:Label>

    </div>

Step-4 : Place Label , TextBox with proper validation control on design form.
<div>
        <table style="width: 100%;">
            <tr>
                <td class="style1">
                    &nbsp;
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                        ControlToValidate="msubtxt" ForeColor="Maroon" ValidationGroup="c1">*</asp:RequiredFieldValidator>
                    &nbsp;Message Subject :
                </td>
                <td>
                    &nbsp;<asp:TextBox ID="msubtxt" runat="server" TabIndex="1"
                        ValidationGroup="c1" Width="222px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td class="style1" valign="top">
                    &nbsp;
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                        ControlToValidate="bdytxt" ForeColor="Maroon" ValidationGroup="c1">*</asp:RequiredFieldValidator>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Body:</td>
                <td>
                    &nbsp;<asp:TextBox ID="bdytxt" runat="server" Height="87px"
                         TabIndex="2" TextMode="MultiLine"
                        ValidationGroup="c1" Width="223px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td class="style1" valign="top">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                        ControlToValidate="rplynmetxt" ForeColor="Maroon" ValidationGroup="c1">*</asp:RequiredFieldValidator>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Reply name:</td>
                <td>
                    <asp:TextBox ID="rplynmetxt" runat="server" TabIndex="3" ValidationGroup="c1"
                        Width="222px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1" valign="top">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
                        ControlToValidate="rplyemltxt" ForeColor="Maroon" ValidationGroup="c1">*</asp:RequiredFieldValidator>
&nbsp;<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                        ControlToValidate="rplyemltxt" ForeColor="Maroon"
                        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                        ValidationGroup="c1">*</asp:RegularExpressionValidator>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Reply Email:</td>
                <td>
                    <asp:TextBox ID="rplyemltxt" runat="server" TabIndex="4" ValidationGroup="c1"
                        Width="222px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1" valign="top">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Send" ValidationGroup="c1"
                        onclick="Button1_Click" />
&nbsp;<asp:Button ID="Button2" runat="server" Text="Cancel" ValidationGroup="c1" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
   

    </div>
Design View
Designing Contact us page using ASP.NET and C#

Code View

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class contact : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(rplyemltxt.Text);
            msg.To.Add("Email id here");
            msg.Subject = msubtxt.Text;
            msg.Body = "Name:" + rplynmetxt.Text + "<br/> Email : " + rplyemltxt.Text + "<br/>Subject :" + msubtxt.Text + "<br/>Contents :" + bdytxt.Text;
            msg.IsBodyHtml = true;
            SmtpClient smt = new SmtpClient("smtp.gmail.com", 587);
            smt.EnableSsl = true;
            smt.Credentials = new System.Net.NetworkCredential("Email id here", "your account password ");
            smt.Send(msg);
            ResultLabel.Text = "Send Message";
        }
        catch (Exception ex)
        {
            ResultLabel.Text = ex.Message;
        }



    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        msubtxt.Text = "";
        bdytxt.Text = "";
        rplyemltxt.Text = "";
        rplynmetxt.Text = "";

    }
}


Note :Read carefully about above mentioned code
msg.To.Add("Email id here"); // pass your gmail id 

 smt.Credentials = new System.Net.NetworkCredential("Email id here""your account password ");  // again pass same gmail id and pass here in the code which is mentioned above.

Output of the Contact page
Designing Contact us page using ASP.NET and C#

Email Output
Designing Contact us page using ASP.NET and C#

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

© Copyright 2013 Computer Programming | All Right Reserved