【问题标题】:Is there a way to get the values of Dynamically created Textbox inside a Dynamically created GridView? Asp.net有没有办法在动态创建的 GridView 中获取动态创建的文本框的值? ASP.NET
【发布时间】:2018-04-04 09:56:18
【问题描述】:

我有一个用于评估的 aspx 页面。该页面的要求之一是创建多个 Gridview 来充当评估表单。 Gridview 由来自我的数据库和文本框的问题以及问题填充。所以 Column1 = 问题Column2 = 文本框。创建的 Gridview 的数量取决于用户在文本框中输入的值。

我的问题是,因为它是动态创建的,我不知道我应该如何检索在文本框中键入的值。我需要将它们保存在我的数据库中。 我该怎么做?或者有更好的方法吗?

我的代码:

protected void btnOk_Click(object sender, EventArgs e)
{
    btnSave.Visible = true;
    int parCounter = Convert.ToInt32(participants.Text);
    DataTable dt = new DataTable();

    using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["TestCS"].ConnectionString))
    {
        string sql = "select * from evalForm";

        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }

    for (i = 1; i <= parCounter; i++)
    {
        GridView objGV = new GridView();
        objGV.AutoGenerateColumns = false;
        objGV.ID = "GV"+i;

        for (int col = 0; col < dt.Columns.Count; col++)
        {
            BoundField boundfield = new BoundField();                
            boundfield.DataField = dt.Columns[col].ColumnName.ToString();
            boundfield.HeaderText = dt.Columns[col].ColumnName.ToString();

            objGV.Columns.Add(boundfield);               
        }

        TemplateField tempfield = new TemplateField();
        tempfield.HeaderText = "Score";
        objGV.Columns.Add(tempfield);

        objGV.RowDataBound += new GridViewRowEventHandler(myGrid_RowDataBound);

        compName.Visible = true;
        fromDate.Visible = true;
        toDate.Visible = true;
        participants.Visible = true;

        objGV.DataSource = dt;
        objGV.DataBind();
        Panel1.ContentTemplateContainer.Controls.Add(objGV);
    }        
}

protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
            TextBox txtScore = new TextBox();
            txtScore.ID = "tbScore";
            e.Row.Cells[2].Controls.Add(txtScore);
    }
}

【问题讨论】:

  • 没有任何创建代码,我只能给你这个提示:“对每个元素使用相同的名称模式,最后增加 ID。所以你可以很容易地收集每个字段(问题/答案),因为你'知道'名字。你可以在 for/while 中循环它并在有 NotFound-Exception 时停止它。"
  • 与使用 FindControl 的 GridView 中的普通文本框相同。正如@LenglBoy 所说,为动态控件提供一个可用于再次找到它的 ID。
  • 这是我的问题,我用我的代码更新了我的问题。我不知道如何使用 FindControl 找到它,因为 Gridview 也是动态创建的。

标签: c# asp.net


【解决方案1】:

要查找任何动态控件,您必须使用 FindControl。这也适用于 GridView 控件。

//loop the dynamic gridviews
for (i = 1; i <= parCounter; i++)
{
    //use findcontrol and cast back to a gridview
    GridView gv = Panel1.ContentTemplateContainer.FindControl("GV" + i);

    //loop all the rows in the gridview
    foreach (GridViewRow row in gv.Rows)
    {
        //use findcontrol again to find the textbox
        TextBox tb = row.FindControl("tbScore") as TextBox;
    }
}

【讨论】:

  • 在为 GridView 使用 FindControl 时出现错误。它说'不能将类型 System.Web.UI.Control 隐式转换为 System.Web.UI.WebControls.GridView。存在显式转换(您是否缺少演员表?)'
  • 我能够通过在线添加 (GridView) 来处理错误。现在问题出在 foreach 循环上,它抛出 NullReferenceException - 对象引用未设置为对象的实例。顺便说一句,我在 OnClick 事件中添加了您的代码,我认为这是错误的原因。不过我不确定。
  • 哦,我现在可以解决我的问题了。只需要对我的代码进行一些修改。
猜你喜欢
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 2012-09-14
相关资源
最近更新 更多