本文介绍了asp.net网格视图将字段绑定到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在gridview中有一个边界域.如何仅将特定列的边界域更改为文本框?
I have a boundfield in a gridview. How can I change the boundfield to a textbox only for a specific column?
我尝试了
BoundField colname= new BoundField();
grid.Columns.Add(colname as TextBox);
但是它具有强制转换表达式
But it goves a cast expression
推荐答案
我不确定这是否适合您的情况,但是您可以尝试使用模板字段,如下所示:
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);
这篇关于asp.net网格视图将字段绑定到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!