2011-09-06 57 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