无法获取gridview动态标签ID,但文本框很好

时间:2018-10-30 11:39:04

标签: c# .net

这是我如何从gridview控件中获取值的方法,如您所见,我对标签的使用方式与文本框相同,不同之处在于,所有文本框都获得值且标签不起作用,并且我在其中找不到任何不同之处代码。函数fillGrid()只是表格的骨架,以便在将控制器放置在表格上之前先填充表格

foreach (GridViewRow row in GridView1.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        var Column1TextBoxes = Request.Form.AllKeys.Where(k => k.Contains("lblIdDetComp")).ToList();   // Gridview Column 1
                        var Column2TextBoxes = Request.Form.AllKeys.Where(k => k.Contains("txtComponente")).ToList();  // Gridview Column 2
                        var Column3TextBoxes = Request.Form.AllKeys.Where(k => k.Contains("txtBase")).ToList();        // Gridview Column 3
                        var Column4TextBoxes = Request.Form.AllKeys.Where(k => k.Contains("txtComprimento")).ToList(); // Gridview Column 4

                        if (Request.Form[Column1TextBoxes[j]] != "") comp[j].ID = Convert.ToInt32(Request.Form[Column1TextBoxes[j]]); // Column1 values
                        else break;
                        if (Request.Form[Column2TextBoxes[j]] != "") comp[j].Nome = Request.Form[Column2TextBoxes[j]]; // Column2 values
                        else break;
                        if (Request.Form[Column3TextBoxes[j]] != "") comp[j].Base = Request.Form[Column3TextBoxes[j]]; // Column3 values
                        else break;
                        if (Request.Form[Column4TextBoxes[j]] != "") comp[j].Comprimento = Convert.ToDouble(Request.Form[Column4TextBoxes[j]]); // Column4 values
                        else break;
                        j++;
                    }
                }


private void fillGrid()
    {
        int rowCount = 0;

        if (ViewState["rowCount"] != null)
        {
            rowCount = Convert.ToInt32(ViewState["rowCount"]);
        }

        DataTable dt = new DataTable();
        dt.Columns.Add("IdDetComp",typeof(string));
        dt.Columns.Add("Componente", typeof(string));
        dt.Columns.Add("Base", typeof(string));
        dt.Columns.Add("Comprimento", typeof(string));


        for (int i = 0; i < rowCount; i++)
        {
            dt.Rows.Add("","", "", "");
        }

        GridView1.DataSource = dt;
        GridView1.DataBind();

        upDetComps.Update();
    }

这是我将信息添加到gridview的地方

int i = 0;
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[0].Visible = false;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {                
            Componente[] Comp = (Componente[])ViewState["Componente"];

            if (i < Convert.ToInt32(txtNumComps.Text))
            {

                Label lblIdDetComp = new Label();
                lblIdDetComp.ID = "lblIdDetComp" + (i + 1).ToString();
                if (Comp[i] != null) lblIdDetComp.Text = Comp[i].ID.ToString();

                TextBox txtComponente = new TextBox();
                txtComponente.ID = "txtComponente" + (i + 1).ToString();
                if (Comp[i] != null) txtComponente.Text = Comp[i].Nome;                    

                TextBox txtBase = new TextBox();
                txtBase.ID = "txtBase" + (i + 1).ToString();
                if (Comp[i] != null) txtBase.Text = Comp[i].Base;

                TextBox txtComprimento = new TextBox();
                txtComprimento.ID = "txtComprimento" + (i + 1).ToString();

                if (Comp[i] != null)
                {
                    if (Comp[i].Comprimento != 0)
                        txtComprimento.Text = Comp[i].Comprimento.ToString();
                    else
                        txtComprimento.Text = "";
                }

                e.Row.Cells[0].Controls.Add(lblIdDetComp);
                e.Row.Cells[1].Controls.Add(txtComponente);
                e.Row.Cells[2].Controls.Add(txtBase);
                e.Row.Cells[3].Controls.Add(txtComprimento);

                i++;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我的猜测是这是因为默认情况下,Label.AutoSize属性为false

  

属性值

     

布尔值

     

true,如果控件调整其宽度以使其内容紧密匹配;否则为false

     

使用设计器添加到表单时,默认值为true。从代码实例化时,默认值为false

所以文本在那里,只有大小(0,0)。

尝试设置

lblIdDetComp.AutoSize = true;