且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

asp.net网格视图将字段绑定到文本框

更新时间:2021-07-14 17:55:35

我不确定这是否适合您的情况,但是您可以尝试使用模板字段,如下所示:

I'm not sure if this will work for your situation, but you could try using a template field, like this:

<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("SomeValue")%>' ... />
    </ItemTemplate>
</asp:TemplateField>

编辑:将TextBox从后面的代码添加到项目模板:

EDIT: Adding TextBox to item template from code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) 
    {
        TemplateField txtColumn = new TemplateField();
        txtColumn.ItemTemplate = new TextColumn();
        GridView1.Columns.Add(txtColumn);
    }
}

public class TextColumn : ITemplate
{
    public void InstantiateIn(System.Web.UI.Control container)
    {
        TextBox txt = new TextBox();
        txt.ID = "MyTextBox";
        container.Controls.Add(txt);
    }
}

编辑:设置动态添加的文本框的文本

EDIT: Setting text of dynamically added TextBox

//get the cell and clear any existing controls
TableCell cell = e.Row.Cells[0];
cell.Controls.Clear();

//create a textbox and add it to the cell
TextBox txt = new TextBox(); 
txt.Text = cell.Text;    
cell.Controls.Add(txt);