Code for viewing only that data which is selected from DropDown in Visual WebPart(SharePoint)

//Create a class to get and set value

class Data
    {
        public string Name { get; set; }
        public string Branch { get; set; }
        public string Roll_No { get; set; }
        public string Address { get; set; }
        public string Mobile_No { get; set; }
    }
   //Code fro Page Load
 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                try
                {
                    SPList info = SPContext.Current.Web.Lists["cust"];
                    List<Data> data = new List<Data>();
                    foreach (SPListItem item in info.Items)
                    {
                        Data d = new Data();
                        d.Roll_No = item["Roll_No."].ToString();
                        d.Name = item["Name"].ToString();
                        d.Mobile_No = item["Mobile_No."].ToString();
                        d.Address = item["Address"].ToString();
                        d.Branch = item["Branch"].ToString();
                        data.Add(d);


                    }
                    DropDownList1.DataTextField = "Roll_No";
                    DropDownList2.DataTextField = "Name";
                    DropDownList1.DataSource = data;
                    DropDownList2.DataSource = data;
                    DropDownList1.DataBind();
                    DropDownList2.DataBind();


                }
                catch (Exception ex)
                {
                    Label1.Text = ex.Message + "-" + ex.StackTrace;

                }

            }

        }

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["Drop"] = DropDownList1.SelectedValue.ToString();  /* Store dropdown value in a viewstate */
          
            Data d = new Data();
           
                SPList info = SPContext.Current.Web.Lists["cust"];
                List<Data> data = new List<Data>();
                foreach (SPListItem item in info.Items)
                {
                    if (ViewState["Drop"].ToString() == item["Roll_No."].ToString())
                    {
                    d.Roll_No = item["Roll_No."].ToString();
                    d.Name = item["Name"].ToString();
                    d.Mobile_No = item["Mobile_No."].ToString();
                    d.Address = item["Address"].ToString();
                    d.Branch = item["Branch"].ToString();
                    data.Add(d);
                    }
                   
                }

                GridView1.DataSource = data;
                GridView1.DataBind();
            }

Comments

Popular posts from this blog

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

How to import CSV files into DataTable in C#

go to top image