You must Sign In to post a response.
  • Category: .NET

    Create dynamic controls with values from database

    Requirement :
    1. Create controls on Web page Dynamically using Database and C#.
    Scenario :
    1. All Information will come from Database table
    Like : Level Name ,text ,Value,Position , type and controls type also ....etc
    2. Controls must be create on page load and and need to be bind from database during page load.
    3. Don't want to connect so many times with Database to get the controls and their values.
    because It's on page load so page loading time can be increase.

    Implemented logic:
    On page load i am fetching all controls details on page load from database and
    after creating controls again I am Connecting with Database to bind the controls.

    Please suggest some other way to handle this ..
    thanks in advance.
    regards,
    Pradeep
  • #762620
    hello pradeep,

    This is not a good way to post.

    Your post is too big to undersatnd. there is much lines of coding. First of all you just split your requirements then post it one by one, then only we can able to understand it more clearly.don't panic try it bold one by one.

    Don't take mistake about me.

    hope this information will help you.

    Paul.S

  • #762624
    Hi Pradeep.

    what type of controls you want to disply, show what all the columns present in your table.
    Like Name as label
    city as dropdown
    or you want all those to be displayed in dynamically created textboxs.
    You can post a image so that we can understand.

    Sridhar Thota.
    Editor: DNS Forum.

  • #762627
    All Kind of ASP.Net .like drop down ,text box,Radio Button etc..and all information I have stored in Database so On Run time I have to find particular controls on specific page...

    all things are mapped in Database ...I need to display these controls on page without connecting multiple times to database..
    I am thinking about Ajax method
    or Services etc Kindly suggest ..

    thanks
    Pradeep

  • #762649
    Hi

    Try below code and map according to your requirement.

    public partial class _Default : System.Web.UI.Page
    {
    SqlConnection cn = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    Label[] l;
    TextBox[] t;
    DropDownList[] d;
    protected void Page_Load(object sender, EventArgs e)
    {
    cn.ConnectionString = WebConfigurationManager.AppSettings["constr"];
    cn.Open();
    cmd.CommandText = "select * from _emp";
    cmd.Connection = cn;
    SqlDataReader dr;
    dr = cmd.ExecuteReader();
    l = new Label[dr.FieldCount];
    t = new TextBox[dr.FieldCount];
    d = new DropDownList[dr.FieldCount];
    for (int i = 0; i < dr.FieldCount; i++)
    {
    if (dr.Read())
    {
    l[i] = new Label();
    l[i].Text = dr.GetName(i);
    this.form1.Controls.Add(l[i]);
    t[i] = new TextBox();
    this.form1.Controls.Add(t[i]);
    t[i].Text = dr[i].ToString();
    d[i] = new DropDownList();
    d[i].Items.Add(dr[i].ToString());
    this.form1.Controls.Add(d[i]);
    }
    }
    }
    }

    Sridhar Thota.
    Editor: DNS Forum.

  • #762650
    thanks for your guidance. I did the same already..
    Wants to create a Drop Down with their values.
    what I am doing :
    1. getting controls details from DB and creating the controls.
    2. again connecting with DB to get the Drop down values.(All Items)

    So what I want : I don't want to connect multiple times. for all controls. I think in performance point of View this is not acceptable. Please suggest some diff idea..

    thanks
    Pradeep

  • #762705
    Hai Pradeep,
    You can get all the information at once from the database and the you can use the switch case statement to go to each of the branch like:

    switch(controlType)
    {
    case "TextBox":
    // logic to generate the text box and fill its value
    break;
    case "Dropdown":
    // logic to generate the dropdown and fill its value
    break;
    ...
    ..
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #762706
    thanks for your response,
    I have implemented same already..I want to avoid database hit multiple times..I have got all controls and even using case statement I have created controls. now I have to bind these dynamically created controls with values.

    I am connecting with DB tow times.
    thanks
    pradeep


Sign In to post your comments