簡體   English   中英

如何從asp.net中動態創建的文本框中獲取價值

[英]how to get value from dynamically created textbox in asp.net

您好,我無法獲得價值,動態創建的文本框並保存到數據庫中。 請幫助我創建此代碼文本框中創建的代碼,但是當我在文本中輸入值並從動態創建的文本框中檢索值時,它給出了錯誤

 protected void btnAtt_Click(object sender, EventArgs e)
    {
        int DPLID = int.Parse(DPLCategory.Text);
        var query = (from p in database.tbl_Attributes
                     where p.ProductTypeId_FK == DPLID
                     select new
                     {
                         p.Attribute_Id,
                         p.AttributeName,
                         p.ProductTypeId_FK,

                     }).ToArray();
        for (int i = 0; i < query.Count(); i++)
        {
            Label lblatt = new Label();
            lblatt.ID = query[i].AttributeName;
            lblatt.Text = query[i].AttributeName + " : ";
            lblatt.CssClass = "control-label";
            TextBox txtatt = new TextBox();
            txtatt.ID = "txtatt"+i;
            txtatt.Attributes.Add("runat", "server");
            txtatt.Text = String.Empty;
            txtatt.CssClass = "form-control input-sm";
            HtmlTextWriterTag.Br.ToString();
            Place1.Controls.Add(lblatt);
            HtmlTextWriterTag.Br.ToString();
            Place1.Controls.Add(txtatt);
            HtmlTextWriterTag.Br.ToString();
        }
    }




 protected void lbtnSave_Click(object sender, EventArgs e)
    {
        int DPLID = int.Parse(DPLCategory.Text);
        var query = (from p in database.tbl_Attributes
                     where p.ProductTypeId_FK == DPLID
                     select new
                     {
                         p.Attribute_Id,
                         p.AttributeName,
                         p.ProductTypeId_FK,

                     }).ToArray();


            int LastId = database.tbl_Products.Max(p => p.ProductId);

            for (int i = 0; i < query.Count(); i++)
            {

                database.tbl_ProductValue.Add(new Models.tbl_ProductValue()
                    {
                        ProductId_FK = LastId,
                        AttributeID_FK = query[i].Attribute_Id,
                        ProductValue = ??,
                    });
                    database.SaveChanges();

            }


        }

請幫助我如何獲取文本框?

我已經有一段時間沒有使用WebForms了,但是您可以通過控件的ID來訪問它們,如下所示:

ProductValue  = ((TextBox)FindControl("txtatt" + i)).Text;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM