locked
How to store selected values from Dynamically created dropdownlist RRS feed

  • Question

  • User-640694033 posted

    Here is the code..

    please do help ..

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class TimeTableTest : System.Web.UI.Page
    {
    string strSubjects, strTest = "";
    DataAccessLayer objDAL = new DataAccessLayer();
    protected void Page_Load(object sender, EventArgs e)
    {
    //LoadTimeTable();
    }
    protected void Page_Init(object sender, EventArgs e)
    {
    LoadTimeTable();
    }

    private void LoadTimeTable()
    {
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Day", typeof(string)));
    DataRow dr = null;
    for (int i = 1; i <= 7; i++)
    {

    dr = dt.NewRow();
    dr["Day"] = "Day" + i;
    dt.Rows.Add(dr);
    }
    dlDay.DataSource = dt;
    dlDay.DataBind();
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
    Label lblDay1 = (Label)e.Item.FindControl("lblDay");
    DataList dlPeriods = (DataList)e.Item.FindControl("dlPeriods");
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Day", typeof(string)));
    DataRow dr = dt.NewRow();
    dr["Day"] = 1;
    dt.Rows.Add(dr);
    dlPeriods.RepeatColumns = 8;
    dlPeriods.DataSource = dt;
    dlPeriods.DataBind();


    }
    protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
    {
    Panel PnlPeriod = (Panel)e.Item.FindControl("PnlPeriod");

    for (int j = 1; j <= 8; j++)
    {
    //DropDownList ddlTemp1 = (DropDownList)e.Item.FindControl("ddlTable1");
    DropDownList ddlTemp1 = new DropDownList();
    ddlTemp1.ID = "DropDown" + j.ToString();
    DataTable dtSubject = objDAL.Subject();
    ddlTemp1.DataSource = dtSubject;
    ddlTemp1.DataTextField = "SubjectName";
    ddlTemp1.DataValueField = "SubjectId";
    ddlTemp1.DataBind();

    ddlTemp1.SelectedIndexChanged += OnSelectedIndexChanged;
    PnlPeriod.Controls.Add(ddlTemp1);
    ddlTemp1.AutoPostBack = true;
    }
    }

    protected void btnClick_Click(object sender, EventArgs e)
    {

    DataTable dtInsert = new DataTable();
    dtInsert.Columns.Add(new DataColumn("DayOrder", typeof(string)));
    dtInsert.Columns.Add(new DataColumn("Subjects", typeof(string)));
    for (int i = 0; i < dlDay.Items.Count; i++)
    {
    DataRow dr = dtInsert.NewRow();
    Label lblDay = (Label)dlDay.Items[i].FindControl("lblDay");
    DataList dlPeriods = (DataList)dlDay.Items[i].FindControl("dlPeriods");
    dr["DayOrder"] = lblDay.Text;
    string strSubject = string.Empty;
    for (int j = 0; j < dlPeriods.RepeatColumns; j++)
    {
    string ddlId = "DropDown" + (j+1).ToString();
    DropDownList ddlID = (DropDownList)dlPeriods.Items[j].FindControl(ddlId);
    strSubject += ddlID.SelectedItem.ToString()+ ",";
    }
    strSubject.Trim(',');
    dr["Subjects"] = strSubject;
    dtInsert.Rows.Add(dr);
    }

    //string Pass = Convert.ToString(ViewState["dtTemp"]);
    //string[] strSub = Pass.Split(',');
    //foreach (string strword in strSub)
    //{
    // DataTable dt = new DataTable();
    // dt.Columns.Add("Subj", typeof(string));
    // DataRow dr = null;
    // dr["Subj"] = strword;
    // dt.Rows.Add(dr);
    //}
    }

    public void OnSelectedIndexChanged(object sender, EventArgs e)
    {

    DropDownList ddlTemp = (DropDownList)sender;
    string ID = ddlTemp.ID;
    //ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert","<script type = 'text/javascript'>alert('" + ID +" fired SelectedIndexChanged event');</script>");
    strSubjects += ddlTemp.SelectedItem.ToString() + ',';
    ViewState["dtTemp"] = strSubjects;
    }
    }

    Monday, June 18, 2018 5:44 AM

All replies

  • User-330142929 posted

    Hi Dinesh Kanda,

    According to your description, I could not reproduce your problem. because your code is incomplete.

    Please add your description and frontend aspx code to help me clear about your requirement.

    It is easily to give you a solution effectively if you could help me understand your problem.

    Best Regards,

    Abraham

    Tuesday, June 19, 2018 8:23 AM
  • User-492460945 posted

    Hi Dinesh,

    What issue you're facing exactly? or can you tell your error?

    Tuesday, June 19, 2018 1:09 PM
  • User-1171043462 posted
    Refer

    https://www.aspsnippets.com/Articles/Creating-Dynamic-DropDownList-Controls-in-ASP.Net.aspx
    Sunday, June 24, 2018 4:59 PM