2011-09-06 36 views
2

我在gridview中有一个绑定域。我如何才能将绑定字段更改为仅用于特定列的文本框?asp.net网格视图绑定字段到文本框

我试图

BoundField colname= new BoundField(); 
grid.Columns.Add(colname as TextBox); 

但goves转换表达式

回答

2

我不知道这是否会为你的情况下工作,但你可以尝试使用模板领域,像这样:

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

编辑

:从后面的代码中添加文本框项模板
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); 
    } 
} 

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

//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); 
+0

nopes我不的标记做到这一点...我需要背后做的方式代码...我要投的BoundField转换为文本框动态形成 – abbas

+0

后面的代码我不知道是否可以使用绑定字段来完成此操作,但是您可以从后面的代码中将模板字段添加到模板字段中。让我看看,我会编辑我的答案。 –

+0

好的..谢谢..我会等待 – abbas