DYNAMICALLY ADD AND SAVE ASP.NET CONTROLS VALUE IN DATABASE

In this article, We will show you how to add ASP.Net web controls(TextBox, Dropdownlist, Radiobuttonlist) dynamically and also how to get that dynamic control value in aspx.cs page. Follow this below coding


Firstly Create a Database “test”. Then add tables.

E.g:

TBL_CONTROL.dbo for to add controls name.
CREATE TABLE [dbo].[TBL_CONTROL](
            [CONTROL_ID] [int] primary key IDENTITY(1,1) NOT NULL,
            [CONTROL_NAME] [varchar](50) NULL
)

TBL_ATTRIBUTE.dbo for to add attributes with controls id.
CREATE TABLE [dbo].[TBL_ATTRIBUTE](
            [ATTRIBUTE_ID] [int] primary key IDENTITY(1,1) NOT NULL,
            [CONTROL_ID] [int] NULL,
            [ATTRIBUTE_NAME] [varchar](50) NULL
)

TBL_OPTION.dbo for to add options for attributes.
CREATE TABLE [dbo].[TBL_OPTION](
            [OPTION_ID] [int] primary key IDENTITY(1,1) NOT NULL,
            [ATTRIBUTE_ID] [int] NULL,
            [OPTION_NAME] [varchar](50) NULL
)

TBL_REPORT.dbo to add all details in this table.
CREATE TABLE [dbo].[TBL_REPORT](
            [ID] [int] primary key IDENTITY(1,1) NOT NULL,
            [ATTRIBUTE_ID] [int] NULL,
            [VALUE] [varchar](50) NULL
)

Now Create an Asp.Net webform application. And named it. Then add your entity framework in the model folder.

Then add a webform in it. Write these below coding.

Coding for Design Page.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="controlsadd" runat="server"></asp:PlaceHolder>
        <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click"/>
    </div>
    </form>
</body>
</html>
For Coding Page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using testcontrols.Model;
namespace testcontrols
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        testEntities1 ob = new testEntities1();
        protected void Page_Load(object sender, EventArgs e)
        {
                bind();
        }
        public void bind()
        {
            var ss = (from a in ob.TBL_ATTRIBUTE.ToList()
                      join b in ob.TBL_CONTROL.ToList() on a.CONTROL_ID equals b.CONTROL_ID
                      select new
                      {
                          a.ATTRIBUTE_ID,
                          a.ATTRIBUTE_NAME,
                          a.CONTROL_ID,
                          b.CONTROL_NAME
                      }).ToList();
            foreach (var a in ss.ToList())
            {
                Literal li = new Literal();
                li.Text = "<div style='width:20%;float:left;'>" + a.ATTRIBUTE_NAME + "</div>";
                controlsadd.Controls.Add(li);
                controlsadd.Controls.Add(controlret(a.ATTRIBUTE_NAME + a.CONTROL_ID, a.CONTROL_NAME, a.ATTRIBUTE_ID));
                 Literal li1 = new Literal();
                 li1.Text = "</br>";
                 controlsadd.Controls.Add(li1);
            }
        }
        public WebControl controlret(string id, string controlname,int attributeid)
        {
            WebControl aa = null;
            var Obb = (from a in ob.TBL_OPTION
                       where a.ATTRIBUTE_ID == attributeid
                       select a).ToList();
            switch (controlname)
            {
                case "Textbox":
                    TextBox txt = new TextBox();
                    txt.ID = id;
                    txt.Width = 100;
                    aa =  txt ;
                    break;
                case "Dropdownlist":
                    DropDownList ddl = new DropDownList();
                    ddl.ID = id;
                    ddl.Width = 100;
                    ddl.DataSource = Obb;
                    ddl.DataTextField = "OPTION_NAME";
                    ddl.DataValueField = "OPTION_ID";
                    ddl.DataBind();
                    aa = ddl ;
                    break;
                case "Radiobuttonlist":                     RadioButtonList rdbtn = new RadioButtonList();                     rdbtn.ID = id;                     rdbtn.Width = 100;                     rdbtn.DataSource = Obb;                     rdbtn.DataTextField = "OPTION_NAME";                     rdbtn.DataValueField = "OPTION_ID";                     rdbtn.DataBind();                     aa = rdbtn ;                     break;
            }             return aa;         }         protected void Button1_Click(object sender, EventArgs e)         {             TBL_REPORT os = new TBL_REPORT();             var ss = (from a in ob.TBL_ATTRIBUTE.ToList()                       join b in ob.TBL_CONTROL.ToList() on a.CONTROL_ID equals b.CONTROL_ID                       select new                       {                           a.ATTRIBUTE_ID,                           a.ATTRIBUTE_NAME,                           a.CONTROL_ID,                           b.CONTROL_NAME                       }).ToList();             foreach (var a in ss.ToList())             {                 Control mycontrol = this.Page.FindControl(a.ATTRIBUTE_NAME + a.CONTROL_ID);                 os.ATTRIBUTE_ID = a.ATTRIBUTE_ID;                 os.VALUE = value(a.CONTROL_NAME,mycontrol);                 ob.TBL_REPORT.Add(os);                 ob.SaveChanges();             }         }         public string value(string control,Control id)         {             string value = null;             switch (control)             {                 case "Textbox":                     TextBox txt = (TextBox)FindControl(id.ID);                     value = txt.Text;                     break;                 case "Dropdownlist":                     DropDownList ddl = (DropDownList)FindControl(id.ID);                     value = ddl.SelectedItem.Text;                     break;                 case "Radiobuttonlist":                     RadioButtonList rdbtn = (RadioButtonList)FindControl(id.ID);                     value = rdbtn.SelectedItem.Text;                     break;             }             return value;         }     } }



Post a Comment

0 Comments