Monday, 10 June 2013

SharePoint 2010 - Webpart Demo- Emp


Open VS2010 ---> 
New ---> 
Project ---> 
Visual C# --> 
SharePoint ---> 
Empty SharePoint Project ---> 
[WpDemos] ---> OK

SE ---> Right Click ---> Add New Item ---> WebPart ---> EmpWebPart ---> OK

Write the following code 


public class EmpWebPart : WebPart
    {

        Label lblEmpname;
        Label lblEmpId;
        Label lblEmpEmail;
        Label lblProjectName;
        Label lblReult;

        TextBox txtEmpName;
        TextBox txtEmpId;
        TextBox txtEmpEmail;
        DropDownList ddProjectName;

        Button btnGo;

        protected override void CreateChildControls()
        {
            lblEmpname = new Label();
            lblEmpname.Text = " Enter Employee Name : ";
            Controls.Add(lblEmpname);

            lblEmpId = new Label();
            lblEmpId.Text = " Enter Employee Id : ";
            Controls.Add(lblEmpId);

            lblEmpEmail = new Label();
            lblEmpEmail.Text = " Enter Employee Email Id :";           
            Controls.Add(lblEmpEmail);

            lblProjectName = new Label();
            lblProjectName.Text = " Selecte Project Name : ";
            Controls.Add(lblProjectName);

            txtEmpName = new TextBox();
            txtEmpName.Width = 100;
            Controls.Add(txtEmpName);

            txtEmpId = new TextBox();
            txtEmpId.Width = 100;
            txtEmpId.Text = " Enter Id ..";
            Controls.Add(txtEmpId);

            txtEmpEmail = new TextBox();
            txtEmpEmail.Width = 100;
            txtEmpEmail.Text = " Enter Email Id ..";
            Controls.Add(txtEmpEmail);

            ddProjectName = new DropDownList();
            ddProjectName.Items.Add("Project 1");
            ddProjectName.Items.Add("Project 2");
            ddProjectName.Items.Add("Project 3");
            ddProjectName.Items.Add("Project 4");
            ddProjectName.Items.Add("Project 5");
            ddProjectName.Width = 200;
            Controls.Add(ddProjectName);

            btnGo = new Button();
            btnGo.Text = " Get Data ";
            btnGo.Click +=new EventHandler(btnGo_Click);
            Controls.Add(btnGo);

            lblReult = new Label();
            lblReult.Text = "";
            Controls.Add(lblReult);

        }

        public void btnGo_Click(object o, EventArgs e)
        {
            lblReult.Text = " Employee name: " + txtEmpName.Text + "<br> Employee Id :" + txtEmpId.Text + "<br> Employee Email: " + txtEmpEmail.Text + "<br> Project Name : " + ddProjectName.SelectedItem;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<table><tr><td>");
            lblEmpname.RenderControl(writer);
            writer.Write("</td><td>");
            txtEmpName.RenderControl(writer);
            writer.Write("</td></tr><tr><td>");
            lblEmpId.RenderControl(writer);
            writer.Write("</td><td>");
            txtEmpId.RenderControl(writer);
            writer.Write("</td></tr><tr><td>");
            lblEmpEmail.RenderControl(writer);
            writer.Write("</td><td>");
            txtEmpEmail.RenderControl(writer);
            writer.Write("</td></tr><tr><td>");
            lblProjectName.RenderControl(writer);
            writer.Write("</td><td>");
            ddProjectName.RenderControl(writer);
            writer.Write("</td></tr><tr><td>");
            btnGo.RenderControl(writer);
            writer.Write("</td><td>");
            lblReult.RenderControl(writer);
            writer.Write("</td></tr></table>");
        }
    }

No comments:

Post a Comment