【问题标题】:How to read dynamical added text box value inside a gridview using C#?如何使用 C# 在 gridview 中读取动态添加的文本框值?
【发布时间】:2016-05-11 07:58:20
【问题描述】:

我想在 GridView 的每个单元格中使用 TextBox 动态添加行和列。我已经成功地做到了。但问题是当我单击按钮时,我无法读取 TextBox 的值。

<asp:GridView runat="server" ID="gv" OnRowDataBound="gv_OnRowDataBound"></asp:GridView>

在网格中动态添加行和列:

protected void btnGenerate_OnClick(object sender, EventArgs e)
{
    int rowsCount = Convert.ToInt32(tbxRow.Text);
    int colsCount = Convert.ToInt32(tbxCol.Text);
    DataTable dt=new DataTable();
    for(int col=0;col<colsCount;col++)
    {
        dt.Columns.Add("D-" + col, typeof (int));
    }
    for (int i = 0; i < rowsCount; i++)
    {
        DataRow dr = dt.NewRow();
        dt.Rows.Add(dr);
    }
    gv.DataSource = dt;
    gv.DataBind();
}

这是我将文本框添加到 GridView 的代码:

protected void gv_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            TextBox txt = new TextBox();
            txt.ID = "tbx" + i;
            e.Row.Cells[i].Controls.Add(txt);
        }
    }
}

我试过这个来获取TextBox的值,但它总是显示为null:

protected void btnSave_OnClick(object sender, EventArgs e)
{
    foreach (GridViewRow row in gv.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < row.Cells.Count; i++)
            {
                TextBox tb = (TextBox) row.Cells[i].FindControl("tbx" + i);
            }

        }
    }
}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您必须将它们添加到 OnRowCreated 中,这不仅会在您对网格进行数据绑定时触发,而且会在每次回发时触发:

    protected void gv_OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                TextBox txt = new TextBox();
                txt.ID = "tbx" + i;
                e.Row.Cells[i].Controls.Add(txt);
            }
        }
    }
    

    因此,如果要分配文本,则必须使用初始化并将它们添加到 RowCreated 并使用 RowDataBound。

    但是你为什么不使用TemplateField 并在那里添加文本框。这让您的生活更轻松。

    旁注:如果您枚举网格的Rows-属性,则不需要DataControlRowType.DataRow-check,因为只返回DataRow-items:

    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gv.Rows)
        {
            for (int i = 0; i < row.Cells.Count; i++)
            {
                TextBox tb = (TextBox) row.Cells[i].FindControl("tbx" + i);
            }
        }
    }
    

    【讨论】:

    • 当我添加它时,它不显示文本框 Tim Schmelter
    • @Deba:你加了OnRowCreated="gv_OnRowCreated"吗?
    【解决方案2】:

    更新

    protected void GenerateGridColumn()
    {
        int colsCount = Convert.ToInt32(tbxCol.Text);
        TemplateField tfield;
        BoundField bfield = new BoundField();
        bfield.HeaderText = "";
        for (int col = 0; col <= colsCount; col++)
        {
            tfield = new TemplateField();
            tfield.HeaderText = "D-" + col;
            gv.Columns.Add(tfield);
        }
        tfield = new TemplateField();
        tfield.HeaderText = "Supply";
        gv.Columns.Add(tfield);
    
    }
    protected void btnGenerate_OnClick(object sender, EventArgs e)
    {
        GenerateGridColumn();
        int rowsCount = Convert.ToInt32(tbxRow.Text);
        DataTable dt = new DataTable();
        for (int i = 0; i <= rowsCount; i++)
        {
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
        }
        gv.DataSource = dt;
        gv.DataBind();
        btnSave.Visible = true;
    }
    
    
    protected void gv_OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                if (i > 0)
                {
                    TextBox txt = new TextBox();
                    txt.ID = "tbx" + i;
                    e.Row.Cells[i].Controls.Add(txt);
                }
            }
        }
    }
    
     protected void btnSave_OnClick(object sender, EventArgs e)
    {
    foreach (GridViewRow row in gv.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < row.Cells.Count; i++)
            {
                TextBox tb = (TextBox) row.Cells[i].FindControl("tbx" + i);
            }
        }
    }
    }
    

    【讨论】:

    • 你能提供任何示例代码或示例@Sarowar Hossen
    • 这只是示例 jquery 代码。我想你知道添加 webmethod 的其余部分。
    猜你喜欢
    • 2019-10-29
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    相关资源
    最近更新 更多