Code for adding HTML controls in Visual Web Part (For SharePoint 2010)

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing;


namespace Sun_SharePointProject2.Gunjan
{
    [ToolboxItemAttribute(false)]
    public class Gunjan : WebPart
    {
        //   public WebPart1()
        //   {
        //       AllowClose=false;
        //   }
/* Here i have taken 2 textbox and 2 label and 1 button for showing a table which will have 3 rows and 2 columns and will contain username and password button and textbox for each label and a button to submit */
        private TextBox tx;
        private TextBox t1;
        private Label l;
        private Label l1;
        private Button bt;
        HtmlTextWriter writer;
        TableCell tc;
        protected override void CreateChildControls()
        {
            tc = new TableCell();
            Controls.Add(tc);
            l = new Label();
            l.Text = "User_Name";
            Controls.Add(l);
            //Controls.Add(new LiteralControl("    "));

            tx = new TextBox();
            Controls.Add(tx);

           // Controls.Add(new LiteralControl("<br/>"));


            l1 = new Label();
            l1.Text = "Password";
            Controls.Add(l1);

          //  Controls.Add(new LiteralControl("    "));
            t1 = new TextBox();
            Controls.Add(t1);
          //  Controls.Add(new LiteralControl("<br/>"));

            bt = new Button();
            bt.Text = "Submit";
            bt.Click += new EventHandler(bt_Click);
            Controls.Add(bt);
           
        

        }

        protected void bt_Click(object sender, EventArgs e)
        {
           
        }



        protected override void RenderContents(HtmlTextWriter writer)
        {

            //writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Blue");
            //writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, "20px");
            //writer.RenderBeginTag(HtmlTextWriterTag.H2);
           


            ////writer.Write("Hello World");
            //writer.WriteBeginTag("tr");
            //writer.WriteBeginTag("td");
            //writer.Write("hello");
            //writer.WriteEndTag("td");
           

            //writer.RenderEndTag();

           /// tx.RenderControl(writer);

            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Enter Your Username");
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            tx.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Enter Your Password");
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            t1.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            tc.ColumnSpan = 1;
            //writer.Write("        ");
            bt.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
          
        }
       
    }
}

Comments

Popular posts from this blog

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

How to import CSV files into DataTable in C#

Search data in Gridview on Textbox Key press event using JQuery in Asp.Net- C#

go to top image