Friday, February 5, 2010

Display the Cell Data of a Row in Textboxes using DropdownList for Identifying the Row

Here the dropdownlist is used to select a row through the ID column.
The selected row's cell data will be displayed on textboxes.
The database table I have used is as shown: (Table name: details1).

Bind the Dropdownlist with the ID column of the table either by using the fallowing code or by the smart task menu >> ConfigureDataSource wizard.








protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con1 = new SqlConnection("your connectionstring");
            con1.Open();
            SqlDataAdapter da1 = new SqlDataAdapter("select id from details1", con1);
            DataSet ds1 = new DataSet();
            da1.Fill(ds1);
            Dropdownlist1.DataSource = ds1;
            Dropdownlist1.DataTextField = "id";
            Dropdownlist1.DataBind();
            con1.Close();
        }
protected void  DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {

        SqlConnection con1 = new SqlConnection("your connectionstring");
        con1.Open();
        SqlDataAdapter da1 = new SqlDataAdapter("select name,designation,place from details1 where   id=@id", con1);
        da1.SelectCommand.Parameters.AddWithValue("@id", DropDownList1.Text);
        DataTable dt1 = new DataTable();
        da1.Fill(dt1);
        con1.Close();
        DataRow row1 = dt1.Rows[0];
        txtname.Text = row1["name"].ToString();
        txtdsgn.Text = row1["designation"].ToString();
        txtplace.Text = row1["place"].ToString();

    }

Monday, February 1, 2010

Nesting 3 DropdownLists in ASP.net C#

  •   Add 3 DropdownList controls to .aspx page.
  • Enable AutoPostback for the 1st and 2nd Dropdownlists.
public partial class Default : System.Web.UI.Page
{
    protected string[] d1 = { "select","eng", "mba"};

    protected string[] eng = { "select","ec","cs" };
    protected string []mba = { "select","fin", "hr" };
   

    protected string []ec = { "ec1", "ec2"};
    protected string []cs = { "cs1", "cs2"};

   
    protected string []fin = { "fin1", "fin2"};
    protected string []hr = { "hr1", "hr2"};

  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = d1;
            DropDownList1.DataBind();
        }
       
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (DropDownList1.SelectedIndex == 1)
        {
            DropDownList2.DataSource = eng;
            DropDownList2.DataBind();
        }
        else if (DropDownList1.SelectedIndex == 2)
        {
            DropDownList2.DataSource = mba;
            DropDownList2.DataBind();
        }
       
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
      
        switch (DropDownList2.SelectedIndex)
        {
            case 1:
                {

                    if (DropDownList1.SelectedIndex == 1)
                    {
                        DropDownList3.DataSource = ec;
                        DropDownList3.DataBind();
                       
                    }
                    else if (DropDownList1.SelectedIndex == 2)
                    {
                        DropDownList3.DataSource = fin;
                        DropDownList3.DataBind();
                       
                    }
                    break;
                }
            case 2:
                {
                    if (DropDownList1.SelectedIndex == 1)
                    {
                        DropDownList3.DataSource = cs;
                        DropDownList3.DataBind();
                       
                    }
                    else if (DropDownList1.SelectedIndex == 2)
                    {
                        DropDownList3.DataSource = hr;
                        DropDownList3.DataBind();
                       
                    }
                }
                break;
         }

    }
}

NESTED DROPDOWNLISTS IN ASP.NET C#(c sharp)

Nesting Dropdownlists is a method of controlling list items of a Dropdownlist through selected index of other DropdownList.
For example let's take two DropdownLists where the first acts as a parent to the second.

Let's assume to list Movie names in the first DropdownList and to list in the second DropdownList the Theatres showing the selected Movie.

  • Add two DropdownList controls to the .aspx page and enable AutoPostback in the properties window of first DropdownList.
  • Add the fallowing code:

public partial class Default : System.Web.UI.Page
{
    // First DropdownList items
    protected string[] Movies ={ "select a movie", "Movie_1", "Movie_2", "Movie_3" };

    // Second DropdownList items. One among these to be choosen.
    protected string[] Movie_1={"Theatre_1","Theatre_2","Theatre_3"};
    protected string[] Movie_2 ={ "Theatre_4", "Theatre_5" };
    protected string[] Movie_3 ={ "Theatre_5", "Theatre_6", "Theatre_7" };
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = Movies;
            DropDownList1.DataBind();
        }

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex == 1)
        {
            DropDownList2.DataSource = Movie_1;
            DropDownList2.DataBind();
        }
        if (DropDownList1.SelectedIndex == 2)
        {
            DropDownList2.DataSource = Movie_2;
            DropDownList2.DataBind();
        }
        if (DropDownList1.SelectedIndex == 3)
        {
            DropDownList2.DataSource = Movie_3;
            DropDownList2.DataBind();
        }
    }
}