简体   繁体   English

ASP.NET C#如何生成动态控件

[英]ASP.NET C# How to generate dynamic Control

I'm using asp.net c# and I want to generate textbox from database. 我正在使用asp.net c#,我想从数据库生成文本框。 I have 4 records in my table so i want 4 textbox at run time. 我的表中有4条记录,因此我希望在运行时有4个文本框。 But I'm getting only one textbox when checking in Insepct Element I get 4 textboxes but it does't show on my page. 但是,当签入Insepct Element我只有一个文本框,但我却得到了4个文本框,但没有显示在页面上。

Not sure where it went wrong. 不知道哪里出了问题。

I'm using code like this 我正在使用这样的代码

OracleConnection obj_Conn = new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["OracleConn"].ToString());
    Table table = new Table();
    table.ID = "table1";
    string Query = "SELECT * FROM XXCUS.MASTER_VERIFICATION";
    OracleDataAdapter da = new OracleDataAdapter(Query, obj_Conn);
    //DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    da.Fill(dt);
    var Count = dt.Rows.Count;
    if (Count > 0)
    {
        TableRow row = new TableRow();
        TextBox txt = new TextBox();
        for (int i = 0; i < Count; i++)
        {
            TableCell cell = new TableCell();
            txt.ID = "txt" + i.ToString();
            cell.ID = "cell" + i.ToString();

            cell.Controls.Add(txt);

            row.Cells.Add(cell);
        }
        table.Rows.Add(row);
        dvGenerateCntrl.Controls.Add(table);
    }

and call this method on Page Load 并在页面加载时调用此方法

put the line which declare a new TextBox inside of for loop 将声明新TextBox放在for循环中

    for (int i = 0; i < Count; i++)
    {
        TextBox txt = new TextBox();
        TableCell cell = new TableCell();
        txt.ID = "txt" + i.ToString();
        cell.ID = "cell" + i.ToString();

        cell.Controls.Add(txt);

        row.Cells.Add(cell);
    }

First debug your code and make sure that you get 4 rows in your datatable and use this code. 首先调试代码,并确保您在得到4行datatable ,并使用此代码。

    OracleConnection obj_Conn = new `OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["oracleConn"].ToString());`
    Table table = new Table();
    table.ID = "table1";
    string Query = "SELECT * FROM XXCUS.MASTER_VERIFICATION";
    OracleDataAdapter da = new OracleDataAdapter(Query, obj_Conn);
    //DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    da.Fill(dt);
    var Count = dt.Rows.Count;
    if (Count > 0)
    {
        TableRow row = new TableRow();
        TextBox txt = new TextBox();
        for (int i = 0; i < Count; i++)
        {
TextBox txt = new TextBox();            
TableCell cell = new TableCell();
            txt.ID = "txt" + i.ToString();
            cell.ID = "cell" + i.ToString();

            cell.Controls.Add(txt);

            row.Cells.Add(cell);
        }
        table.Rows.Add(row);
        dvGenerateCntrl.Controls.Add(table);
    }

In your code you don't create object of TextBox every time. 在您的代码中,您不会每次都创建TextBox对象。 I think it will help you. 我认为它将为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM