Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my button click code:

C#
protected void btnAddOwner_Click(object sender, EventArgs e)
    {
        if (ValidateTableData(tblPartner, "Javascript:alert('First enter all row(s) data!')"))
        {
            if (ViewState["DelayID"] == null) ViewState["DelayID"] = 0;
            AddPartnerRow(1, true);
            ViewState["DelayID"] = Convert.ToInt32(ViewState["DelayID"]) + 1;
            // Clearfield();
      
        }
    }


This is the function that I am using to generate the dynamic rows through code but when postback happens in case of using dropdown it removes all the rows and reaches to the default that I made in designing:

C#
    protected void AddPartnerRow(int rowCount, bool isNew)
    {
        int i = 0;
        if (isNew) { i = Convert.ToInt32(ViewState["DelayID"]);
        rowCount = Convert.ToInt32(ViewState["DelayID"]) + 1;
        }

        for (i=1; i < rowCount; i++)
        {
            TableRow Row = new TableRow();

            TableCell CellSNo = new TableCell();
            CellSNo.Text = tblPartner.Rows.Count.ToString();
            CellSNo.HorizontalAlign = HorizontalAlign.Center;
            CellSNo.VerticalAlign = VerticalAlign.Middle;
            CellSNo.CssClass = "lab_row";

            TableCell CellStartTime = new TableCell();
            CellStartTime.HorizontalAlign = HorizontalAlign.Center;
            CellStartTime.VerticalAlign = VerticalAlign.Middle;
            CellStartTime.CssClass = "lab_row";

            TableCell CellEndTime = new TableCell();
            CellEndTime.HorizontalAlign = HorizontalAlign.Center;
            CellEndTime.VerticalAlign = VerticalAlign.Middle;
            CellEndTime.CssClass = "lab_row";

            TableCell CellProject = new TableCell();
            CellProject.HorizontalAlign = HorizontalAlign.Center;
            CellProject.VerticalAlign = VerticalAlign.Middle;
            CellProject.CssClass = "lab_row";
           
            TableCell CellModule = new TableCell();
            CellModule.HorizontalAlign = HorizontalAlign.Center;
            CellModule.VerticalAlign = VerticalAlign.Middle;
            CellModule.CssClass = "lab_row";

            TableCell CellTask = new TableCell();
            CellTask.HorizontalAlign = HorizontalAlign.Center;
            CellTask.VerticalAlign = VerticalAlign.Middle;
            CellTask.CssClass = "lab_row";

            TableCell CellWorktype = new TableCell();
            CellWorktype.HorizontalAlign = HorizontalAlign.Center;
            CellWorktype.VerticalAlign = VerticalAlign.Middle;
            CellWorktype.CssClass = "lab_row";

            TableCell CellStatus = new TableCell();
            CellStatus.HorizontalAlign = HorizontalAlign.Center;
            CellStatus.VerticalAlign = VerticalAlign.Middle;
            CellStatus.CssClass = "lab_row";

            TableCell CellRemarks = new TableCell();
            CellRemarks.HorizontalAlign = HorizontalAlign.Center;
            CellRemarks.VerticalAlign = VerticalAlign.Middle;
            CellRemarks.CssClass = "lab_row";

            TextBox txtStartTime = new TextBox();
            txtStartTime.ID = "txtStartTime" + Convert.ToString(i + 4);//ControlsList.Count.ToString();// NextID("txtPartnerName");
            CellStartTime.Controls.Add(txtStartTime);
            txtStartTime.Width = 60;

            TextBox txtEndTime = new TextBox();
            txtEndTime.ID = "txtEndTime" + Convert.ToString(i + 4);//ControlsList.Count.ToString();// NextID("txtPartnerName");
            CellEndTime.Controls.Add(txtEndTime);
            txtEndTime.Width = 60;

            DropDownList ddlProject = new DropDownList();
            ddlProject.ID = "ddlProject" + Convert.ToString(i + 4); //NextID("txtPartnerAddress");
            CellProject.Controls.Add(ddlProject);
            ddlProject.AutoPostBack = true;
            ddlProject.Width = 70;
            ddlProject.SelectedValue.ToString();
            ddlProject.Text="";
            ddlProject.DataTextField="ProjectABR";
            ddlProject.DataValueField = "ProjectId";
            

            DropDownList ddlModule = new DropDownList();
            ddlModule.ID = "ddlModule" + Convert.ToString(i + 4); //NextID("txtPartnerPost");
            CellModule.Controls.Add(ddlModule);
            ddlModule.AutoPostBack = true;
            ddlModule.Width = 70;
            
            DropDownList ddlTask = new DropDownList();
            ddlTask.ID = "ddlTask" + Convert.ToString(i + 4); //NextID("txtPartnerPost");
            CellTask.Controls.Add(ddlTask);
            ddlTask.AutoPostBack = true;
            ddlTask.Width = 70;

            TextBox txtWorkType = new TextBox();
            txtWorkType.ID = "txtWorkType" + Convert.ToString(i + 4);//ControlsList.Count.ToString();// NextID("txtPartnerName");
            CellWorktype.Controls.Add(txtWorkType);
            txtWorkType.Width = 60;

            DropDownList ddlStatus = new DropDownList();
            ddlStatus.ID = "ddlStatus" + Convert.ToString(i + 4); //NextID("txtPartnerPost");
            CellStatus.Controls.Add(ddlStatus);
            ddlStatus.Width = 70;

            TextBox txtRemarks = new TextBox();
            txtRemarks.ID = "txtRemarks" + Convert.ToString(i + 4);//ControlsList.Count.ToString();// NextID("txtPartnerName");
            CellRemarks.Controls.Add(txtRemarks);
            txtRemarks.TextMode = TextBoxMode.MultiLine;
            txtRemarks.Width = 90;
            
            Row.Cells.Add(CellSNo);
            Row.Cells.Add(CellStartTime);
            Row.Cells.Add(CellEndTime);
            Row.Cells.Add(CellProject);
            Row.Cells.Add(CellModule);
            Row.Cells.Add(CellTask);
            Row.Cells.Add(CellWorktype);
            Row.Cells.Add(CellStatus);
            Row.Cells.Add(CellRemarks);
            tblPartner.Rows.Add(Row);
//another function
 protected bool ValidateTableData(Table tbl, string Message)
    {
        bool res = false;
        StringBuilder sb = new StringBuilder();
        foreach (Control tr in tbl.Rows)
        {
            if (!(tr is TableHeaderRow))
            {
                foreach (Control cell in tr.Controls)
                {
                    if (cell is TableCell)
                    {
                        foreach (Control ctl in cell.Controls)
                        {
                            if (ctl is TextBox)
                            {
                                TextBox txtValue = ctl as TextBox;
                                if (ctl != null)
                                {
                                    //if(txt.ID.StartsWith())
                                    sb.Append(txtValue.Text + "#");
                                }
                            }
                        }
                    }
                }
                string tmp = sb.ToString();
                if (tmp.Replace("#", "").Length < 1)
                    Response.Write(Message);
                else
                    res = true;
            }
        }
        return res;
    }


And when I generate rows the default rows dropdown connect to database the newly generated row dropdown won't able to connect to backend

Please help to check and tell me the right answer please please help me
Posted
Updated 7-Nov-11 23:48pm
v2
Comments
Smithers-Jones 8-Nov-11 5:49am    
Added code-block.

1 solution

As with any dynamically create controls in ASP.NET they must be recreated at postback.

You could use a DataList or Repeater control and bind it to some data source, then when needed a new row you add it to the data source and let the ASP.NET rendering handle creating the actual row elements.

If there is no data a simpler way to add a new row to the DOM would be to use JavaScript and clone an existing one and insert it

<table>
   <tr id='example'>
      <td>Something...</td>
   </tr>
</table>

var newRow = $("#example").clone();
$("#example").after(newRow);
 
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