Showing posts with label DropDown. Show all posts
Showing posts with label DropDown. Show all posts

Sunday, 20 January 2013

ToolTip in dropdown in asp.net using c#.


In this post I will explain how to show tooltip in dropdown in asp.net using c#.

Description:-

In dropdown when the user name or something is too long ,so that we cannot read or see it properly in dropdown we use tooltip to see the full name. When cursor is on that item in dropdown it will show the full name or path. It’s better to see the example.

First make a design page ,on this take one dropdownlist and bind with table .

Default.aspx:-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <asp:DropDownList ID="DropDownList1" runat="server"
            ondatabound="DropDownList1_DataBound">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Code behind page:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class dropdown_ToolTipInDropDown : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropDown();
        }
    }
    public void BindDropDown()
    {
        SqlConnection con = new SqlConnection(@"Data Source=SUSHIL-PC;Initial Catalog=modi;Integrated Security=True");
        SqlCommand com = new SqlCommand("Select EmpId , EmpName From EmpDetails",con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        con.Open();
        da.Fill(ds);
        con.Close();
        DropDownList1.DataTextField = "EmpName";
        DropDownList1.DataValueField = "EmpId";
        DropDownList1.DataSource = ds;
        DropDownList1.DataBind();
    }
    protected void DropDownList1_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        if (ddl != null)
        {
            foreach (ListItem li in ddl.Items)
            {
                li.Attributes["title"]=li.Text;
            }
        }
    }
}

Output :-

Sunday, 13 January 2013

Read Xml file in DropDownList in asp.net using c#.


In this post I will explain how to read XML file in DropDownList in asp.net using c#.

Description:-


I have written many post regarding DropDownList. In this post I will explain how to read a xml file in DropDownList or how to bind xml data to DropDownList. For this first Create an xml file and put some dummy data in it.

Create a xml file name Employee.xml and insert some data in this file.

Employee.xml:-

<?xml version="1.0" encoding="utf-8" ?>
<employees>
  <employee>
    <EmpName>Sushil Kumar</EmpName>
    <EmailId>Sushilct25@gmail.com</EmailId>
    <Designation>Engg.</Designation>
    <PhoneNo>9998882345</PhoneNo>
  </employee>
  <employee>
    <EmpName>Prdeep Kumar</EmpName>
    <EmailId>pradeep@gmail.com</EmailId>
    <Designation>Manager</Designation>
    <PhoneNo>12344567890</PhoneNo>
  </employee>
  <employee>
    <EmpName>sushil Modi</EmpName>
    <EmailId>modi@gmail.com</EmailId>
    <Designation>Soft. Developer</Designation>
    <PhoneNo>224525426</PhoneNo>
  </employee>
  <employee>
    <EmpName>Rahul Ranjan</EmpName>
    <EmailId>ranjan@gmail.com</EmailId>
    <Designation>Engg.</Designation>
    <PhoneNo>1245225</PhoneNo>
  </employee>
  <employee>
    <EmpName>Chulbul Pandey</EmpName>
    <EmailId>pandey@gmail.com</EmailId>
    <Designation>Economist</Designation>
    <PhoneNo>235235353</PhoneNo>
  </employee>
</employees>

After that add a web page to the project and drag  a dropdownlist on this page like this..

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Read Xml file in DrpDownList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

After that add the following namespace  in codebehind:-

using System.Data;
using System.Xml;

Code behind page:-

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropDownList();
        }
    }
    public void BindDropDownList()
    {
        XmlTextReader xmlReader = new XmlTextReader(Server.MapPath("Employee.xml"));
        DataSet ds = new DataSet();
        ds.ReadXml(xmlReader);
        xmlReader.Close();
        if (ds.Tables.Count != 0)
        {
            DropDownList1.DataSource = ds;
            DropDownList1.DataTextField = "EmpName";
            DropDownList1.DataValueField = "EmpName";
            DropDownList1.DataBind();
        }
       
    }

         Output Like this..



If you like this post please spread this among in your friends.


Tuesday, 11 December 2012

Cascaded DropDown in Asp.net.


In this post I will explain Cascaded DropDown in Asp.Net.
Description:-

Cascaded DropDown means when we select one dropdownlist then the second dropdownlist will bind the data according to selection of first dropdownlist. In simple word when we select country in one dropdownlist then in second dropdownlist city will come according to country. It’s better to understand with pracrical example so follow the code.
First make the three table country state and city.

Country Table:-

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[country](
                [country_id] [int] NOT NULL,
                [country_name] [varchar](max) NOT NULL,
 CONSTRAINT [PK_country] PRIMARY KEY CLUSTERED
(
                [country_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

State Table :-

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[state](
                [state_id] [int] NOT NULL,
                [state_name] [varchar](max) NOT NULL,
                [country_id] [int] NOT NULL,
 CONSTRAINT [PK_state] PRIMARY KEY CLUSTERED
(
                [state_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[state]  WITH CHECK ADD  CONSTRAINT [FK_state_country] FOREIGN KEY([country_id])
REFERENCES [dbo].[country] ([country_id])
GO

ALTER TABLE [dbo].[state] CHECK CONSTRAINT [FK_state_country]
GO

City Tabel:-

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[city](
                [city_id] [int] NOT NULL,
                [city_name] [varchar](max) NOT NULL,
                [state_id] [int] NOT NULL,
 CONSTRAINT [PK_city] PRIMARY KEY CLUSTERED
(
                [city_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[city]  WITH CHECK ADD  CONSTRAINT [FK_city_city] FOREIGN KEY([state_id])
REFERENCES [dbo].[state] ([state_id])
GO

ALTER TABLE [dbo].[city] CHECK CONSTRAINT [FK_city_city]
GO

Run the following code in your database.
Now create web page.

DropDown.aspx:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center">
<tr>
<td colspan="2" height="200px" valign="bottom">
<h3>Cascading Dropdowns Sample</h3>
</td>
</tr>
<tr>
<td>
Select Country:
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlCountry_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
Select State:
</td>
<td>
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlState_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
Select Region:
</td>
<td>
<asp:DropDownList ID="ddlRegion" runat="server"
        onselectedindexchanged="ddlRegion_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
</table>
    </div>
    </form>
</body>
</html>

Code behind page:-

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

public partial class DropDown : System.Web.UI.Page
{
    private String strConnection = @"Data Source=SUSHIL-PC;Initial Catalog=modi;Integrated Security=True";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindContrydropdown();
        }
    }
    protected void BindContrydropdown()
    {
        //conenction path for database
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from country", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        ddlCountry.DataSource = ds;
        ddlCountry.DataTextField = "country_name";
        ddlCountry.DataValueField = "country_id";
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
        ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
        ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));

    }
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from state where country_id=" + CountryID, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        ddlState.DataSource = ds;
        ddlState.DataTextField = "state_name";
        ddlState.DataValueField = "state_id";
        ddlState.DataBind();
        ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
        if (ddlState.SelectedValue == "0")
        {
            ddlRegion.Items.Clear();
            ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
        }
    }
    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        int StateID = Convert.ToInt32(ddlState.SelectedValue);
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from city where state_id=" + StateID, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        ddlRegion.DataSource = ds;
        ddlRegion.DataTextField = "city_name";
        ddlRegion.DataValueField = "city_id";
        ddlRegion.DataBind();
        ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
        if (ddlState.SelectedValue == "0")
        {
            ddlRegion.Items.Clear();
            ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
        }
    }
    protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
Debug and Run.