Thursday, 6 June 2013

DROPDOWNLIST FOR SELECTING COUNTRY,STATE,CITY USING GRIDVIEW

DROPDOWNLIST FOR SELECTING COUNTRY,STATE,CITY USING GRIDVIEW




Image

Image
DESIGN FOR GRIDVIEW(DEFAULT.ASPX):

<form id=”form1″ runat=”server”>
    <div>
  
    </div>
    <p>
        <asp:Button ID=”Button3″ runat=”server” onclick=”Button3_Click”
            style=”top: 21px; left: 112px; position: absolute; height: 23px; width: 68px; right: 795px;”
            Text=”Delete” />
    </p>
    <asp:Button ID=”Button1″ runat=”server”
        style=”top: 22px; left: 25px; position: absolute;right: 860px;”
        Text=”Get Data” onclick=”Button1_Click” />
    <p>
  
        <asp:GridView ID=”GridView1″ runat=”server”
          
            style=”top: 84px; left: 18px; position: absolute; height: 222px; width: 657px; bottom: 246px;”
            AutoGenerateColumns=”False” BackColor=”#CCCCCC” BorderColor=”#999999″
            BorderStyle=”Solid” BorderWidth=”3px” CellPadding=”4″ CellSpacing=”2″
            ForeColor=”#000066″ onrowediting=”GridView1_RowEditing”
            onrowupdating=”GridView1_RowUpdating”
            onrowcancelingedit=”GridView1_RowCancelingEdit” ShowFooter=”True”
            onrowcommand=”GridView1_RowCommand”
            onrowdatabound=”GridView1_RowDataBound” >
            <FooterStyle BackColor=”#CCCCCC” />
            <RowStyle BackColor=”White” />
            <Columns>
            <asp:TemplateField HeaderText=”CheckBox”>
            <ItemTemplate>
                     <asp:CheckBox ID=”ch1″ runat=”server” AutoPostBack=”true”/>
            </ItemTemplate>
              <FooterTemplate>
                    <asp:Button ID=”btnInsert” runat=”server” Text=”Insert” CommandName=”Add” />
             </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText=”jobid”>
            <ItemTemplate>
            <asp:LinkButton ID=”li1″ Text=’<%#Eval(“jobid”)%>’ runat=”server”></asp:LinkButton>
            </ItemTemplate>
            <EditItemTemplate>
            <asp:Label ID=”l1″ Text=’<%#Eval(“jobid”)%>’ runat=”server”></asp:Label>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID=”fid” runat=”server”></asp:TextBox>
            </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText=”description”>
            <ItemTemplate>
            <%#Eval(“description”)%>
            </ItemTemplate>
            <EditItemTemplate>
            <asp:TextBox ID=”t2″ Text=’<%#Eval(“description”)%>’ runat=”server”></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID=”fdesc” runat=”server”></asp:TextBox>
            </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText=”maxlvl”>
            <ItemTemplate>
            <%#Eval(“maxlvl”)%>
            </ItemTemplate>
            <EditItemTemplate>
            <asp:TextBox ID=”t3″ Text=’<%#Eval(“maxlvl”)%>’ runat=”server”></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID=”fmax” runat=”server”></asp:TextBox>
            </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText=”minlvl”>
            <ItemTemplate>
            <%#Eval(“minlvl”)%>
            </ItemTemplate>
            <EditItemTemplate>
            <asp:TextBox ID=”t4″ Text=’<%#Eval(“minlvl”)%>’ runat=”server”></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID=”fmin” runat=”server”></asp:TextBox>
            </FooterTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText=”countryname”>
            <ItemTemplate>
            <%#Eval(“countryname”)%>
            </ItemTemplate>
            <EditItemTemplate>
            <asp:DropDownList ID=”ddlEcountryname” onselectedindexchanged=”ddlEcountryname_SelectedIndexChanged” AutoPostBack=”true” runat=”server”></asp:DropDownList>
            </EditItemTemplate>
            <FooterTemplate>
             <asp:DropDownList ID=”ddlFcountryname” runat=”server” AutoPostBack=”true” onselectedindexchanged=”ddlFcountryname_SelectedIndexChanged”></asp:DropDownList>
            </FooterTemplate>
            </asp:TemplateField>
              <asp:TemplateField HeaderText=”statename”>
            <ItemTemplate>
            <%#Eval(“statename”)%>
            </ItemTemplate>
            <EditItemTemplate>
               <asp:DropDownList ID=”ddlEstatename”  OnSelectedIndexChanged=”ddlEstatename_SelectedIndexChanged” AutoPostBack=”true” runat=”server”></asp:DropDownList>
            </EditItemTemplate>
            <FooterTemplate>
              <asp:DropDownList ID=”ddlFstatename” OnSelectedIndexChanged=”ddlFstatename_SelectedIndexChanged” AutoPostBack=”true” runat=”server”></asp:DropDownList>
            </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText=”cityname”>
            <ItemTemplate>
            <%#Eval(“cityname”)%>
            </ItemTemplate>
            <EditItemTemplate>
               <asp:DropDownList ID=”ddlEcityname”  runat=”server”></asp:DropDownList>
            </EditItemTemplate>
            <FooterTemplate>
              <asp:DropDownList ID=”ddlFcityname” runat=”server”></asp:DropDownList>
            </FooterTemplate>
            </asp:TemplateField>
                   
            <asp:CommandField  ShowEditButton=”true”    ShowCancelButton=”true”/>
            </Columns>
            <PagerStyle BackColor=”#CCCCCC” ForeColor=”Black” HorizontalAlign=”Left” />
            <SelectedRowStyle BackColor=”#000099″ Font-Bold=”True” ForeColor=”White” />
            <HeaderStyle BackColor=”Black” Font-Bold=”True” ForeColor=”White” />
        </asp:GridView>
  
    </p>
    <p>
  
        &nbsp;</p>
 
    </form>

CODE(DEFAULT.ASPX.CS):
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
    DataSet ds;
    bl obj = new bl();
    bl obj1 = new bl();
    bl obj2 = new bl();
    jobtype j = new jobtype();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ds = obj.getdetails();
        GridView1.DataSource = ds;
        GridView1.DataBind();
        DropDownList ddlCountry1 = (DropDownList)GridView1.FooterRow.FindControl(“ddlFcountryname”);
        ddlCountry1.DataSource = obj1.getcountry();
        ddlCountry1.DataValueField = “countryid”;
        ddlCountry1.DataTextField = “countryname”;
        ddlCountry1.DataBind();
        ddlCountry1.Items.Insert(0, “–Select–”);
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        int index = GridView1.EditIndex;
        Session["index"] = index;
        Button1_Click(sender, e);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow r = GridView1.Rows[e.RowIndex];
        Label id = (Label)r.FindControl(“l1″);
        //string id = (r.Cells[1]).Text;
        TextBox des=(TextBox)r.FindControl(“t2″);
        TextBox max=(TextBox)r.FindControl(“t3″);
        TextBox min=(TextBox)r.FindControl(“t4″);
        DropDownList ddlcountry = (DropDownList)r.FindControl(“ddlEcountryname”);
        DropDownList ddlstate = (DropDownList)r.FindControl(“ddlEstatename”);
        j.Jobid =int.Parse(id.Text);
        j.Jobdesc = des.Text;
        j.Maxlvl = int.Parse(max.Text);
        j.Minlvl=int.Parse(min.Text);
        j.countryid = int.Parse(ddlcountry.SelectedValue);
        j.stateid = int.Parse(ddlstate.SelectedValue);
        GridView1.DataSource = obj.update(j);
        GridView1.EditIndex = -1;
        Button1_Click(sender, e);
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex =-1;
        Button1_Click(sender, e);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        string s = null;
        foreach (GridViewRow  r1 in GridView1.Rows)
        {
            CheckBox c = (CheckBox)r1.FindControl(“ch1″);
            if (c.Checked)
            {
                LinkButton l = (LinkButton)r1.FindControl(“li1″);
                sb.Append(l.Text).Append(‘,’);
            }
        }
        s = “delete from jobs where jobid in (“+sb.Remove(sb.Length – 1,1)+ “)”;
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = s;
        con.Open();
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        Button1_Click(sender, e);
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
        if (e.CommandName==”Add”)
        {
            GridViewRow r = GridView1.FooterRow;
            TextBox jid = (TextBox)r.FindControl(“fid”);
            TextBox jdes = (TextBox)r.FindControl(“fdesc”);
            TextBox jmax = (TextBox)r.FindControl(“fmax”);
            TextBox jmin = (TextBox)r.FindControl(“fmin”);
            DropDownList ddlcountry = (DropDownList)r.FindControl(“ddlFcountryname”);
            DropDownList ddlstate = (DropDownList)r.FindControl(“ddlFstatename”);
            DropDownList ddlcity = (DropDownList)r.FindControl(“ddlFcityname”);
            j.Jobid = int.Parse(jid.Text);
            j.Jobdesc = jdes.Text;
            j.Maxlvl = int.Parse(jmax.Text);
            j.Minlvl = int.Parse(jmin.Text);
            j.countryid = int.Parse(ddlcountry.SelectedValue);
            j.stateid = int.Parse(ddlstate.SelectedValue);
            j.cityid = int.Parse(ddlcity.SelectedValue);
         
            GridView1.DataSource = obj.insert(j);
            GridView1.EditIndex = -1;
            Button1_Click(sender, e);
        }
    }
    protected void ddlFcountryname_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlCountry1 = (DropDownList)GridView1.FooterRow.FindControl(“ddlFcountryname”);
        DropDownList ddlState1 = (DropDownList)GridView1.FooterRow.FindControl(“ddlFstatename”);
        //DropDownList ddlcity1 = (DropDownList)GridView1.FooterRow.FindControl(“ddlFcityname”);
      
        ddlState1.DataSource = obj2.getstatebycountry(Convert.ToInt32(ddlCountry1.SelectedValue));
        ddlState1.DataValueField = “stateid”;
        ddlState1.DataTextField = “statename”;
        ddlState1.DataBind();
        ddlState1.Items.Insert(0, “–Select–”);
    }
    protected void ddlEcountryname_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index=Convert.ToInt32(Session["index"]);
        DropDownList ddlCountry1 = (DropDownList)GridView1.Rows[index].FindControl(“ddlEcountryname”);
        DropDownList ddlState1 = (DropDownList)GridView1.Rows[index].FindControl(“ddlEstatename”);
        ddlState1.DataSource = obj2.getstatebycountry(Convert.ToInt32(ddlCountry1.SelectedValue));
        ddlState1.DataValueField = “stateid”;
        ddlState1.DataTextField = “statename”;
        ddlState1.DataBind();
        ddlState1.Items.Insert(0, “–Select–”);
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowType == DataControlRowType.DataRow) && (e.Row.RowState.HasFlag(DataControlRowState.Edit) && (e.Row.DataItem != null)))
        {
            DropDownList ddlCountry = e.Row.FindControl(“ddlEcountryname”) as DropDownList;
            ddlCountry.DataSource = obj1.getcountry();
            ddlCountry.DataValueField = “countryid”;
            ddlCountry.DataTextField = “countryname”;
            ddlCountry.DataBind();
            ddlCountry.Items.Insert(0, “–Select–”);
        }
    }
    protected void ddlFstatename_SelectedIndexChanged(object sender, EventArgs e)
    {
        //DropDownList ddlCountry1 = (DropDownList)GridView1.FooterRow.FindControl(“ddlFcountryname”);
        DropDownList ddlState1 = (DropDownList)GridView1.FooterRow.FindControl(“ddlFstatename”);
        DropDownList ddlcity1 = (DropDownList)GridView1.FooterRow.FindControl(“ddlFcityname”);
        ddlcity1.DataSource = obj2.getcitybystate(Convert.ToInt32(ddlState1.SelectedValue));
        ddlcity1.DataValueField = “cityid”;
        ddlcity1.DataTextField = “cityname”;
        ddlcity1.DataBind();
        ddlcity1.Items.Insert(0, “–Select–”);
 

    }
    protected void ddlEstatename_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = Convert.ToInt32(Session["index"]);
        //DropDownList ddlCountry1 = (DropDownList)GridView1.Rows[index].FindControl(“ddlEcountryname”);
        DropDownList ddlState1 = (DropDownList)GridView1.Rows[index].FindControl(“ddlEstatename”);
        DropDownList ddlcity1 = (DropDownList)GridView1.Rows[index].FindControl(“ddlEcityname”);
        ddlcity1.DataSource = obj2.getcitybystate(Convert.ToInt32(ddlState1.SelectedValue));
        ddlcity1.DataValueField = “cityid”;
        ddlcity1.DataTextField = “cityname”;
        ddlcity1.DataBind();
        ddlcity1.Items.Insert(0, “–Select–”);

    }
}

CLASS(DATAACESS LAYER.CS):
public class datalayer
{
    public static DataSet getdata(string sqlstat)
    {
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(sqlstat, cn);
        SqlCommandBuilder cmb = new SqlCommandBuilder(da);
        da.Fill(ds);
        return ds;
    }

CLASS(BL..CS):
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
/// Summary description for bl
/// </summary>
public class bl
{
    public DataSet getdetails()
    {
        //return datalayer.getdata(“select *from jobs”);
        //string S = “select j.jobid,j.description,j.maxlvl,j.minlvl,c.countryname,s.statename from jobs j,country c,state s where j.countryid=c.countryid and j.stateid=s.stateid”;
        string S = “select j.jobid,j.description,j.maxlvl,j.minlvl,c.countryname,s.statename,c1.cityname from jobs j,country c,state s,city c1 where j.countryid=c.countryid and j.stateid=s.stateid and j.cityid=c1.cityid”;
        return datalayer.getdata(S);
    }
    public DataSet update(jobtype j)
    {
        return datalayer.getdata(“update jobs set description=’” + j.Jobdesc + “‘,maxlvl=” + j.Maxlvl + “,minlvl=” + j.Minlvl + “,countryid=” + j.countryid + “,stateid=” + j.stateid + ” where jobid=” + j.Jobid);
    }
    public object insert(jobtype j)
    {
        //return datalayer.getdata(“insert into jobs values(” + j.Jobid + “,’” + j.Jobdesc + “‘,” + j.Maxlvl + “,” + j.Minlvl + “,” + j.countryid + “,” + j.stateid +”)”);
        return datalayer.getdata(“insert into jobs values(” + j.Jobid + “,’” + j.Jobdesc + “‘,” + j.Maxlvl + “,” + j.Minlvl + “,” + j.countryid + “,” + j.stateid + “,” + j.cityid + “)”);
    }
    //public object delete(jobtype j)
    //{
    //    string s = null;
    //    s += “delete jobs where jobid=” + j.Jobid + “” + “;”;
    //    s = s.Substring(0, s.TrimEnd(‘;’).Length);
    //    return datalayer.getdata(“s=” + “delete jobs where jobid=” + j.Jobid + “” + “;”);
    //}
    public DataSet getcountry()
    {
        return datalayer.getdata(“select *from country”);
    }
    public DataSet getstate()
    {
        return datalayer.getdata(“select *from state”);
    }
    public DataSet getstatebycountry(int countryid)
    {
        return datalayer.getdata(“select *from state where countryid=” + countryid + “”);
    }
    public DataSet getcitybystate(int cityid)
    {
        return datalayer.getdata(“select * from city where cityid=”+cityid+”");
     
    }
}
CLASS(JOBTYPE.CS):

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
/// Summary description for jobtype
/// </summary>
public class jobtype
{
    int _jobid, _maxlvl, _minlvl, _countryid, _stateid, _cityid;
    string _jobdesc;
    public string Jobdesc
    {
        get { return _jobdesc; }
        set { _jobdesc = value; }
    }
    public int Minlvl
    {
        get { return _minlvl; }
        set { _minlvl = value; }
    }
    public int Maxlvl
    {
        get { return _maxlvl; }
        set { _maxlvl = value; }
    }
    public int Jobid
    {
        get { return _jobid; }
        set { _jobid = value; }
    }
    public int countryid
    {
        get { return _countryid; }
        set { _countryid = value; }
    }
    public int cityid
    {
        get { return _cityid; }
        set { _cityid = value; }
    }
    public int stateid
    {
        get { return _stateid; }
        set { _stateid = value; }
    }
  
}

No comments:

Post a Comment