asp:Table中的文本框在启用后会显示以前的值。禁用文本框后,效果很好

时间:2018-12-12 11:16:51

标签: c# asp.net textbox

我正在构建一个常规的asp:Table,用于显示从API提取的数据。数据将被编辑并保存回去。这部分工作正常。如果需要,用户可以添加新行以输入更多数据。添加新数据后,他按Update(更新)以通过API将更新的数据写入数据库。禁用文本框后,一切正常。添加一个空行,以前的数据移至下面的单元格。但是,启用文本框后,将复制之前单元格中的数据,而不显示空白行。我也处理了回发问题,因为添加行的功能在回发部分中。有什么办法吗? 代码如下:

async protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) //For first page load
    {
        string id = Request.QueryString["id"];

        JObject jsonresponse;

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost:3000/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            object values = new
            {
                documentId = Convert.ToInt16(id)
            };

            var response = await client.PostAsJsonAsync("admins/searchonedocument", values);

            var responseString2 = await response.Content.ReadAsStringAsync();

            jsonresponse = JObject.Parse(responseString2);
        }

        if ((bool)jsonresponse["success"])
        {
            lblDocumentID.Text = jsonresponse["document"]["documentId"].ToString();
            lblDocumentTitle.Text = jsonresponse["document"]["title"].ToString();
            lblDateModified.Text = jsonresponse["document"]["dateModified"].ToString();

            JArray subheadings = (JArray)jsonresponse["document"]["subheadings"];
            JArray content = (JArray)jsonresponse["document"]["text"];

            for (int i = 0; i < subheadings.Count; i++)
            {
                TableRow tr = new TableRow();

                TableCell tcsrno = new TableCell();
                tcsrno.Width = Unit.Percentage(5);
                TextBox tbsrno = new TextBox();
                tbsrno.Text = (i + 1).ToString();
                tbsrno.Enabled = false;
                tbsrno.Width = Unit.Percentage(95);
                tcsrno.Controls.Add(tbsrno);
                tr.Cells.Add(tcsrno);

                TableCell tcsection = new TableCell();
                tcsection.Width = Unit.Percentage(20);
                TextBox tbsection = new TextBox();
                tbsection.Text = subheadings[i].ToString();
                tbsection.Enabled = true;
                tbsection.Width = Unit.Percentage(95);
                tcsection.Controls.Add(tbsection);
                tr.Cells.Add(tcsection);

                TableCell tccontent = new TableCell();
                tccontent.Width = Unit.Percentage(75);
                TextBox tbcontent = new TextBox();
                tbcontent.TextMode = TextBoxMode.MultiLine;
                tbcontent.Height = 150;
                tbcontent.Text = content[i].ToString();
                tbcontent.Enabled = true;
                tbcontent.Width = Unit.Percentage(95);
                tccontent.Controls.Add(tbcontent);
                tr.Cells.Add(tccontent);

                tabledocuments.Rows.Add(tr);
            }
        }
    }
    else //Post Back
    {
        string id = Request.QueryString["id"];

        JObject jsonresponse;

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost:3000/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            object values = new
            {
                documentId = Convert.ToInt16(id)
            };

            var response = await client.PostAsJsonAsync("admins/searchonedocument", values);

            var responseString2 = await response.Content.ReadAsStringAsync();

            jsonresponse = JObject.Parse(responseString2);
        }

        if ((bool)jsonresponse["success"])
        {
            lblDocumentID.Text = jsonresponse["document"]["documentId"].ToString();
            lblDocumentTitle.Text = jsonresponse["document"]["title"].ToString();
            lblDateModified.Text = jsonresponse["document"]["dateModified"].ToString();

            JArray subheadings = (JArray)jsonresponse["document"]["subheadings"];
            JArray content = (JArray)jsonresponse["document"]["text"];
            bool isAdded = false;

            for (int i = 0, sectionnumber = 0; i < subheadings.Count; i++, sectionnumber++)
            {
                if (txtboxsectionnumber.Text.Length != 0 && (Convert.ToInt16(txtboxsectionnumber.Text) - 1) == i && !isAdded) //Adding new Row
                {
                    TableRow tr = new TableRow();

                    TableCell tcsrno = new TableCell();
                    tcsrno.Width = Unit.Percentage(5);
                    TextBox tbsrno = new TextBox();
                    tbsrno.Text = (sectionnumber + 1).ToString();
                    tbsrno.Enabled = false;
                    tbsrno.Width = Unit.Percentage(95);
                    tbsrno.Height = 100;
                    tcsrno.Controls.Add(tbsrno);
                    tr.Cells.Add(tcsrno);

                    TableCell tcsection = new TableCell();
                    tcsection.Width = Unit.Percentage(20);
                    TextBox tbsection = new TextBox();
                    tbsection.Attributes.Add("readonly", "readonly");
                    tbsection.Text = "";
                    tbsection.Enabled = true;
                    tbsection.Width = Unit.Percentage(95);
                    tcsection.Controls.Add(tbsection);
                    tr.Cells.Add(tcsection);

                    TableCell tccontent = new TableCell();
                    tccontent.Width = Unit.Percentage(75);
                    TextBox tbcontent = new TextBox();
                    tbcontent.TextMode = TextBoxMode.MultiLine;
                    tbcontent.Height = 150;
                    tbcontent.Text = "";
                    tbcontent.Enabled = true;
                    tbcontent.Width = Unit.Percentage(95);
                    tccontent.Controls.Add(tbcontent);
                    tr.Cells.Add(tccontent);

                    tabledocuments.Rows.Add(tr);
                    isAdded = true;
                    i--;
                }
                else
                {
                    TableRow tr = new TableRow();

                    TableCell tcsrno = new TableCell();
                    tcsrno.Width = Unit.Percentage(5);
                    TextBox tbsrno = new TextBox();
                    tbsrno.Text = (sectionnumber + 1).ToString();
                    tbsrno.Enabled = false;
                    tbsrno.Width = Unit.Percentage(95);
                    tcsrno.Controls.Add(tbsrno);
                    tr.Cells.Add(tcsrno);

                    TableCell tcsection = new TableCell();
                    tcsection.Width = Unit.Percentage(20);
                    TextBox tbsection = new TextBox();
                    tbsection.Text = subheadings[i].ToString();
                    tbsection.Enabled = true;
                    tbsection.Width = Unit.Percentage(95);
                    tcsection.Controls.Add(tbsection);
                    tr.Cells.Add(tcsection);

                    TableCell tccontent = new TableCell();
                    tccontent.Width = Unit.Percentage(75);
                    TextBox tbcontent = new TextBox();
                    tbcontent.TextMode = TextBoxMode.MultiLine;
                    tbcontent.Height = 150;
                    tbcontent.Text = content[i].ToString();
                    tbcontent.Enabled = true;
                    tbcontent.Width = Unit.Percentage(95);
                    tccontent.Controls.Add(tbcontent);
                    tr.Cells.Add(tccontent);


                    tabledocuments.Rows.Add(tr);
                }
            }
        }
    }
}

0 个答案:

没有答案