我在gridview中有一个boundfield。如何仅将绑定字段更改为特定列的文本框?
我试过
BoundField colname= new BoundField();
grid.Columns.Add(colname as TextBox);
但它改编了演员表达
答案 0 :(得分:2)
我不确定这是否适用于您的情况,但您可以尝试使用模板字段,如下所示:
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("SomeValue")%>' ... />
</ItemTemplate>
</asp:TemplateField>
编辑:从后面的代码中将TextBox添加到项目模板:
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);
}
}
编辑:设置动态添加的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);