请说明如何使用HTML标记生成动态行并防止回发 [英] Please tell how to generate dynamic rows in using HTML tags and prevent postback

查看:55
本文介绍了请说明如何使用HTML标记生成动态行并防止回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的按钮点击代码:

This is my button click code:

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:

    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

推荐答案

与ASP.NET中的任何动态创建控件一样,必须在回发时重新创建它们.

您可以使用DataList或Repeater控件并将其绑定到某个数据源,然后在需要时将新行添加到数据源中,并由ASP.NET呈现处理创建实际的行元素.

如果没有数据,则向DOM添加新行的更简单方法是使用JavaScript并克隆现有的行并将其插入

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").clone();


("#example").after(newRow);
("#example").after(newRow);


这篇关于请说明如何使用HTML标记生成动态行并防止回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆