Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public partial class Code_User_DMSdataclass : System.Web.UI.Page
{
    private int numOfRows = 1;
    SqlCommand cmd = null;
    SqlConnection objSqlConnection1 = null;
    Dataconnection connection = new Dataconnection();
    priviligesChecking priviliges = new priviligesChecking();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["RowsCount"] = numOfRows + 1;
            GenerateTable(numOfRows);
            
        }
        System.Web.UI.WebControls.Button btn = (System.Web.UI.WebControls.Button)Panel2.FindControl("btn_addindex");
        if (btn == null)
        {
            if (ViewState["RowsCount"] != null)
            {
                numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
                GenerateTable(--numOfRows);
            }
        }
        
    }
   
    protected void btn_addindex_Click(object sender, EventArgs e)
    {

        if (ViewState["RowsCount"] != null)
        {
            numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
            GenerateTable(numOfRows);
        }
    }
    private void SetPreviousData(int rowsCount, int colsCount)
    {
        ArrayList name = new ArrayList();
        ArrayList discription = new ArrayList();
        ArrayList type = new ArrayList();
        Table table = (Table)Panel1.FindControl("table3");
        if (table != null)
        {
            for (int i = 0; i < rowsCount; i++)
            {
                for (int j = 0; j < colsCount; j++)
                {
                    if (j == 2)
                    {
                        DropDownList Db =(DropDownList)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
                        Db.Text =Request.Form["TextBoxRow_" + i + "Col_" + j];
                        string d_type = Db.Text;
                        type.Add(d_type);
                    }
                    //Extracting the Dynamic Controls from the Table
                    if (j == 1)
                    {
                        System.Web.UI.WebControls.TextBox tb = (System.Web.UI.WebControls.TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);

                        //Use Request objects for getting the previous data of the dynamic textbox
                        tb.Text =Request.Form["TextBoxRow_" + i + "Col_" + j];
                        string d_d = tb.Text;
                        discription.Add(d_d);
                    }
                    if (j == 0)
                    {
                        System.Web.UI.WebControls.TextBox tb1 = (System.Web.UI.WebControls.TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
                        
                        //Use Request objects for getting the previous data of the dynamic textbox
                        tb1.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];
                        string discriptor = tb1.Text;
                        name.Add(tb1.Text);
                    }

                }
            }

        }
        ViewState["Name"] = name;
        ViewState["Descriptor"] = discription;
        ViewState["Type"] = type;
    }
    private void GenerateTable(int rowsCount)
    {
        objSqlConnection1 = connection.getConnection();
        objSqlConnection1.Open();
        //Creat the Table and Add it to the Page
        Table table = new Table();
        table.ID = "table3";
        Panel1.Controls.Add(table);
        //The number of Columns to be generated
        const int colsCount = 3;//You can changed the value of 3 based on you requirements
        // Now iterate through the table and add your controls
        for (int i = 0; i < rowsCount; i++)
        {
            TableRow row = new TableRow();
            for (int j = 0; j < colsCount; j++)
            {
                TableCell cell = new TableCell();
                System.Web.UI.WebControls.TextBox tb = new System.Web.UI.WebControls.TextBox();
                DropDownList dl = new DropDownList();
                // Set a unique ID for each TextBox added
                // Add the control to the TableCell
                if (j == 2)
                {
                    
                    string query1 = "select * from Tbl_Datatype";
                    cmd = new SqlCommand(query1, objSqlConnection1);
                    SqlDataReader dr1;
                    dr1 = cmd.ExecuteReader();
                    dl.Items.Add(new ListItem("Select One...", "-1"));
                    int Dt_id1;
                    string Dt_name;
                    //String g_name;
                    //int p_type_id;

                    while (dr1.Read())
                    {
                        Dt_id1 = dr1.GetInt32(0);
                        Dt_name = dr1.GetString(1).ToString();
                        dl.Items.Add(new ListItem(Dt_name, Dt_id1.ToString()));
                        //dbodescriptor2.Items.Add(new ListItem(Dt_name, Dt_id1.ToString()));
                        //dbodescriptor3.Items.Add(new ListItem(Dt_name, Dt_id1.ToString()));
                    }
                    dr1.Close();
                    dl.ID = "TextBoxRow_" + i + "Col_" + j;
                    dl.CausesValidation = false;
                    dl.Width = new Unit("230");
                    cell.Controls.Add(dl);
                    row.Cells.Add(cell);
                }
                // Add the TableCell to the TableRow
                else
                {
                    tb.ID = "TextBoxRow_" + i + "Col_" + j;
                    tb.CausesValidation = false;
                    tb.Width = new Unit("210");
                    cell.Controls.Add(tb);
                    row.Cells.Add(cell);
                }
            }
            table.Rows.Add(row);
        }
        SetPreviousData(rowsCount, colsCount);
        rowsCount++;
        ViewState["RowsCount"] = rowsCount;
    }
}
Posted
Updated 4-Mar-10 19:57pm
v4

1 solution

I don't find any code where are you keeping a track of last data added. You keep track of rowCounts by Viewstate, what about data?

Keep a track of data added, preferably not in a viewstate as table data might be big.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900