簡體   English   中英

如何使用 C# 在 gridview 中讀取動態添加的文本框值?

[英]How to read dynamical added text box value inside a gridview using C#?

我想在 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);
        }
    }
}

我試過這個來獲取文本框的值,但它總是顯示為空:

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);
            }

        }
    }
}

您必須將它們添加到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);
        }
    }
}

因此,如果要分配文本,則必須使用 initialize 並將它們添加到RowCreated並使用RowDataBound

但是為什么不使用TemplateField並在那里添加文本框。 這讓你的生活更輕松。

旁注:您不需要DataControlRowType.DataRow是否枚舉網格的Rows -屬性,因為只返回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);
        }
    }
}

更新

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);
        }
    }
}
}

暫無
暫無

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

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