【问题标题】:asp.net grid view bound field to text boxasp.net 网格视图绑定字段到文本框
【发布时间】:2012-03-22 07:05:09
【问题描述】:

我在网格视图中有一个边界域。如何将绑定字段更改为仅针对特定列的文本框?

我试过了

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

但它有一个演员表

【问题讨论】:

    标签: asp.net


    【解决方案1】:

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

    <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);
        }
    }
    

    EDIT:设置动态添加的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);
    

    【讨论】:

    • 不,我不会在标记中这样做...我需要后面的代码来实现它...我想将 BoundField 动态转换为文本框,形成后面的代码
    • 我不知道您是否可以使用绑定字段来做到这一点,但是您可以从后面的代码中将文本框添加到模板字段中。让我检查一下,我会编辑我的答案。
    • 我没有模板字段...我在网格视图中有一个绑定字段,该字段是从后面的代码添加到网格视图中的。您能否修改我的代码 sn-p...我是有点困惑...谢谢
    • 不能用模板字段代替吗?我认为您不能将 TextBox 添加到绑定字段。
    • 嘿,谢谢,我找到了方法链接是forums.asp.net/t/1660176.aspx/1
    猜你喜欢
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 2012-10-13
    • 1970-01-01
    相关资源
    最近更新 更多